Skip to content

Commit

Permalink
restructure tool
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed May 2, 2024
1 parent f42d425 commit c3c2541
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 73 deletions.
3 changes: 2 additions & 1 deletion lib/athena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use speedy_uuid::Uuid;
use std::{
any::{Any, TypeId},
ptr,
sync::Arc,
};
use typed_builder::TypedBuilder;
Expand Down Expand Up @@ -56,7 +57,7 @@ where
if obj.type_id() == TypeId::of::<T>() {
#[allow(unsafe_code)]
// SAFETY: the `TypeId` equality check ensures this type cast is correct
Some(unsafe { &*(obj as *const dyn Keepable).cast::<T>() })
Some(unsafe { &*ptr::from_ref::<dyn Keepable>(obj).cast::<T>() })
} else {
None
}
Expand Down
88 changes: 88 additions & 0 deletions lib/mrf-tool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use self::args::{ManifestSubcommand, ModuleSubcommand, ToolArgs, ToolSubcommand};
use clap::Parser;
use color_eyre::{eyre::bail, Result};
use std::{
ffi::OsString,
fs::{self, File},
io::Write,
path::Path,
};

mod args;

pub fn read_manifest<W>(sink: &mut W, module: &[u8]) -> Result<()>
where
W: Write,
{
let Some((manifest, _section_range)) = mrf_manifest::decode(module)? else {
bail!("missing manifest in module");

Check warning on line 18 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L13-L18

Added lines #L13 - L18 were not covered by tests
};

let prettified = serde_json::to_string_pretty(&manifest)?;
writeln!(sink, "{prettified}")?;

Check warning on line 22 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L21-L22

Added lines #L21 - L22 were not covered by tests

Ok(())
}

Check warning on line 25 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L24-L25

Added lines #L24 - L25 were not covered by tests

pub fn remove_manifest(module_path: &Path, output_path: &Path) -> Result<()> {
let module = fs::read(module_path)?;
let Some((_manifest, section_range)) = mrf_manifest::decode(&module)? else {
bail!("missing manifest in module");

Check warning on line 30 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L27-L30

Added lines #L27 - L30 were not covered by tests
};

let mut module_file = File::options()
.create(true)
.truncate(true)
.write(true)
.open(output_path)?;

Check warning on line 37 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L33-L37

Added lines #L33 - L37 were not covered by tests

module_file.write_all(&module[..section_range.start])?;
module_file.write_all(&module[section_range.end..])?;

Check warning on line 40 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L39-L40

Added lines #L39 - L40 were not covered by tests

Ok(())
}

Check warning on line 43 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L42-L43

Added lines #L42 - L43 were not covered by tests

pub fn write_manifest(manifest: &[u8], module_path: &Path) -> Result<()> {

Check warning on line 45 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L45

Added line #L45 was not covered by tests
// Parse the manifest and re-encode it in canonical JSON
let parsed_manifest = serde_json::from_slice(manifest)?;
let custom_section = mrf_manifest::encode(&parsed_manifest)?;

Check warning on line 48 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L47-L48

Added lines #L47 - L48 were not covered by tests

let mut file = File::options().append(true).open(module_path)?;
file.write_all(&custom_section)?;

Check warning on line 51 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L50-L51

Added lines #L50 - L51 were not covered by tests

Ok(())
}

Check warning on line 54 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L53-L54

Added lines #L53 - L54 were not covered by tests

