syn_args/
macro_args.rs

1use std::{
2    collections::HashMap,
3    fmt::Debug,
4    ops::{Deref, DerefMut},
5};
6
7use quote::ToTokens;
8use syn::Error;
9
10use crate::SynArgs;
11
12/// Contains basic types for syn-args, used for quick parameter parsing
13pub mod def;
14
15/// Contains some auxiliary functions
16pub mod utils;
17
18/// parse the input into Arguments
19pub struct Formal {}
20
21impl Default for Formal {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl Formal {
28    pub fn new() -> Self {
29        Formal {}
30    }
31
32    pub fn parse(&self, input: &str) -> Result<Arguments, Error> {
33        let expr = syn::parse_str::<SynArgs>(input).unwrap();
34        // println!("Formal: {:#?}", expr);
35        Ok(Arguments(expr.value))
36    }
37}
38
39/// Intermediate layer type, usually converted from syn::Expr
40#[derive(Debug, Clone, PartialEq)]
41pub enum Value {
42    Null,
43    Expr(def::Expr),
44    Int(def::Int),
45    Float(def::Float),
46    Bool(def::Bool),
47    String(def::String),
48    Option(def::Option<Box<Value>>),
49    Object(def::Object<Value>),
50    Array(def::Array<Value>),
51}
52
53/// Arguments type, usually converted from Value
54/// This type is used to distinguish top-level types for easier processing, and is ultimately converted to specific types through Arguments.
55#[derive(Debug, Clone, PartialEq)]
56pub struct Arguments(pub Value);
57
58impl Deref for Arguments {
59    type Target = Value;
60
61    fn deref(&self) -> &Self::Target {
62        &self.0
63    }
64}
65
66impl DerefMut for Arguments {
67    fn deref_mut(&mut self) -> &mut Self::Target {
68        &mut self.0
69    }
70}
71
72impl TryFrom<Value> for Arguments {
73    type Error = Error;
74
75    fn try_from(value: Value) -> Result<Self, Self::Error> {
76        Ok(Arguments(value))
77    }
78}