Skip to main content

rdfx/term/generator/
interpretation.rs

1use std::{borrow::Cow, cell::RefCell};
2
3use iri_rs::{Iri, IriBuf};
4
5use crate::{
6    BlankId,
7    CowLiteral,
8    Interpretation,
9    InterpretationMut,
10    LiteralRef,
11    LocalTerm,
12    interpretation::{ConstGenerativeInterpretation, LocalInterpretation, ReverseInterpretation, ReverseLocalInterpretation},
13};
14
15use super::LocalGenerator;
16
17pub struct LocalGeneratorInterpretation<G>(RefCell<G>);
18
19impl<G> LocalGeneratorInterpretation<G> {
20    pub const fn new(generator: G) -> Self {
21        Self(RefCell::new(generator))
22    }
23
24    pub fn into_generator(self) -> G {
25        self.0.into_inner()
26    }
27}
28
29impl<G> Interpretation for LocalGeneratorInterpretation<G> {
30    type Resource = LocalTerm;
31
32    fn iri(&self, iri: Iri<&str>) -> Option<Self::Resource> {
33        Some(IriBuf::from(iri).into())
34    }
35
36    fn literal<'a>(&self, literal: impl Into<LiteralRef<'a>>) -> Option<Self::Resource> {
37        Some(literal.into().to_owned().into())
38    }
39}
40
41impl<G> LocalInterpretation for LocalGeneratorInterpretation<G> {
42    fn blank_id(&self, blank_id: &BlankId) -> Option<Self::Resource> {
43        Some(blank_id.to_owned().into())
44    }
45}
46
47impl<G> InterpretationMut for LocalGeneratorInterpretation<G> {
48    fn insert_iri<'a>(&mut self, iri: impl Into<Cow<'a, IriBuf>>) -> Self::Resource {
49        iri.into().into_owned().into()
50    }
51
52    fn insert_literal<'a>(&mut self, literal: impl Into<CowLiteral<'a>>) -> Self::Resource {
53        literal.into().into_owned().into()
54    }
55}
56
57impl<G> ReverseInterpretation for LocalGeneratorInterpretation<G> {
58    type Iris<'a>
59        = std::option::IntoIter<Cow<'a, IriBuf>>
60    where
61        Self: 'a;
62    type Literals<'a>
63        = std::option::IntoIter<CowLiteral<'a>>
64    where
65        Self: 'a;
66
67    fn iris_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Iris<'a> {
68        match resource {
69            LocalTerm::Named(crate::Term::Iri(iri)) => Some(Cow::Borrowed(iri)).into_iter(),
70            _ => None.into_iter(),
71        }
72    }
73
74    fn literals_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Literals<'a> {
75        resource.as_literal().map(LiteralRef::into_cow).into_iter()
76    }
77
78    fn triple_term_components(&self, resource: &LocalTerm) -> Option<(LocalTerm, LocalTerm, LocalTerm)> {
79        match resource {
80            LocalTerm::Triple(t) => {
81                let s = match &t.0 {
82                    crate::Id::Iri(iri) => LocalTerm::iri(iri.clone()),
83                    crate::Id::BlankId(b) => LocalTerm::BlankId(b.clone()),
84                };
85                Some((s, LocalTerm::iri(t.1.clone()), t.2.clone()))
86            }
87            _ => None,
88        }
89    }
90
91    fn triple_term_components_view<'a>(&'a self, resource: &'a LocalTerm) -> Option<(Cow<'a, LocalTerm>, Cow<'a, LocalTerm>, Cow<'a, LocalTerm>)> {
92        match resource {
93            LocalTerm::Triple(t) => {
94                let s = match &t.0 {
95                    crate::Id::Iri(iri) => LocalTerm::iri(iri.clone()),
96                    crate::Id::BlankId(b) => LocalTerm::BlankId(b.clone()),
97                };
98                Some((Cow::Owned(s), Cow::Owned(LocalTerm::iri(t.1.clone())), Cow::Borrowed(&t.2)))
99            }
100            _ => None,
101        }
102    }
103}
104
105impl<G> ReverseLocalInterpretation for LocalGeneratorInterpretation<G> {
106    type BlankIds<'a>
107        = std::option::IntoIter<Cow<'a, BlankId>>
108    where
109        Self: 'a;
110
111    fn blank_ids_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::BlankIds<'a> {
112        resource.as_blank_id().map(Cow::Borrowed).into_iter()
113    }
114}
115
116impl<G: LocalGenerator> ConstGenerativeInterpretation for LocalGeneratorInterpretation<G> {
117    fn new_resource(&self) -> Self::Resource {
118        let mut generator = self.0.borrow_mut();
119        generator.next_local_term()
120    }
121}