~kmaasrud/pimalaya-webfinger

38c80b3bdb3f4f2ec0b89a72852f119398b479a0 — Knut Magnus Aasrud 10 months ago 6804234
style: simplify rel module with macros
1 files changed, 147 insertions(+), 282 deletions(-)

M src/rel.rs
M src/rel.rs => src/rel.rs +147 -282
@@ 1,287 1,152 @@
use serde::{Deserialize, Serialize};

/// A link relation type as defined in [RFC 8288](https://www.rfc-editor.org/rfc/rfc8288.html).
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Rel {
    About,
    Acl,
    Alternate,
    Amphtml,
    Appendix,
    #[serde(rename = "apple-touch-icon")]
    AppleTouchIcon,
    #[serde(rename = "apple-touch-startup-image")]
    AppleTouchStartupImage,
    Archives,
    Attachment,
    Author,
    #[serde(rename = "blocked-by")]
    BlockedBy,
    Bookmark,
    Canonical,
    Chapter,
    #[serde(rename = "cite-as")]
    CiteAs,
    Collection,
    Contents,
    ConvertedFrom,
    Copyright,
    #[serde(rename = "create-form")]
    CreateForm,
    Current,
    DescribedBy,
    Describes,
    Disclosure,
    #[serde(rename = "dns-prefetch")]
    DnsPrefetch,
    Duplicate,
    Edit,
    #[serde(rename = "edit-form")]
    EditForm,
    #[serde(rename = "edit-media")]
    EditMedia,
    Enclosure,
    External,
    First,
    Glossary,
    Help,
    Home,
    Hosts,
    Hub,
    Icon,
    Index,
    #[serde(rename = "in-reply-to")]
    InReplyTo,
    IntervalAfter,
    IntervalBefore,
    IntervalContains,
    IntervalDisjoint,
    IntervalDuring,
    IntervalEquals,
    IntervalFinishedBy,
    IntervalFinishes,
    IntervalIn,
    IntervalMeets,
    IntervalMetBy,
    IntervalOverlappedBy,
    IntervalOverlaps,
    IntervalStartedBy,
    IntervalStarts,
    Item,
    Last,
    #[serde(rename = "latest-version")]
    LatestVersion,
    License,
    Linkset,
    Lrdd,
    Manifest,
    MaskIcon,
    MediaFeed,
    Memento,
    MicroPub,
    ModulePreload,
    Monitor,
    MonitorGroup,
    Next,
    NextArchive,
    NoFollow,
    NoOpener,
    NoReferrer,
    Opener,
    #[serde(rename = "openid2.local_id")]
    OpenId2LocalId,
    #[serde(rename = "openid2.provider")]
    OpenId2Provider,
    Original,
    P3pv1,
    Payment,
    Pingback,
    PreConnect,
    #[serde(rename = "predecessor-version")]
    PredecessorVersion,
    PreFetch,
    PreLoad,
    PreRender,
    Prev,
    Preview,
    Previous,
    #[serde(rename = "prev-archive")]
    PrevArchive,
    #[serde(rename = "privacy-policy")]
    PrivacyPolicy,
    Profile,
    Publication,
    Related,
    RestConf,
    Replies,
    RuleInput,
    Search,
    Section,
    #[serde(rename = "self")]
    Self_,
    Service,
    #[serde(rename = "service-desc")]
    ServiceDesc,
    #[serde(rename = "service-doc")]
    ServiceDoc,
    #[serde(rename = "service-meta")]
    ServiceMeta,
    #[serde(rename = "sip-trunking-capability")]
    SipTrunkingCapability,
    Sponsored,
    Start,
    Status,
    Stylesheet,
    Subsection,
    #[serde(rename = "successor-version")]
    SuccessorVersion,
    Sunset,
    Tag,
    #[serde(rename = "terms-of-service")]
    TermsOfService,
    TimeGate,
    TimeMap,
    Type,
    Ugc,
    Up,
    #[serde(rename = "version-history")]
    VersionHistory,
    Via,
    WebMention,
    #[serde(rename = "working-copy")]
    WorkingCopy,
    #[serde(rename = "working-copy-of")]
    WorkingCopyOf,
}
macro_rules! rel {
    { $( ($variant:ident, $name:literal), )* } => {
        /// A link relation type as defined in [RFC 8288](https://www.rfc-editor.org/rfc/rfc8288.html).
        #[derive(Debug, Deserialize, PartialEq, Serialize)]
        pub enum Rel {
            $(
                #[serde(rename = $name)]
                $variant
            ),*

        }

impl AsRef<str> for Rel {
    fn as_ref(&self) -> &str {
        match self {
            Rel::About => "about",
            Rel::Acl => "acl",
            Rel::Alternate => "alternate",
            Rel::Amphtml => "amphtml",
            Rel::Appendix => "appendix",
            Rel::AppleTouchIcon => "apple-touch-icon",
            Rel::AppleTouchStartupImage => "apple-touch-startup-image",
            Rel::Archives => "archives",
            Rel::Attachment => "attachment",
            Rel::Author => "author",
            Rel::BlockedBy => "blocked-by",
            Rel::Bookmark => "bookmark",
            Rel::Canonical => "canonical",
            Rel::Chapter => "chapter",
            Rel::CiteAs => "cite-as",
            Rel::Collection => "collection",
            Rel::Contents => "contents",
            Rel::ConvertedFrom => "converted-from",
            Rel::Copyright => "copyright",
            Rel::CreateForm => "create-form",
            Rel::Current => "current",
            Rel::DescribedBy => "described-by",
            Rel::Describes => "describes",
            Rel::Disclosure => "disclosure",
            Rel::DnsPrefetch => "dns-prefetch",
            Rel::Duplicate => "duplicate",
            Rel::Edit => "edit",
            Rel::EditForm => "edit-form",
            Rel::EditMedia => "edit-media",
            Rel::Enclosure => "enclosure",
            Rel::External => "external",
            Rel::First => "first",
            Rel::Glossary => "glossary",
            Rel::Help => "help",
            Rel::Home => "home",
            Rel::Hosts => "hosts",
            Rel::Hub => "hub",
            Rel::Icon => "icon",
            Rel::Index => "index",
            Rel::InReplyTo => "in-reply-to",
            Rel::IntervalAfter => "interval-after",
            Rel::IntervalBefore => "interval-before",
            Rel::IntervalContains => "interval-contains",
            Rel::IntervalDisjoint => "interval-disjoint",
            Rel::IntervalDuring => "interval-during",
            Rel::IntervalEquals => "interval-equals",
            Rel::IntervalFinishedBy => "interval-finished-by",
            Rel::IntervalFinishes => "interval-finishes",
            Rel::IntervalIn => "interval-in",
            Rel::IntervalMeets => "interval-meets",
            Rel::IntervalMetBy => "interval-met-by",
            Rel::IntervalOverlappedBy => "interval-overlapped-by",
            Rel::IntervalOverlaps => "interval-overlaps",
            Rel::IntervalStartedBy => "interval-started-by",
            Rel::IntervalStarts => "interval-starts",
            Rel::Item => "item",
            Rel::Last => "last",
            Rel::LatestVersion => "latest-version",
            Rel::License => "license",
            Rel::Linkset => "linkset",
            Rel::Lrdd => "lrdd",
            Rel::Manifest => "manifest",
            Rel::MaskIcon => "mask-icon",
            Rel::MediaFeed => "media-feed",
            Rel::Memento => "memento",
            Rel::MicroPub => "micropub",
            Rel::ModulePreload => "module-preload",
            Rel::Monitor => "monitor",
            Rel::MonitorGroup => "monitor-group",
            Rel::Next => "next",
            Rel::NextArchive => "next-archive",
            Rel::NoFollow => "nofollow",
            Rel::NoOpener => "noopener",
            Rel::NoReferrer => "noreferrer",
            Rel::Opener => "opener",
            Rel::OpenId2LocalId => "openid2.local_id",
            Rel::OpenId2Provider => "openid2.provider",
            Rel::Original => "original",
            Rel::P3pv1 => "p3pv1",
            Rel::Payment => "payment",
            Rel::Pingback => "pingback",
            Rel::PreConnect => "preconnect",
            Rel::PredecessorVersion => "predecessor-version",
            Rel::PreFetch => "prefetch",
            Rel::PreLoad => "preload",
            Rel::PreRender => "prerender",
            Rel::Prev => "prev",
            Rel::Preview => "preview",
            Rel::Previous => "previous",
            Rel::PrevArchive => "prev-archive",
            Rel::PrivacyPolicy => "privacy-policy",
            Rel::Profile => "profile",
            Rel::Publication => "publication",
            Rel::Related => "related",
            Rel::RestConf => "restconf",
            Rel::Replies => "replies",
            Rel::RuleInput => "rule-input",
            Rel::Search => "search",
            Rel::Section => "section",
            Rel::Self_ => "self",
            Rel::Service => "service",
            Rel::ServiceDesc => "service-desc",
            Rel::ServiceDoc => "service-doc",
            Rel::ServiceMeta => "service-meta",
            Rel::SipTrunkingCapability => "sip-trunking-capability",
            Rel::Sponsored => "sponsored",
            Rel::Start => "start",
            Rel::Status => "status",
            Rel::Stylesheet => "stylesheet",
            Rel::Subsection => "subsection",
            Rel::SuccessorVersion => "successor-version",
            Rel::Sunset => "sunset",
            Rel::Tag => "tag",
            Rel::TermsOfService => "terms-of-service",
            Rel::TimeGate => "timegate",
            Rel::TimeMap => "timemap",
            Rel::Type => "type",
            Rel::Ugc => "ugc",
            Rel::Up => "up",
            Rel::VersionHistory => "version-history",
            Rel::Via => "via",
            Rel::WebMention => "webmention",
            Rel::WorkingCopy => "working-copy",
            Rel::WorkingCopyOf => "working-copy-of",
        impl AsRef<str> for Rel {
            fn as_ref(&self) -> &str {
                match self {
                    $(
                        Self::$variant => $name,
                    )*
                }
            }
        }
    }
    };
}

rel! {
    (About, "about"),
    (Acl, "acl"),
    (Alternate, "alternate"),
    (Amphtml, "amphtml"),
    (Appendix, "appendix"),
    (AppleTouchIcon, "apple-touch-icon"),
    (AppleTouchStartupImage, "apple-touch-startup-image"),
    (Archives, "archives"),
    (Attachment, "attachment"),
    (Author, "author"),
    (BlockedBy, "blocked-by"),
    (Bookmark, "bookmark"),
    (Canonical, "canonical"),
    (Chapter, "chapter"),
    (CiteAs, "cite-as"),
    (Collection, "collection"),
    (Contents, "contents"),
    (ConvertedFrom, "convertedFrom"),
    (Copyright, "copyright"),
    (CreateForm, "create-form"),
    (Current, "current"),
    (DescribedBy, "describedby"),
    (Describes, "describes"),
    (Disclosure, "disclosure"),
    (DnsPrefetch, "dns-prefetch"),
    (Duplicate, "duplicate"),
    (Edit, "edit"),
    (EditForm, "edit-form"),
    (EditMedia, "edit-media"),
    (Enclosure, "enclosure"),
    (External, "external"),
    (First, "first"),
    (Glossary, "glossary"),
    (Help, "help"),
    (Home, "home"),
    (Hosts, "hosts"),
    (Hub, "hub"),
    (Icon, "icon"),
    (Index, "index"),
    (InReplyTo, "in-reply-to"),
    (IntervalAfter, "intervalafter"),
    (IntervalBefore, "intervalbefore"),
    (IntervalContains, "intervalcontains"),
    (IntervalDisjoint, "intervaldisjoint"),
    (IntervalDuring, "intervalduring"),
    (IntervalEquals, "intervalequals"),
    (IntervalFinishedBy, "intervalfinishedby"),
    (IntervalFinishes, "intervalfinishes"),
    (IntervalIn, "intervalin"),
    (IntervalMeets, "intervalmeets"),
    (IntervalMetBy, "intervalmetby"),
    (IntervalOverlappedBy, "intervaloverlappedby"),
    (IntervalOverlaps, "intervaloverlaps"),
    (IntervalStartedBy, "intervalstartedby"),
    (IntervalStarts, "intervalstarts"),
    (Item, "item"),
    (Last, "last"),
    (LatestVersion, "latest-version"),
    (License, "license"),
    (Linkset, "linkset"),
    (Lrdd, "lrdd"),
    (Manifest, "manifest"),
    (MaskIcon, "maskicon"),
    (MediaFeed, "mediafeed"),
    (Memento, "memento"),
    (MicroPub, "micropub"),
    (ModulePreload, "modulepreload"),
    (Monitor, "monitor"),
    (MonitorGroup, "monitorgroup"),
    (Next, "next"),
    (NextArchive, "nextarchive"),
    (NoFollow, "nofollow"),
    (NoOpener, "noopener"),
    (NoReferrer, "noreferrer"),
    (Opener, "opener"),
    (OpenId2LocalId, "openid2.local_id"),
    (OpenId2Provider, "openid2.provider"),
    (Original, "original"),
    (P3pv1, "p3pv1"),
    (Payment, "payment"),
    (Pingback, "pingback"),
    (PreConnect, "preconnect"),
    (PredecessorVersion, "predecessor-version"),
    (PreFetch, "prefetch"),
    (PreLoad, "preload"),
    (PreRender, "prerender"),
    (Prev, "prev"),
    (Preview, "preview"),
    (Previous, "previous"),
    (PrevArchive, "prev-archive"),
    (PrivacyPolicy, "privacy-policy"),
    (Profile, "profile"),
    (Publication, "publication"),
    (Related, "related"),
    (RestConf, "restconf"),
    (Replies, "replies"),
    (RuleInput, "ruleinput"),
    (Search, "search"),
    (Section, "section"),
    (Self_, "self"),
    (Service, "service"),
    (ServiceDesc, "service-desc"),
    (ServiceDoc, "service-doc"),
    (ServiceMeta, "service-meta"),
    (SipTrunkingCapability, "sip-trunking-capability"),
    (Sponsored, "sponsored"),
    (Start, "start"),
    (Status, "status"),
    (Stylesheet, "stylesheet"),
    (Subsection, "subsection"),
    (SuccessorVersion, "successor-version"),
    (Sunset, "sunset"),
    (Tag, "tag"),
    (TermsOfService, "terms-of-service"),
    (TimeGate, "timegate"),
    (TimeMap, "timemap"),
    (Type, "type"),
    (Ugc, "ugc"),
    (Up, "up"),
    (VersionHistory, "version-history"),
    (Via, "via"),
    (WebMention, "webmention"),
    (WorkingCopy, "working-copy"),
    (WorkingCopyOf, "working-copy-of"),
}