aboutsummaryrefslogtreecommitdiff
path: root/common/object/src/debug.rs
blob: 15812b11182ad1a14c598e9e82b695eb8a3fd05c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::{Object, ObjectBuffer, Tag, TypedTag, ValueType};
use std::{any::type_name, fmt::Debug};

impl Debug for ObjectBuffer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_object().fmt(f)
    }
}

impl Debug for Object<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = f.debug_struct("Object");
        let mut nonexhaustive = false;
        for (i, tag) in self.keys().enumerate() {
            let kbytes = tag.0.to_be_bytes();
            let k = str::from_utf8(&kbytes).unwrap();
            let ty = self.offset_type(i);
            let sz = self.size(i);
            match (ty, sz) {
                (ValueType::String, _) => s.field(k, &self.get_typed::<&str>(i).unwrap()),
                (ValueType::Binary, _) => s.field(k, &self.get_typed::<&[u8]>(i).unwrap()),
                (ValueType::Object, _) => s.field(k, &self.get_typed::<Object>(i).unwrap()),
                (ValueType::UInt, 4) => s.field(k, &self.get_typed::<u32>(i).unwrap()),
                (ValueType::UInt, 8) => s.field(k, &self.get_typed::<u64>(i).unwrap()),
                (ValueType::Int, 8) => s.field(k, &self.get_typed::<i64>(i).unwrap()),
                _ => {
                    nonexhaustive = true;
                    &mut s
                }
            };
        }
        if nonexhaustive {
            s.finish_non_exhaustive()
        } else {
            s.finish()
        }
    }
}

impl<T> Debug for TypedTag<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple(&format!("TypedTag<{}>", type_name::<T>()))
            .field(&self.0)
            .finish()
    }
}
impl Debug for Tag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let bytes = self.0.to_be_bytes();
        let name = str::from_utf8(&bytes).unwrap();
        f.debug_tuple("Tag").field(&name).finish()
    }
}