diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-02-18 03:41:05 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-02-18 03:41:05 +0100 |
| commit | b39e1a10c731fd0e61a566a0668abc33ae821b49 (patch) | |
| tree | 6e2e48b5e56a2cf4b4f966b6f2e014c446a242a1 /common/object/src/debug.rs | |
| parent | 95606a9deed45ae285c2d4dee01de9d21a43b044 (diff) | |
| download | jellything-b39e1a10c731fd0e61a566a0668abc33ae821b49.tar jellything-b39e1a10c731fd0e61a566a0668abc33ae821b49.tar.bz2 jellything-b39e1a10c731fd0e61a566a0668abc33ae821b49.tar.zst | |
use fourcc as object tags (bad idea); store type info within objects
Diffstat (limited to 'common/object/src/debug.rs')
| -rw-r--r-- | common/object/src/debug.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/common/object/src/debug.rs b/common/object/src/debug.rs new file mode 100644 index 0000000..15812b1 --- /dev/null +++ b/common/object/src/debug.rs @@ -0,0 +1,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() + } +} |