From b39e1a10c731fd0e61a566a0668abc33ae821b49 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 18 Feb 2026 03:41:05 +0100 Subject: use fourcc as object tags (bad idea); store type info within objects --- common/object/src/debug.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 common/object/src/debug.rs (limited to 'common/object/src/debug.rs') 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 +*/ + +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::(i).unwrap()), + (ValueType::UInt, 4) => s.field(k, &self.get_typed::(i).unwrap()), + (ValueType::UInt, 8) => s.field(k, &self.get_typed::(i).unwrap()), + (ValueType::Int, 8) => s.field(k, &self.get_typed::(i).unwrap()), + _ => { + nonexhaustive = true; + &mut s + } + }; + } + if nonexhaustive { + s.finish_non_exhaustive() + } else { + s.finish() + } + } +} + +impl Debug for TypedTag { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple(&format!("TypedTag<{}>", type_name::())) + .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() + } +} -- cgit v1.3