aboutsummaryrefslogtreecommitdiffstats
path: root/crates/mk_config/src/parse.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/mk_config/src/parse.rs')
-rw-r--r--crates/mk_config/src/parse.rs145
1 files changed, 145 insertions, 0 deletions
diff --git a/crates/mk_config/src/parse.rs b/crates/mk_config/src/parse.rs
new file mode 100644
index 0000000..c48aef6
--- /dev/null
+++ b/crates/mk_config/src/parse.rs
@@ -0,0 +1,145 @@
+use syn::parse::{Parse, ParseStream, Result};
+use syn::token::Brace;
+use syn::{Attribute, Expr, Ident, Token, Type, braced, bracketed};
+
+#[derive(Debug)]
+pub(crate) struct ParseBase {
+ pub(crate) name: Ident,
+ pub(crate) fields: Vec<ParsePart>,
+ pub(crate) imports: Vec<syn::Path>,
+}
+
+#[derive(Clone, Debug)]
+#[expect(clippy::large_enum_variant)]
+pub(crate) enum ParsePart {
+ Section {
+ name: Ident,
+ fields: Vec<ParsePart>,
+ documentation: Vec<Attribute>,
+ },
+ Field {
+ name: Ident,
+ ty: Type,
+ default: Option<FieldDefault>,
+ documentation: Vec<Attribute>,
+ finalizer: Option<Ident>,
+ },
+}
+
+#[derive(Clone, Debug)]
+pub(crate) struct FieldDefault {
+ pub(crate) default: Expr,
+ pub(crate) may_error: bool,
+}
+
+impl Parse for FieldDefault {
+ fn parse(input: ParseStream<'_>) -> Result<Self> {
+ let lookahead = input.lookahead1();
+ let (may_error, expr) = if lookahead.peek(Token![try]) {
+ input.parse::<Token![try]>()?;
+ (true, input.parse()?)
+ } else {
+ (false, input.parse()?)
+ };
+
+ Ok(Self {
+ default: expr,
+ may_error,
+ })
+ }
+}
+
+impl Parse for ParsePart {
+ fn parse(input: ParseStream<'_>) -> Result<Self> {
+ let documentation = input.call(Attribute::parse_outer)?;
+
+ let name = input.parse()?;
+ input.parse::<Token![:]>()?;
+
+ let lookahead = input.lookahead1();
+ if lookahead.peek(Brace) {
+ let content;
+ braced!(content in input);
+ let fields = content
+ .parse_terminated(ParsePart::parse, Token![,])?
+ .into_iter()
+ .collect();
+
+ Ok(Self::Section {
+ name,
+ fields,
+ documentation,
+ })
+ } else {
+ let ty = input.parse()?;
+
+ let lookahead = input.lookahead1();
+ let mut default = None;
+ let mut finalizer = None;
+
+ if lookahead.peek(Token![=>]) {
+ input.parse::<Token![=>]>()?;
+ finalizer = Some(input.parse::<Ident>()?);
+
+ let lookahead = input.lookahead1();
+ if !lookahead.peek(Token![,]) {
+ return Err(lookahead.error());
+ }
+ } else if lookahead.peek(Token![=]) {
+ input.parse::<Token![=]>()?;
+ default = Some(input.parse()?);
+
+ let lookahead = input.lookahead1();
+ if lookahead.peek(Token![=>]) {
+ input.parse::<Token![=>]>()?;
+ finalizer = Some(input.parse::<Ident>()?);
+
+ let lookahead = input.lookahead1();
+ if !lookahead.peek(Token![,]) {
+ return Err(lookahead.error());
+ }
+ }
+ } else if lookahead.peek(Token![,]) {
+ // leave the `,` so that the `parse_terminated` can remove it
+ } else {
+ return Err(lookahead.error());
+ }
+
+ Ok(Self::Field {
+ name,
+ ty,
+ default,
+ documentation,
+ finalizer,
+ })
+ }
+ }
+}
+
+impl Parse for ParseBase {
+ fn parse(input: ParseStream<'_>) -> Result<Self> {
+ input.parse::<Token![use]>()?;
+ let content;
+ bracketed!(content in input);
+ let imports = content
+ .parse_terminated(syn::Path::parse, Token![;])?
+ .into_iter()
+ .collect();
+
+ input.parse::<Token![struct]>()?;
+ let name: Ident = input.parse()?;
+
+ let content;
+ braced!(content in input);
+ let fields = content
+ .parse_terminated(ParsePart::parse, Token![,])?
+ .into_iter()
+ .collect();
+
+ Ok(ParseBase {
+ name,
+ fields,
+ imports,
+ })
+ }
+}