|
1 |
| -//! The Idea is to parse smth like #[path(value, key=value, key(value))] for arbitrary types really. |
2 |
| -//! Parsing works the same way as a function call... meaning any syntax valid ther + the additional |
3 |
| -//! key=value. One important thing to keep in mind, is the limitation on values being valid |
4 |
| -//! expresions so we can rely on syn for the , splitting :D |
5 |
| -
|
| 1 | +//! Basic gist is a struct like this: |
| 2 | +//! ``` |
| 3 | +//! #[derive(Attribute)] |
| 4 | +//! #[attribute(ident = "collection")] |
| 5 | +//! #[attribute(invalid_field = "Error when an unsupported value is set (e.g. meaning=42")] |
| 6 | +//! struct CollectionAttribute { |
| 7 | +//! // Options are optional by default (will be set to None if not specified) |
| 8 | +//! authority: Option<String>, |
| 9 | +//! #[attribute(missing = "Error when the value is not set")] |
| 10 | +//! name: String, |
| 11 | +//! // Any type implementing default can be flagged as default |
| 12 | +//! // This will be set to Vec::default() when not specified |
| 13 | +//! #[attribute(default)] |
| 14 | +//! #[attribute(expected = "Error when an error occured while parsing")] |
| 15 | +//! views: Vec<Type>, |
| 16 | +//! } |
| 17 | +//! ``` |
| 18 | +//! |
| 19 | +//! Will be able to parse an attribute like this: |
| 20 | +//! ``` |
| 21 | +//! #[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()])] |
| 22 | +//! ``` |
| 23 | +//! |
| 24 | +//! Any type that [`ConvertParsed`] is implemented for is supported. These should be the general |
| 25 | +//! types that syn supports like [`LitStr`](struct@LitStr) or [`Type`] or that have a direct equivalent in those |
| 26 | +//! like [`String`], [`char`] or [`f32`]. A special treatment have [`Vecs`](Vec) which are parsed |
| 27 | +//! using [`Array`] with the syntax `[a, b, c]` and [`Options`](Option) that will be [`None`] if |
| 28 | +//! not specified and [`Some`] when the value is specified via the attribute. It is not |
| 29 | +//! specified via `Some(value)` but as just `value`. |
6 | 30 | use std::fmt::Display;
|
7 | 31 |
|
8 | 32 | use proc_macro2::{Literal, Span};
|
| 33 | +#[doc(hidden)] |
9 | 34 | pub use r#macro::Attribute;
|
10 | 35 | use syn::{
|
11 | 36 | bracketed, parse::Parse, punctuated::Punctuated, Expr, Lit, LitBool, LitByteStr, LitChar,
|
12 | 37 | LitFloat, LitInt, LitStr, Path, Result, Token, Type, __private::ToTokens,
|
13 | 38 | };
|
14 | 39 |
|
| 40 | +#[deny(missing_docs)] |
| 41 | + |
| 42 | +#[doc(hidden)] |
15 | 43 | pub mod __private {
|
16 | 44 | pub use proc_macro2;
|
17 | 45 | pub use syn;
|
18 | 46 | }
|
19 | 47 |
|
| 48 | +/// The trait you actually derive on your attribute struct. |
| 49 | +/// |
| 50 | +/// Basic gist is a struct like this: |
| 51 | +/// ``` |
| 52 | +/// #[derive(Attribute)] |
| 53 | +/// #[attribute(ident = "collection")] |
| 54 | +/// #[attribute(invalid_field = "Error when an unsupported value is set (e.g. meaning=42")] |
| 55 | +/// struct CollectionAttribute { |
| 56 | +/// // Options are optional by default (will be set to None if not specified) |
| 57 | +/// authority: Option<String>, |
| 58 | +/// #[attribute(missing = "Error when the value is not set")] |
| 59 | +/// name: String, |
| 60 | +/// // Any type implementing default can be flagged as default |
| 61 | +/// // This will be set to Vec::default() when not specified |
| 62 | +/// #[attribute(default)] |
| 63 | +/// #[attribute(expected = "Error when an error occured while parsing")] |
| 64 | +/// views: Vec<Type>, |
| 65 | +/// } |
| 66 | +/// ``` |
| 67 | +/// |
| 68 | +/// Will be able to parse an attribute like this: |
| 69 | +/// ``` |
| 70 | +/// #[collection(authority="Some String", name = r#"Another string"#, views = [Option, ()])] |
| 71 | +/// ``` |
20 | 72 | pub trait Attribute
|
21 | 73 | where
|
22 | 74 | Self: Sized,
|
23 | 75 | {
|
| 76 | + /// Parses an [`IntoIterator`] of [`syn::Attributes`](syn::Attribute) e.g. |
| 77 | + /// [`Vec<Attribute>`](Vec). |
| 78 | + /// |
| 79 | + /// It can therefore parse fields set over multiple attributes like: |
| 80 | + /// ``` |
| 81 | + /// #[collection(authority = "Authority", name = "Name")] |
| 82 | + /// #[collection(views = [A, B])] |
| 83 | + /// ``` |
| 84 | + /// and also catch duplicate/conflicting settings over those. |
| 85 | + /// |
| 86 | + /// Fails with a [`syn::Error`] so you can conveniently return that as a compiler error in a proc |
| 87 | + /// macro. |
24 | 88 | fn from_attributes(attrs: impl IntoIterator<Item = syn::Attribute>) -> Result<Self>;
|
25 | 89 | }
|
26 | 90 |
|
| 91 | +/// Helper trait to convert syn types implementing [`Parse`] like [`LitStr`](struct@LitStr) to rust |
| 92 | +/// types like [`String`] |
| 93 | +/// |
| 94 | +/// You probably don't need to implement this trait, as most syn types like [`LitStr`](struct@LitStr) |
| 95 | +/// and [`Type`] or that have a direct equivalent in those like [`String`], [`char`] or [`f32`] are |
| 96 | +/// already implemented. A special treatment have [`Vecs`](Vec) which are parsed |
| 97 | +/// using the helper [`Array`] with the syntax `[a, b, c]` and [`Options`](Option) that will be |
| 98 | +/// [`None`] if not specified and [`Some`] when the value is specified via the attribute. It is not |
| 99 | +/// specified via `Some(value)` but as just `value`. |
27 | 100 | pub trait ConvertParsed
|
28 | 101 | where
|
29 | 102 | Self: Sized,
|
30 | 103 | Self::Type: Error,
|
31 | 104 | {
|
| 105 | + /// The type this can be converted from |
32 | 106 | type Type;
|
| 107 | + /// This takes [`Self::Type`] and converts it to [`Self`]. |
| 108 | + /// |
| 109 | + /// This can return an error, e.g. when parsing an integer too large for a [`u8`] into an `u8` |
33 | 110 | fn convert(value: Self::Type) -> Result<Self>;
|
| 111 | + /// Should values of this type return their default when they are not specified even when the |
| 112 | + /// `default` flag is not specified (only returns `true` for [`Option`] currently) |
34 | 113 | fn default_by_default() -> bool {
|
35 | 114 | false
|
36 | 115 | }
|
| 116 | + /// The default value, this is necessary to implement the implicit default behavior of |
| 117 | + /// [`Option`] |
37 | 118 | fn default() -> Self {
|
38 | 119 | unreachable!("default_by_default should only return true if this is overridden")
|
39 | 120 | }
|
40 | 121 | }
|
41 | 122 |
|
42 | 123 | /// Helper trait to generate sensible errors
|
43 | 124 | pub trait Error {
|
| 125 | + /// This is used to be able to create errors more easily. Mostly used through the |
| 126 | + /// implementation for [`T: ToTokens`](ToTokens). |
44 | 127 | fn error(&self, message: impl Display) -> syn::Error;
|
45 | 128 | }
|
46 | 129 |
|
|
53 | 136 | }
|
54 | 137 | }
|
55 | 138 |
|
| 139 | +/// Macro to easily implement [`ConvertParsed`] for syn types |
56 | 140 | macro_rules! convert_parsed {
|
57 | 141 | ($type:path) => {
|
58 | 142 | impl ConvertParsed for $type {
|
@@ -130,7 +214,8 @@ where
|
130 | 214 | }
|
131 | 215 | }
|
132 | 216 |
|
133 |
| -/// Helper struct to parse array literals |
| 217 | +/// Helper struct to parse array literals: |
| 218 | +/// `[a, b, c]` |
134 | 219 | pub struct Array<T> {
|
135 | 220 | data: Vec<T>,
|
136 | 221 | span: Span,
|
|
0 commit comments