Skip to content

Commit bab2ee1

Browse files
committed
Update dependencies and refactor error handling with SDKErr enum; begin writing Codebase structure for AST parsing
1 parent 430865e commit bab2ee1

File tree

4 files changed

+128
-11
lines changed

4 files changed

+128
-11
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
syn = "2.0.86"
7+
syn = { version = "2.0.87", features = ["full"] }
8+
thiserror = "1.0.67"
89

910
[dev-dependencies]
1011
soroban-sdk = "21.7.6"

src/ast.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

src/errors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![warn(clippy::pedantic)]
2+
use thiserror::Error;
3+
4+
#[derive(Error, Debug)]
5+
#[non_exhaustive]
6+
pub enum SDKErr {
7+
#[error("source file not found: {0}")]
8+
SrcFileNotFound(String),
9+
#[error("failed to read file: {0}")]
10+
IOError(#[from] std::io::Error),
11+
#[error("failed to parse ast for file: {0}")]
12+
AstParseError(String),
13+
}

src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
#![warn(clippy::pedantic)]
2+
use ast::Codebase;
3+
use errors::SDKErr;
24
use std::path::Path;
35

4-
#[derive(Debug)]
5-
#[non_exhaustive]
6-
pub enum SDKErr {
7-
SrcFileNotFound(String),
8-
}
9-
6+
mod ast;
7+
pub mod errors;
108
/// Build the code model from the given files.
119
/// # Errors
12-
/// - If the file is not found.
13-
pub fn build_code_model(files: Vec<String>) -> Result<bool, SDKErr> {
10+
/// - `SDKErr::SrcFileNotFound` If the file is not found.
11+
/// - `std::io::Error` If there is an error reading the file.
12+
/// - `SDKErr::AstParseError` If there is an error parsing the AST.
13+
pub fn build_code_model(files: Vec<String>) -> Result<Box<Codebase>, SDKErr> {
14+
let mut codebasae = Box::new(Codebase::default());
1415
for file in files {
1516
let path = Path::new(&file);
1617
if !path.exists() {
17-
return Err(SDKErr::SrcFileNotFound(format!("file not found: {file}")));
18+
return Err(SDKErr::SrcFileNotFound(file));
1819
}
20+
let mut content = std::fs::read_to_string(path)?;
21+
codebasae.parse_and_add_file(&file, &mut content)?;
1922
}
20-
Ok(true)
23+
codebasae.build_api();
24+
Ok(codebasae)
2125
}
2226

2327
#[cfg(test)]

0 commit comments

Comments
 (0)