summaryrefslogtreecommitdiff
path: root/src/url.rs
blob: 734d1a400884d7b8a45090266ecf9ef970e77b96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
    parenthesized,
    parse::{Parse, ParseStream},
    Fields, Token,
};

use crate::{
    diagnostic::{DiagnosticConcreteArgs, DiagnosticDef},
    utils::{display_pat_members, gen_all_variants_with, gen_unused_pat},
};
use crate::{
    fmt::{self, Display},
    forward::WhichFn,
};

pub enum Url {
    Display(Display),
    DocsRs,
}

impl Parse for Url {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident = input.parse::<syn::Ident>()?;
        if ident == "url" {
            let la = input.lookahead1();
            if la.peek(syn::token::Paren) {
                let content;
                parenthesized!(content in input);
                if content.peek(syn::LitStr) {
                    let fmt = content.parse()?;
                    let args = if content.is_empty() {
                        TokenStream::new()
                    } else {
                        fmt::parse_token_expr(&content, false)?
                    };
                    let display = Display {
                        fmt,
                        args,
                        has_bonus_display: false,
                    };
                    Ok(Url::Display(display))
                } else {
                    let option = content.parse::<syn::Ident>()?;
                    if option == "docsrs" {
                        Ok(Url::DocsRs)
                    } else {
                        Err(syn::Error::new(option.span(), "Invalid argument to url() sub-attribute. It must be either a string or a plain `docsrs` identifier"))
                    }
                }
            } else {
                input.parse::<Token![=]>()?;
                Ok(Url::Display(Display {
                    fmt: input.parse()?,
                    args: TokenStream::new(),
                    has_bonus_display: false,
                }))
            }
        } else {
            Err(syn::Error::new(ident.span(), "not a url"))
        }
    }
}

impl Url {
    pub(crate) fn gen_enum(
        enum_name: &syn::Ident,
        variants: &[DiagnosticDef],
    ) -> Option<TokenStream> {
        gen_all_variants_with(
            variants,
            WhichFn::Url,
            |ident, fields, DiagnosticConcreteArgs { url, .. }| {
                let (pat, fmt, args) = match url.as_ref()? {
                    // fall through to `_ => None` below
                    Url::Display(display) => {
                        let (display_pat, display_members) = display_pat_members(fields);
                        let (fmt, args) = display.expand_shorthand_cloned(&display_members);
                        (display_pat, fmt.value(), args)
                    }
                    Url::DocsRs => {
                        let pat = gen_unused_pat(fields);
                        let fmt =
                            "https://docs.rs/{crate_name}/{crate_version}/{mod_name}/{item_path}"
                                .into();
                        let item_path = format!("enum.{}.html#variant.{}", enum_name, ident);
                        let args = quote! {
                            ,
                            crate_name=env!("CARGO_PKG_NAME"),
                            crate_version=env!("CARGO_PKG_VERSION"),
                            mod_name=env!("CARGO_PKG_NAME").replace('-', "_"),
                            item_path=#item_path
                        };
                        (pat, fmt, args)
                    }
                };
                Some(quote! {
                    Self::#ident #pat => std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args))),
                })
            },
        )
    }

    pub(crate) fn gen_struct(
        &self,
        struct_name: &syn::Ident,
        fields: &Fields,
    ) -> Option<TokenStream> {
        let (pat, fmt, args) = match self {
            Url::Display(display) => {
                let (display_pat, display_members) = display_pat_members(fields);
                let (fmt, args) = display.expand_shorthand_cloned(&display_members);
                (display_pat, fmt.value(), args)
            }
            Url::DocsRs => {
                let pat = gen_unused_pat(fields);
                let fmt =
                    "https://docs.rs/{crate_name}/{crate_version}/{mod_name}/{item_path}".into();
                let item_path = format!("struct.{}.html", struct_name);
                let args = quote! {
                    ,
                    crate_name=env!("CARGO_PKG_NAME"),
                    crate_version=env!("CARGO_PKG_VERSION"),
                    mod_name=env!("CARGO_PKG_NAME").replace('-', "_"),
                    item_path=#item_path
                };
                (pat, fmt, args)
            }
        };
        Some(quote! {
            fn url(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
                #[allow(unused_variables, deprecated)]
                let Self #pat = self;
                std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args)))
            }
        })
    }
}