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(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(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
26/// Display something with options.
27pub trait DisplayWithOptions: Display {
28    fn fmt_with_options(&self, f: &mut Formatter<'_>, _options: &Options) -> Result {
29        self.fmt(f)
30    }
31}
32
33pub struct PsyXDisplayable<'a, P: DisplayWithOptions> {
34    p: &'a P,
35    options: Options,
36}
37
38impl<'a, P> PsyXDisplayable<'a, P>
39where
40    P: DisplayWithOptions,
41{
42    pub fn wrap(p: &'a P, options: Options) -> PsyXDisplayable<'a, P> {
43        Self { p, options }
44    }
45}
46
47impl<P> Display for PsyXDisplayable<'_, P>
48where
49    P: DisplayWithOptions,
50{
51    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
52        self.p.fmt_with_options(f, &self.options)
53    }
54}