|
| 1 | +#![warn(clippy::pedantic)] |
| 2 | +use crate::errors::SDKErr; |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +fn parse_file(file_name: &str, content: &mut str) -> Result<syn::File, SDKErr> { |
| 6 | + if let Ok(ast) = syn::parse_file(content) { |
| 7 | + Ok(ast) |
| 8 | + } else { |
| 9 | + Err(SDKErr::AstParseError(file_name.to_string())) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Clone, Default)] |
| 14 | +pub struct Codebase { |
| 15 | + ast_map: HashMap<String, syn::File>, |
| 16 | + free_functions: Vec<Function>, |
| 17 | + contracts: Vec<Contract>, |
| 18 | +} |
| 19 | + |
| 20 | +impl Codebase { |
| 21 | + pub fn new() -> Self { |
| 22 | + Codebase { |
| 23 | + ast_map: HashMap::new(), |
| 24 | + free_functions: Vec::new(), |
| 25 | + contracts: Vec::new(), |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn parse_and_add_file(&mut self, file_name: &str, content: &mut str) -> Result<(), SDKErr> { |
| 30 | + let file = parse_file(file_name, content)?; |
| 31 | + self.ast_map.insert(file_name.to_string(), file); |
| 32 | + Ok(()) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn build_api(&mut self) { |
| 36 | + for (file_name, file) in &self.ast_map { |
| 37 | + for item in &file.items { |
| 38 | + match item { |
| 39 | + syn::Item::Fn(f) => { |
| 40 | + println!("Function: {}", f.sig.ident); |
| 41 | + } |
| 42 | + syn::Item::Struct(s) => { |
| 43 | + println!("Struct: {}", s.ident); |
| 44 | + } |
| 45 | + syn::Item::Enum(e) => { |
| 46 | + println!("Enum: {}", e.ident); |
| 47 | + } |
| 48 | + _ => {} |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Clone)] |
| 56 | +pub struct Contract { |
| 57 | + name: String, |
| 58 | + functions: Vec<Function>, |
| 59 | + structs: Vec<Struct>, |
| 60 | + enums: Vec<Enum>, |
| 61 | +} |
| 62 | + |
| 63 | +impl Contract { |
| 64 | + pub fn new(name: &str) -> Self { |
| 65 | + Contract { |
| 66 | + name: name.to_string(), |
| 67 | + functions: Vec::new(), |
| 68 | + structs: Vec::new(), |
| 69 | + enums: Vec::new(), |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[derive(Clone)] |
| 75 | +pub struct Function {} |
| 76 | + |
| 77 | +impl Function { |
| 78 | + pub fn new() -> Self { |
| 79 | + Function {} |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Clone)] |
| 84 | +pub struct Struct {} |
| 85 | + |
| 86 | +impl Struct { |
| 87 | + pub fn new() -> Self { |
| 88 | + Struct {} |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[derive(Clone)] |
| 93 | +pub struct Enum {} |
| 94 | + |
| 95 | +impl Enum { |
| 96 | + pub fn new() -> Self { |
| 97 | + Enum {} |
| 98 | + } |
| 99 | +} |
0 commit comments