massbit_sol/parser/
schema_builder.rs

1use crate::schema::Schema;
2use std::path::Path;
3use std::{fs, io};
4use syn::{Attribute, Item, ItemEnum, ItemUse, Variant};
5
6pub struct SchemaBuilder<'a> {
7    pub instruction_path: &'a str,
8    pub output_dir: &'a str,
9}
10impl<'a> SchemaBuilder<'a> {
11    fn default() -> Self {
12        Self {
13            instruction_path: "",
14            output_dir: "",
15        }
16    }
17    pub fn builder() -> SchemaBuilder<'a> {
18        SchemaBuilder::default()
19    }
20    pub fn with_instruction_path(mut self, path: &'a str) -> Self {
21        self.instruction_path = path;
22        self
23    }
24    pub fn with_output_dir(mut self, output_dir: &'a str) -> Self {
25        self.output_dir = output_dir;
26        self
27    }
28    pub fn build(&self) {
29        let input_content = fs::read_to_string(self.instruction_path).expect(
30            format!(
31                "Something went wrong reading the file {}",
32                &self.instruction_path
33            )
34            .as_str(),
35        );
36        // let mut file = File::open(INPUT_RUST_CODE)?;
37        // let mut content = String::new();
38        // file.read_to_string(&mut content)?;
39
40        match syn::parse_file(&input_content) {
41            Ok(ast) => {
42                let schema = Schema::default();
43                if let Some(shebang) = ast.shebang {
44                    println!("{}", shebang);
45                }
46                ast.items.iter().for_each(|item| self.process_item(item));
47                ast.attrs.iter().for_each(|item| println!("{:?}", item));
48            }
49            Err(_) => {}
50        };
51    }
52    fn process_item(&self, item: &Item) {
53        match item {
54            Item::Const(_) => {}
55            Item::Enum(item_enum) => {
56                self.process_item_enum(item_enum);
57            }
58            Item::ExternCrate(_) => {}
59            Item::Fn(_) => {}
60            Item::ForeignMod(_) => {}
61            Item::Impl(_) => {}
62            Item::Macro(_) => {}
63            Item::Macro2(_) => {}
64            Item::Mod(_) => {}
65            Item::Static(_) => {}
66            Item::Struct(_) => {}
67            Item::Trait(_) => {}
68            Item::TraitAlias(_) => {}
69            Item::Type(_) => {}
70            Item::Union(_) => {}
71            Item::Use(item_use) => {
72                self.process_item_use(item_use);
73            }
74            Item::Verbatim(_) => {}
75            Item::__TestExhaustive(_) => {}
76        }
77        //println!("{:?}", item)
78    }
79    fn process_item_enum(&self, item_enum: &ItemEnum) {
80        println!("Variant number: {}", item_enum.variants.len());
81        item_enum
82            .variants
83            .iter()
84            .for_each(|variant| self.process_item_variant(variant));
85    }
86    fn process_item_variant(&self, variant: &Variant) {
87        println!("Variant attrs {:?}", &variant.attrs);
88        println!("Variant ident {:?}", &variant.ident);
89        println!("Variant fields {:?}", &variant.fields);
90        println!("Variant discriminant {:?}", &variant.discriminant);
91    }
92    fn process_attribute(&self, attribute: &Attribute) {
93        //println!("{:?}", attribute)
94    }
95    fn process_item_use(&self, item_use: &ItemUse) {}
96    pub fn write_to_file<P: ?Sized + AsRef<Path>>(
97        &self,
98        output_path: &P,
99        content: &String,
100    ) -> io::Result<()> {
101        match fs::write(output_path, content) {
102            Ok(_) => {
103                log::info!(
104                    "Write content to file {:?} successfully",
105                    &output_path.as_ref().as_os_str()
106                );
107                Ok(())
108            }
109            e @ Err(_) => {
110                log::info!(
111                    "Write content to file {:?} fail. {:?}",
112                    &output_path.as_ref().as_os_str(),
113                    &e
114                );
115                e
116            }
117        }
118    }
119}