mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-26 12:05:05 +08:00
Merge pull request #258 from meilisearch/debug-schema
Implement a better debug for the schema
This commit is contained in:
commit
c4087e2ec2
@ -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<InnerSchema>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
struct InnerSchema {
|
||||
identifier: String,
|
||||
attrs: HashMap<String, SchemaAttr>,
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user