From b1d1f2f627c156eb11a187490aad0ac6cad6dcf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Tue, 5 Nov 2019 11:21:07 +0100 Subject: [PATCH] Implement a better debug system for the schema --- meilidb-schema/src/lib.rs | 85 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/meilidb-schema/src/lib.rs b/meilidb-schema/src/lib.rs index 7486032ba..5ec64f3b6 100644 --- a/meilidb-schema/src/lib.rs +++ b/meilidb-schema/src/lib.rs @@ -22,7 +22,7 @@ pub const RANKED: SchemaProps = SchemaProps { ranked: true, }; -#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SchemaProps { #[serde(default)] pub displayed: bool, @@ -60,6 +60,36 @@ impl BitOr for SchemaProps { } } +impl fmt::Debug for SchemaProps { + #[allow(non_camel_case_types)] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + #[derive(Debug)] + struct DISPLAYED; + + #[derive(Debug)] + struct INDEXED; + + #[derive(Debug)] + struct RANKED; + + let mut debug_set = f.debug_set(); + + if self.displayed { + debug_set.entry(&DISPLAYED); + } + + if self.indexed { + debug_set.entry(&INDEXED); + } + + if self.ranked { + debug_set.entry(&RANKED); + } + + debug_set.finish() + } +} + #[derive(Serialize, Deserialize)] pub struct SchemaBuilder { identifier: String, @@ -102,12 +132,12 @@ impl SchemaBuilder { } } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] pub struct Schema { inner: Arc, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] struct InnerSchema { identifier: String, attrs: HashMap, @@ -184,6 +214,16 @@ impl<'de> Deserialize<'de> for Schema { } } +impl fmt::Debug for Schema { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let builder = self.to_builder(); + f.debug_struct("Schema") + .field("identifier", &builder.identifier) + .field("attributes", &builder.attributes) + .finish() + } +} + #[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)] pub struct SchemaAttr(pub u16); @@ -447,4 +487,43 @@ mod tests { Ok(()) } + + #[test] + fn debug_output() { + use std::fmt::Write as _; + + let mut builder = SchemaBuilder::with_identifier("id"); + builder.new_attribute("alpha", DISPLAYED); + builder.new_attribute("beta", DISPLAYED | INDEXED); + builder.new_attribute("gamma", INDEXED); + let schema = builder.build(); + + let mut output = String::new(); + let _ = write!(&mut output, "{:#?}", schema); + + let expected = r#"Schema { + identifier: "id", + attributes: { + "alpha": { + DISPLAYED, + }, + "beta": { + DISPLAYED, + INDEXED, + }, + "gamma": { + INDEXED, + }, + }, +}"#; + + assert_eq!(output, expected); + + let mut output = String::new(); + let _ = write!(&mut output, "{:?}", schema); + + let expected = r#"Schema { identifier: "id", attributes: {"alpha": {DISPLAYED}, "beta": {DISPLAYED, INDEXED}, "gamma": {INDEXED}} }"#; + + assert_eq!(output, expected); + } }