shex_ast/ir/
object_value.rs

1use std::fmt::Display;
2
3use iri_s::IriS;
4use srdf::{literal::Literal, Object};
5
6#[derive(PartialEq, Eq, Clone, Debug)]
7pub enum ObjectValue {
8    IriRef(IriS),
9    ObjectLiteral(Literal),
10}
11
12impl ObjectValue {
13    pub(crate) fn match_value(&self, object: &Object) -> bool {
14        match self {
15            ObjectValue::IriRef(iri_expected) => match object {
16                Object::Iri(iri) => iri == iri_expected,
17                _ => false,
18            },
19            ObjectValue::ObjectLiteral(literal_expected) => match object {
20                Object::Literal(lit) => {
21                    // We compare lexical forms and datatypes because some parsed literals are not optimized as primitive literals (like integers)
22                    literal_expected.datatype() == lit.datatype()
23                        && literal_expected.lexical_form() == lit.lexical_form()
24                }
25                _ => false,
26            },
27        }
28    }
29}
30
31impl Display for ObjectValue {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            ObjectValue::IriRef(iri) => {
35                write!(f, "{iri}")?;
36                Ok(())
37            }
38            ObjectValue::ObjectLiteral(lit) => {
39                write!(f, "{lit}")
40            }
41        }
42    }
43}