Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 186e3f1

Browse files
0xSwapFeeder0xmemorygrinder
authored andcommitted
feat(libs/solc-wrapper): added forge-wrapper and ast extraction
1 parent 7d93eb2 commit 186e3f1

File tree

12 files changed

+2758
-0
lines changed

12 files changed

+2758
-0
lines changed

libs/solc-wrapper/Cargo.lock

+202
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/solc-wrapper/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "solc-wrapper"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
serde_json = "1.0.113"
10+
solc-ast-rs-types = "0.1.6"
11+
thiserror = "1.0.56"

libs/solc-wrapper/src/error.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use thiserror::Error;
2+
3+
use crate::forge::error::CommandError;
4+
5+
#[derive(Error, Debug)]
6+
pub enum SolcWrapperError {
7+
#[error("Solc error: {0}")]
8+
Solc(#[from] CommandError),
9+
#[error("JSON parsing error: {0}")]
10+
Json(#[from] serde_json::Error),
11+
#[error("No build info produced by foundry")]
12+
NoBuildInfo,
13+
#[error("Cannot read build info file")]
14+
ReadBuildInfo(#[from] std::io::Error),
15+
#[error("Cannot read source file")]
16+
ReadSourceFile(std::io::Error)
17+
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::process::Command;
2+
use std::process::Stdio;
3+
use std::path::PathBuf;
4+
5+
use super::error::{CommandError, CommandType};
6+
7+
pub struct ForgeCommand {
8+
args: Vec<String>,
9+
bin_path : PathBuf,
10+
current_dir: String
11+
}
12+
13+
impl Default for ForgeCommand {
14+
fn default() -> Self {
15+
ForgeCommand::new("forge")
16+
}
17+
}
18+
19+
impl ForgeCommand {
20+
21+
pub fn new(path: impl Into<PathBuf>) -> Self {
22+
ForgeCommand {
23+
args: Vec::new(),
24+
bin_path: path.into(),
25+
current_dir: String::from(".")
26+
}
27+
}
28+
29+
pub fn current_dir(mut self, current_dir: String) -> Self {
30+
self.current_dir = current_dir;
31+
self
32+
}
33+
34+
pub fn arg<T: Into<String>>(mut self, arg: T) -> Self {
35+
self.args.push(arg.into());
36+
self
37+
}
38+
39+
pub fn args<I, S>(mut self, args: I) -> Self
40+
where
41+
I: IntoIterator<Item = S>,
42+
S: Into<String>,
43+
{
44+
for arg in args {
45+
self = self.arg(arg);
46+
}
47+
self
48+
}
49+
50+
pub fn execute(&self) -> Result<(), CommandError> {
51+
Command::new(&self.bin_path)
52+
.current_dir(&self.current_dir)
53+
.args(&self.args)
54+
.stdout(Stdio::piped())
55+
.output()
56+
.map_err(|e| {
57+
eprintln!("Forge Command Error: {}", e.to_string());
58+
CommandError { error: e.to_string(), command_type: CommandType::ParseFile }
59+
})?;
60+
Ok(())
61+
}
62+
}

libs/solc-wrapper/src/forge/error.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use thiserror::Error;
2+
3+
#[derive(Debug)]
4+
pub enum CommandType {
5+
ParseFile,
6+
ParseStdin,
7+
}
8+
9+
#[derive(Error, Debug)]
10+
pub struct CommandError {
11+
pub command_type: CommandType,
12+
pub error: String,
13+
}
14+
15+
impl std::fmt::Display for CommandError {
16+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
17+
write!(f, "CommandError: {}", self.error)
18+
}
19+
}

libs/solc-wrapper/src/forge/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod error;
2+
pub mod command;
3+
pub mod parsing_error;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use thiserror::Error;
2+
3+
4+
5+
#[derive(Error, Debug)]
6+
pub struct ErrorLocation {
7+
pub file: String,
8+
pub line: usize,
9+
pub column: usize,
10+
pub length: usize
11+
}
12+
13+
#[derive(Error, Debug)]
14+
pub struct ParsingError {
15+
pub error: String,
16+
pub location: ErrorLocation,
17+
}
18+
19+
impl std::fmt::Display for ErrorLocation {
20+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21+
write!(f, "ErrorLocation in {} at line {}, column {}", self.file, self.line, self.column)
22+
}
23+
}
24+
25+
impl std::fmt::Display for ParsingError {
26+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
27+
write!(f, "ParsingError: {}", self.error)
28+
}
29+
}

0 commit comments

Comments
 (0)