Tag UserInputAst

This commit is contained in:
Remi Dettai
2025-04-03 10:07:34 +02:00
parent 6f77083493
commit fb12b7be28
2 changed files with 62 additions and 25 deletions

View File

@@ -367,6 +367,9 @@ mod tests {
message: "test error message".to_string(), message: "test error message".to_string(),
}; };
assert_eq!(serde_json::to_string(&error).unwrap(), "{\"pos\":42,\"message\":\"test error message\"}"); assert_eq!(
serde_json::to_string(&error).unwrap(),
"{\"pos\":42,\"message\":\"test error message\"}"
);
} }
} }

View File

@@ -196,14 +196,39 @@ impl UserInputBound {
} }
#[derive(PartialEq, Clone, Serialize)] #[derive(PartialEq, Clone, Serialize)]
#[serde(rename_all = "snake_case")] #[serde(into = "UserInputAstSerde")]
pub enum UserInputAst { pub enum UserInputAst {
Clause(Vec<(Option<Occur>, UserInputAst)>), Clause(Vec<(Option<Occur>, UserInputAst)>),
Boost(Box<UserInputAst>, f64), Boost(Box<UserInputAst>, f64),
Leaf(Box<UserInputLeaf>),
}
#[derive(Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum UserInputAstSerde {
Clause {
clause: Vec<(Option<Occur>, UserInputAst)>,
},
Boost {
underlying: Box<UserInputAst>,
boost: f64,
},
#[serde(untagged)] #[serde(untagged)]
Leaf(Box<UserInputLeaf>), Leaf(Box<UserInputLeaf>),
} }
impl From<UserInputAst> for UserInputAstSerde {
fn from(ast: UserInputAst) -> Self {
match ast {
UserInputAst::Clause(clause) => UserInputAstSerde::Clause { clause },
UserInputAst::Boost(underlying, boost) => {
UserInputAstSerde::Boost { underlying, boost }
}
UserInputAst::Leaf(leaf) => UserInputAstSerde::Leaf(leaf),
}
}
}
impl UserInputAst { impl UserInputAst {
#[must_use] #[must_use]
pub fn unary(self, occur: Occur) -> UserInputAst { pub fn unary(self, occur: Occur) -> UserInputAst {
@@ -357,14 +382,11 @@ mod tests {
#[test] #[test]
fn test_boost_serialization() { fn test_boost_serialization() {
let inner_ast = UserInputAst::Leaf(Box::new(UserInputLeaf::All)); let inner_ast = UserInputAst::Leaf(Box::new(UserInputLeaf::All));
let boost_ast = UserInputAst::Boost( let boost_ast = UserInputAst::Boost(Box::new(inner_ast), 2.5);
Box::new(inner_ast),
2.5,
);
let json = serde_json::to_string(&boost_ast).unwrap(); let json = serde_json::to_string(&boost_ast).unwrap();
assert_eq!( assert_eq!(
json, json,
r#"{"boost":[{"type":"all"},2.5]}"# r#"{"type":"boost","underlying":{"type":"all"},"boost":2.5}"#
); );
} }
@@ -372,40 +394,52 @@ mod tests {
fn test_boost_serialization2() { fn test_boost_serialization2() {
let boost_ast = UserInputAst::Boost( let boost_ast = UserInputAst::Boost(
Box::new(UserInputAst::Clause(vec![ Box::new(UserInputAst::Clause(vec![
(Some(Occur::Must), UserInputAst::Leaf(Box::new(UserInputLeaf::All))), (
(Some(Occur::Should), UserInputAst::Leaf(Box::new(UserInputLeaf::Literal(UserInputLiteral { Some(Occur::Must),
field_name: Some("title".to_string()), UserInputAst::Leaf(Box::new(UserInputLeaf::All)),
phrase: "hello".to_string(), ),
delimiter: Delimiter::None, (
slop: 0, Some(Occur::Should),
prefix: false, UserInputAst::Leaf(Box::new(UserInputLeaf::Literal(UserInputLiteral {
})))) field_name: Some("title".to_string()),
phrase: "hello".to_string(),
delimiter: Delimiter::None,
slop: 0,
prefix: false,
}))),
),
])), ])),
2.5, 2.5,
); );
let json = serde_json::to_string(&boost_ast).unwrap(); let json = serde_json::to_string(&boost_ast).unwrap();
assert_eq!( assert_eq!(
json, json,
r#"{"boost":[{"clause":[["must",{"type":"all"}],["should",{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}]]},2.5]}"# r#"{"type":"boost","underlying":{"type":"clause","clause":[["must",{"type":"all"}],["should",{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}]]},"boost":2.5}"#
); );
} }
#[test] #[test]
fn test_clause_serialization() { fn test_clause_serialization() {
let clause = UserInputAst::Clause(vec![ let clause = UserInputAst::Clause(vec![
(Some(Occur::Must), UserInputAst::Leaf(Box::new(UserInputLeaf::All))), (
(Some(Occur::Should), UserInputAst::Leaf(Box::new(UserInputLeaf::Literal(UserInputLiteral { Some(Occur::Must),
field_name: Some("title".to_string()), UserInputAst::Leaf(Box::new(UserInputLeaf::All)),
phrase: "hello".to_string(), ),
delimiter: Delimiter::None, (
slop: 0, Some(Occur::Should),
prefix: false, UserInputAst::Leaf(Box::new(UserInputLeaf::Literal(UserInputLiteral {
})))) field_name: Some("title".to_string()),
phrase: "hello".to_string(),
delimiter: Delimiter::None,
slop: 0,
prefix: false,
}))),
),
]); ]);
let json = serde_json::to_string(&clause).unwrap(); let json = serde_json::to_string(&clause).unwrap();
assert_eq!( assert_eq!(
json, json,
r#"{"clause":[["must",{"type":"all"}],["should",{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}]]}"# r#"{"type":"clause","clause":[["must",{"type":"all"}],["should",{"type":"literal","field_name":"title","phrase":"hello","delimiter":"none","slop":0,"prefix":false}]]}"#
); );
} }
} }