Skip to main content

ferro_babe/format/
mod.rs

1//! Text-formatting APIs for complete [`Class`](crate::Class) models.
2//!
3//! A [`Formatter`] may stream to any [`std::fmt::Write`] destination. Its provided
4//! [`Formatter::format`] method collects that output into a [`String`].
5
6mod ferro;
7mod opcode;
8
9use std::fmt;
10
11use crate::{Class, FerroBabeError};
12
13pub use ferro::FerroFormatter;
14
15/// Renders a complete class model into text.
16///
17/// Implement this trait when a consumer needs a presentation other than the compact
18/// reverse-engineering output produced by [`FerroFormatter`]. Implementations receive only a
19/// complete [`Class`]; partial disassemblies must be handled by the caller.
20pub trait Formatter {
21    /// Writes `class` to `output`.
22    ///
23    /// # Errors
24    ///
25    /// Returns [`std::fmt::Error`] when `output` rejects a write.
26    fn write_class(&self, class: &Class, output: &mut dyn fmt::Write) -> fmt::Result;
27
28    /// Renders `class` into a newly allocated string.
29    ///
30    /// # Errors
31    ///
32    /// Returns [`FerroBabeError::Format`] when [`Self::write_class`] cannot write to the string
33    /// destination.
34    fn format(&self, class: &Class) -> Result<String, FerroBabeError> {
35        let mut output = String::new();
36        self.write_class(class, &mut output)
37            .map_err(|source| FerroBabeError::Format { source })?;
38        Ok(output)
39    }
40}