This commit is contained in:
2026-08-22 23:31:14 -04:00
parent 631bb539f1
commit e28cfd1c53
7 changed files with 234 additions and 189 deletions
+21 -33
View File
@@ -53,47 +53,35 @@ impl LanguageCondition {
}
}
pub fn filter(conditions: impl IntoIterator<Item = (String, Self)>) -> String {
let exact_expression = |variable: String, language| Expression::FunctionCall(
pub fn filter(&self, variable: impl Into<String>) -> String {
let variable = variable.into();
let exact = |language: &LanguageTag<String>| Expression::FunctionCall(
Function::LangMatches, vec![
Expression::FunctionCall(Function::Lang, vec![
Expression::Variable(Variable::new_unchecked(variable))
Expression::Variable(Variable::new_unchecked(&variable))
]),
Expression::Literal(Literal::new_simple_literal(language))
Expression::Literal(Literal::new_simple_literal(language.to_string()))
],
);
let untagged_expression = |variable| Expression::Not(
let untagged = Expression::Not(
Box::new(Expression::FunctionCall(Function::HasLang, vec![
Expression::Variable(Variable::new_unchecked(variable))
Expression::Variable(Variable::new_unchecked(&variable))
]))
);
let condition_to_expression = |variable, condition| match condition {
LanguageCondition::ExactMatchOnly(language) =>
Some(exact_expression(variable, language.to_string())),
LanguageCondition::ExactMatchOrUntagged(language) => {
Some(Expression::Or(
Box::new(exact_expression(variable.clone(), language.to_string())),
Box::new(untagged_expression(variable)),
))
}
LanguageCondition::UntaggedOnly => Some(untagged_expression(variable)),
LanguageCondition::AnyOrNone => None,
};
let mut iter = conditions.into_iter();
let first_expression = iter.next()
.and_then(|(variable, condition)| condition_to_expression(variable, condition));
if let Some(expression) = first_expression {
let expr = iter.filter_map(|(variable, condition)| condition_to_expression(variable, condition))
.fold(expression, |acc, next_exp| Expression::Or(Box::new(acc), Box::new(next_exp)));
GraphPattern::Filter {
expr,
inner: Box::new(GraphPattern::Bgp { patterns: vec![] }),
}.to_string()
} else {
String::default()
}
match self {
Self::ExactMatchOnly(language) => Some(exact(language)),
Self::ExactMatchOrUntagged(language) => Some(Expression::Or(
Box::new(exact(language)),
Box::new(untagged),
)),
Self::UntaggedOnly => Some(untagged),
Self::AnyOrNone => None,
}.map(|expr| GraphPattern::Filter {
expr,
inner: Box::new(GraphPattern::Bgp { patterns: vec![] }),
}.to_string()).unwrap_or_default()
}
}