rdf_syntax/interpretation/impl/
generator.rs1use std::{borrow::Cow, cell::RefCell};
2
3use iref::Iri;
4use rdf_types::{ConstGenDomain, Domain};
5
6use crate::{
7 BlankId, CowLiteral, Generator, GroundInterpretation, GroundInterpretationMut, LiteralRef,
8 Term,
9 interpretation::{
10 Interpretation, InterpretationMut, ReverseGroundInterpretation, ReverseInterpretation,
11 },
12};
13
14pub struct GenInterpretation<G>(RefCell<G>);
16
17impl<G> GenInterpretation<G> {
18 pub fn new(generator: G) -> Self {
20 Self(RefCell::new(generator))
21 }
22
23 pub fn into_generator(self) -> G {
25 self.0.into_inner()
26 }
27}
28
29impl<G> Domain for GenInterpretation<G> {
30 type Resource = Term;
31}
32
33impl<G> GroundInterpretation for GenInterpretation<G> {
34 fn iri(&self, iri: &Iri) -> Option<Self::Resource> {
35 Some(iri.to_owned().into())
36 }
37
38 fn literal<'a>(&self, literal: impl Into<LiteralRef<'a>>) -> Option<Self::Resource> {
39 Some(literal.into().to_owned().into())
40 }
41}
42
43impl<G> Interpretation for GenInterpretation<G> {
44 fn blank_id(&self, blank_id: &BlankId) -> Option<Self::Resource> {
45 Some(blank_id.to_owned().into())
46 }
47}
48
49impl<G> GroundInterpretationMut for GenInterpretation<G> {
50 fn insert_iri<'a>(&mut self, iri: impl Into<Cow<'a, Iri>>) -> Self::Resource {
51 iri.into().into_owned().into()
52 }
53
54 fn insert_literal<'a>(&mut self, literal: impl Into<CowLiteral<'a>>) -> Self::Resource {
55 literal.into().into_owned().into()
56 }
57}
58
59impl<G> InterpretationMut for GenInterpretation<G> {
60 fn insert_blank_id<'a>(&mut self, blank_id: impl Into<Cow<'a, BlankId>>) -> Self::Resource {
61 blank_id.into().into_owned().into()
62 }
63}
64
65impl<G> ReverseGroundInterpretation for GenInterpretation<G> {
66 type Iris<'a>
67 = std::option::IntoIter<&'a Iri>
68 where
69 Self: 'a;
70 type Literals<'a>
71 = std::option::IntoIter<LiteralRef<'a>>
72 where
73 Self: 'a;
74
75 fn iris_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Iris<'a> {
76 resource.as_iri().into_iter()
77 }
78
79 fn literals_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::Literals<'a> {
80 resource.as_literal().into_iter()
81 }
82}
83
84impl<G> ReverseInterpretation for GenInterpretation<G> {
85 type BlankIds<'a>
86 = std::option::IntoIter<&'a BlankId>
87 where
88 Self: 'a;
89
90 fn blank_ids_of<'a>(&'a self, resource: &'a Self::Resource) -> Self::BlankIds<'a> {
91 resource.as_blank_id().into_iter()
92 }
93}
94
95impl<G: Generator> ConstGenDomain for GenInterpretation<G> {
96 fn new_resource(&self) -> Self::Resource {
97 let mut generator = self.0.borrow_mut();
98 generator.next_id().into()
99 }
100}