Skip to content

Commit

Permalink
Ingest asm from string
Browse files Browse the repository at this point in the history
  • Loading branch information
gzanitti committed Apr 20, 2024
1 parent 3ef9944 commit a590318
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion etk-asm/src/bin/eas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ fn run() -> Result<(), Error> {
let hex_out = HexWrite::new(&mut out);

let mut ingest = Ingest::new(hex_out, hardfork);
ingest.ingest_file(opt.input)?;

let ingest_str = match opt.asm {

Check failure on line 61 in etk-asm/src/bin/eas.rs

View workflow job for this annotation

GitHub Actions / Test Suite

no field `asm` on type `Opt`

Check failure on line 61 in etk-asm/src/bin/eas.rs

View workflow job for this annotation

GitHub Actions / clippy

no field `asm` on type `Opt`

error[E0609]: no field `asm` on type `Opt` --> etk-asm/src/bin/eas.rs:61:32 | 61 | let ingest_str = match opt.asm { | ^^^ unknown field | = note: available fields are: `input`, `out`, `hardfork`
Some(str) => ingest.ingest_str(str)?,
None => ingest.ingest_file(opt.input)?,
};

out.write_all(b"\n").unwrap();

Expand Down
62 changes: 62 additions & 0 deletions etk-asm/src/ingest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ where
Ok(())
}

/// Assembly instructions from `src` as if they were read from a string
pub fn ingest_str(&mut self, src: &str) -> Result<(), Error> {
self.ingest(PathBuf::from(""), src)
}

/// Assemble instructions from `src` as if they were read from a file located
/// at `path`.
pub fn ingest<P>(&mut self, path: P, text: &str) -> Result<(), Error>
Expand Down Expand Up @@ -472,6 +477,63 @@ mod tests {
Ok(())
}

#[test]
fn ingest_str() -> Result<(), Error> {
let text = r#"
push1 1
push1 2
"#;

let mut output = Vec::new();
let mut ingest = Ingest::new(&mut output, HardFork::Cancun);
ingest.ingest_str(text)?;
assert_eq!(output, hex!("60016002"));

Ok(())
}

#[test]
fn ingest_str_include() -> Result<(), Error> {
let (f, _root) = new_file("push1 42");

let text = format!(
r#"
push1 1
%include("{}")
push1 2
"#,
f.path().display()
);

let mut output = Vec::new();
let mut ingest = Ingest::new(&mut output, HardFork::Cancun);
ingest.ingest_str(&text)?;
assert_eq!(output, hex!("6001602a6002"));

Ok(())
}

#[test]
fn ingest_str_import() -> Result<(), Error> {
let (f, _root) = new_file("push1 42");

let text = format!(
r#"
push1 1
%import("{}")
push1 2
"#,
f.path().display()
);

let mut output = Vec::new();
let mut ingest = Ingest::new(&mut output, HardFork::Cancun);
ingest.ingest_str(&text)?;
assert_eq!(output, hex!("6001602a6002"));

Ok(())
}

#[test]
fn ingest_import_twice() {
let (f, root) = new_file(
Expand Down

0 comments on commit a590318

Please sign in to comment.