Skip to main content

Formatter

Trait Formatter 

Source
pub trait Formatter {
    // Required method
    fn write_class(&self, class: &Class, output: &mut dyn Write) -> Result;

    // Provided method
    fn format(&self, class: &Class) -> Result<String, FerroBabeError> { ... }
}
Expand description

Renders a complete class model into text.

Implement this trait when a consumer needs a presentation other than the compact reverse-engineering output produced by FerroFormatter. Implementations receive only a complete Class; partial disassemblies must be handled by the caller.

Required Methods§

Source

fn write_class(&self, class: &Class, output: &mut dyn Write) -> Result

Writes class to output.

§Errors

Returns std::fmt::Error when output rejects a write.

Provided Methods§

Source

fn format(&self, class: &Class) -> Result<String, FerroBabeError>

Renders class into a newly allocated string.

§Errors

Returns FerroBabeError::Format when Self::write_class cannot write to the string destination.

Examples found in repository?
examples/disassemble.rs (line 39)
17fn run() -> Result<ExitCode, String> {
18    let path = class_path()?;
19    let file =
20        File::open(&path).map_err(|error| format!("could not open {}: {error}", path.display()))?;
21    let disassembly = Disassembler::default()
22        .parse_reader(file)
23        .map_err(|error| format!("could not parse {}: {error}", path.display()))?;
24
25    let version = disassembly.version();
26    let Some(class) = disassembly.class() else {
27        println!(
28            "partial class file v{}.{}",
29            version.major(),
30            version.minor()
31        );
32        for diagnostic in disassembly.diagnostics() {
33            print_diagnostic(diagnostic);
34        }
35        return Ok(ExitCode::from(2));
36    };
37
38    let output = FerroFormatter
39        .format(class)
40        .map_err(|error| format!("could not format {}: {error}", path.display()))?;
41    print!("{output}");
42
43    Ok(ExitCode::SUCCESS)
44}

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§