pub fn handle<I, W>(sink: &mut W, input: I) -> Result<()>
where
I: IntoIterator,
<I as IntoIterator>::Item: Into<OsString> + Clone,
W: Write,
{
let args = ToolArgs::try_parse_from(input)?;
match args.command {
ToolSubcommand::Manifest(ManifestSubcommand::Add(args)) => {
let manifest = fs::read(args.manifest_path)?;

Check warning on line 65 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L56-L65

Added lines #L56 - L65 were not covered by tests

// Only copy if the paths are distinct
if args.module_path != args.output {
fs::copy(&args.module_path, &args.output)?;
}

Check warning on line 70 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L68-L70

Added lines #L68 - L70 were not covered by tests

write_manifest(&manifest, &args.output)?;

Check warning on line 72 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L72

Added line #L72 was not covered by tests
}
ToolSubcommand::Manifest(ManifestSubcommand::Read(args)) => {
let data = fs::read(args.module_path)?;
read_manifest(sink, &data)?;

Check warning on line 76 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L74-L76

Added lines #L74 - L76 were not covered by tests
}
ToolSubcommand::Manifest(ManifestSubcommand::Remove(args)) => {
remove_manifest(&args.module_path, &args.output)?;

Check warning on line 79 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L78-L79

Added lines #L78 - L79 were not covered by tests
}
ToolSubcommand::Module(ModuleSubcommand::Validate(args)) => {
let data = fs::read(args.module_path)?;
wasmparser::validate(&data)?;

Check warning on line 83 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L81-L83

Added lines #L81 - L83 were not covered by tests
}
}

Ok(())
}

Check warning on line 88 in lib/mrf-tool/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/lib.rs#L87-L88

Added lines #L87 - L88 were not covered by tests
80 changes: 8 additions & 72 deletions lib/mrf-tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,78 +1,14 @@
use self::args::{ManifestSubcommand, ModuleSubcommand, ToolArgs, ToolSubcommand};
use clap::Parser;
use color_eyre::{eyre::bail, Result};
use std::{
fs::{self, File},
io::Write,
path::Path,
};

mod args;

fn read_manifest(module: &[u8]) -> Result<()> {
let Some((manifest, _section_range)) = mrf_manifest::decode(module)? else {
bail!("missing manifest in module");
};

let prettified = serde_json::to_string_pretty(&manifest)?;
println!("{prettified}");

Ok(())
}

fn remove_manifest(module_path: &Path, output_path: &Path) -> Result<()> {
let module = fs::read(module_path)?;
let Some((_manifest, section_range)) = mrf_manifest::decode(&module)? else {
bail!("missing manifest in module");
};

let mut module_file = File::options()
.create(true)
.truncate(true)
.write(true)
.open(output_path)?;

module_file.write_all(&module[..section_range.start])?;
module_file.write_all(&module[section_range.end..])?;

Ok(())
}

fn write_manifest(manifest: &[u8], module_path: &Path) -> Result<()> {
// Parse the manifest and re-encode it in canonical JSON
let parsed_manifest = serde_json::from_slice(manifest)?;
let custom_section = mrf_manifest::encode(&parsed_manifest)?;

let mut file = File::options().append(true).open(module_path)?;
file.write_all(&custom_section)?;

Ok(())
}
use color_eyre::Result;
use std::{env, io};

fn main() -> Result<()> {
let args = ToolArgs::parse();
match args.command {
ToolSubcommand::Manifest(ManifestSubcommand::Add(args)) => {
let manifest = fs::read(args.manifest_path)?;

// Only copy if the paths are distinct
if args.module_path != args.output {
fs::copy(&args.module_path, &args.output)?;
}

write_manifest(&manifest, &args.output)?;
}
ToolSubcommand::Manifest(ManifestSubcommand::Read(args)) => {
let data = fs::read(args.module_path)?;
read_manifest(&data)?;
}
ToolSubcommand::Manifest(ManifestSubcommand::Remove(args)) => {
remove_manifest(&args.module_path, &args.output)?;
}
ToolSubcommand::Module(ModuleSubcommand::Validate(args)) => {
let data = fs::read(args.module_path)?;
wasmparser::validate(&data)?;
let stdout = io::stdout();
if let Err(error) = mrf_tool::handle(&mut stdout.lock(), env::args_os()) {
if let Some(error) = error.downcast_ref::<clap::Error>() {
error.exit();

Check warning on line 8 in lib/mrf-tool/src/main.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/main.rs#L5-L8

Added lines #L5 - L8 were not covered by tests
}

return Err(error);

Check warning on line 11 in lib/mrf-tool/src/main.rs

View check run for this annotation

Codecov / codecov/patch

lib/mrf-tool/src/main.rs#L10-L11

Added lines #L10 - L11 were not covered by tests
}

Ok(())
Expand Down

0 comments on commit c3c2541

Please sign in to comment.