rdf_types/dataset/graph/
fallible.rs1use crate::{
4 Triple,
5 pattern::LinearTriplePattern,
6 util::{InfallibleIterator, TriplesIntoOwned},
7};
8
9use super::{FiniteGraph, Graph, GraphMut, PatternMatchingGraph};
10
11pub trait TryGraph {
19 type Resource;
21
22 type Error;
24}
25
26impl<D: Graph> TryGraph for D {
29 type Resource = D::Resource;
30 type Error = std::convert::Infallible;
31}
32
33pub trait TryFiniteGraph: TryGraph {
35 type TryTriples<'a>: Iterator<Item = Result<Triple<Self::Resource>, Self::Error>>
37 where
38 Self: 'a;
39
40 fn try_triples(&self) -> Result<Self::TryTriples<'_>, Self::Error>;
42}
43
44impl<D: FiniteGraph> TryFiniteGraph for D
47where
48 D::Resource: Clone,
49{
50 type TryTriples<'a>
51 = InfallibleIterator<TriplesIntoOwned<D::Triples<'a>>>
52 where
53 Self: 'a;
54
55 fn try_triples(&self) -> Result<Self::TryTriples<'_>, Self::Error> {
56 Ok(InfallibleIterator(TriplesIntoOwned(self.triples())))
57 }
58}
59
60pub trait TryPatternMatchingGraph: TryGraph {
62 type TryTriplePatternMatching<'a, 'p>: Iterator<
64 Item = Result<Triple<Self::Resource>, Self::Error>,
65 >
66 where
67 Self: 'a,
68 Self::Resource: 'p;
69
70 fn try_triple_pattern_matching<'p>(
73 &self,
74 pattern: LinearTriplePattern<&'p Self::Resource>,
75 ) -> Result<Self::TryTriplePatternMatching<'_, 'p>, Self::Error>;
76
77 fn try_contains_triple(&self, triple: Triple<&Self::Resource>) -> Result<bool, Self::Error> {
79 Ok(self
80 .try_triple_pattern_matching(triple.into())?
81 .next()
82 .transpose()?
83 .is_some())
84 }
85}
86
87impl<D: PatternMatchingGraph> TryPatternMatchingGraph for D
90where
91 D::Resource: Clone,
92{
93 type TryTriplePatternMatching<'a, 'p>
94 = InfallibleIterator<TriplesIntoOwned<D::TriplePatternMatching<'a, 'p>>>
95 where
96 Self: 'a,
97 Self::Resource: 'p;
98
99 fn try_triple_pattern_matching<'p>(
100 &self,
101 pattern: LinearTriplePattern<&'p Self::Resource>,
102 ) -> Result<Self::TryTriplePatternMatching<'_, 'p>, Self::Error> {
103 Ok(InfallibleIterator(TriplesIntoOwned(
104 self.triple_pattern_matching(pattern),
105 )))
106 }
107}
108
109pub trait TryGraphMut: TryGraph {
111 fn try_insert(&mut self, triple: Triple<Self::Resource>) -> Result<(), Self::Error>;
113}
114
115impl<D: GraphMut> TryGraphMut for D {
118 fn try_insert(&mut self, triple: Triple<Self::Resource>) -> Result<(), Self::Error> {
119 self.insert(triple);
120 Ok(())
121 }
122}