1use std::default::Default;
5use std::fmt::{Display, Formatter, Result};
6
7#[derive(Clone, Default)]
9pub enum CodeFormat {
10 #[default]
11 None,
12 Hex,
13 Disassembly,
14}
15
16#[derive(Clone, Default)]
18pub struct Options {
19 pub code_format: CodeFormat,
21
22 pub recursive: bool,
24
25 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
41pub 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}