psyk/
display.rs

1// SPDX-FileCopyrightText: © 2025 TTKB, LLC
2// SPDX-License-Identifier: BSD-3-CLAUSE
3
4use std::default::Default;
5use std::fmt::{Display, Formatter, Result};
6
7/// The format used to display code.
8#[derive(Clone, Default)]
9pub enum CodeFormat {
10    #[default]
11    None,
12    Hex,
13    Disassembly,
14}
15
16/// Options for displaying [LIB](super::LIB) and [OBJ](super::OBJ) data.
17#[derive(Clone, Default)]
18pub struct Options {
19    /// The code format to emit
20    pub code_format: CodeFormat,
21
22    /// Whether or not to recurse into each module of a [LIB](super::LIB)
23    pub recursive: bool,
24
25    /// Level to indent
26    pub indent_level: u8,
27}
28
29impl Options {
30    pub fn indent(&self) -> Self {
31        let mut o = self.clone();
32        o.indent_level += 1;
33        o
34    }
35
36    pub fn write_indent(&self, f: &mut Formatter) -> Result {
37        write!(f, "{:width$}", "", width = 4 * (self.indent_level as usize))
38    }
39}
40
41/// Display something with options.
42pub trait DisplayWithOptions: Display {
43    fn fmt_with_options(&self, f: &mut Formatter<'_>, _options: &Options) -> Result {
44        self.fmt(f)
45    }
46}
47
48pub struct PsyXDisplayable<'a, P: DisplayWithOptions> {
49    p: &'a P,
50    options: Options,
51}
52
53impl<'a, P> PsyXDisplayable<'a, P>
54where
55    P: DisplayWithOptions,
56{
57    pub fn wrap(p: &'a P, options: Options) -> PsyXDisplayable<'a, P> {
58        Self { p, options }
59    }
60}
61
62impl<P> Display for PsyXDisplayable<'_, P>
63where
64    P: DisplayWithOptions,
65{
66    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
67        self.p.fmt_with_options(f, &self.options)
68    }
69}