srdf/
shacl_path.rs

1use iri_s::IriS;
2use serde::Serialize;
3use std::fmt::Display;
4
5/// SHACL paths follow the [SHACL property paths spec](https://www.w3.org/TR/shacl/#property-paths)
6/// which are a subset of SPARQL property paths
7///
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
9pub enum SHACLPath {
10    Predicate { pred: IriS },
11    Alternative { paths: Vec<SHACLPath> },
12    Sequence { paths: Vec<SHACLPath> },
13    Inverse { path: Box<SHACLPath> },
14    ZeroOrMore { path: Box<SHACLPath> },
15    OneOrMore { path: Box<SHACLPath> },
16    ZeroOrOne { path: Box<SHACLPath> },
17}
18
19impl SHACLPath {
20    pub fn iri(pred: IriS) -> Self {
21        SHACLPath::Predicate { pred }
22    }
23
24    pub fn pred(&self) -> Option<&IriS> {
25        match self {
26            SHACLPath::Predicate { pred } => Some(pred),
27            _ => None,
28        }
29    }
30}
31
32impl Display for SHACLPath {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            SHACLPath::Predicate { pred } => write!(f, "{pred}"),
36            SHACLPath::Alternative { .. } => todo!(),
37            SHACLPath::Sequence { .. } => todo!(),
38            SHACLPath::Inverse { .. } => todo!(),
39            SHACLPath::ZeroOrMore { .. } => todo!(),
40            SHACLPath::OneOrMore { .. } => todo!(),
41            SHACLPath::ZeroOrOne { .. } => todo!(),
42        }
43    }
44}
45
46// impl From<SHACLPath> for &str {
47//     fn from(value: SHACLPath) -> Self {
48//         match value {
49//             SHACLPath::Predicate { .. } => todo!(),
50//             SHACLPath::Alternative { .. } => todo!(),
51//             SHACLPath::Sequence { .. } => todo!(),
52//             SHACLPath::Inverse { .. } => todo!(),
53//             SHACLPath::ZeroOrMore { .. } => todo!(),
54//             SHACLPath::OneOrMore { .. } => todo!(),
55//             SHACLPath::ZeroOrOne { .. } => todo!(),
56//         }
57//     }
58// }