oxirs-core 0.2.4

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! RDF Quad implementation

use crate::model::{
    BlankNode, NamedNode, Object, ObjectRef, Predicate, PredicateRef, Subject, SubjectRef, Triple,
    TripleRef, Variable,
};
use std::fmt;
use std::hash::Hash;

/// Union type for terms that can be graph names
#[derive(
    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
pub enum GraphName {
    NamedNode(NamedNode),
    BlankNode(BlankNode),
    Variable(Variable),
    DefaultGraph,
}

impl GraphName {
    /// Returns true if this is the default graph
    pub fn is_default_graph(&self) -> bool {
        matches!(self, GraphName::DefaultGraph)
    }
}

impl fmt::Display for GraphName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GraphName::NamedNode(n) => write!(f, "{n}"),
            GraphName::BlankNode(b) => write!(f, "{b}"),
            GraphName::Variable(v) => write!(f, "{v}"),
            GraphName::DefaultGraph => write!(f, "DEFAULT"),
        }
    }
}

impl AsRef<str> for GraphName {
    fn as_ref(&self) -> &str {
        match self {
            GraphName::NamedNode(n) => n.as_str(),
            GraphName::BlankNode(b) => b.as_str(),
            GraphName::Variable(v) => v.name(),
            GraphName::DefaultGraph => "DEFAULT",
        }
    }
}

impl From<NamedNode> for GraphName {
    fn from(node: NamedNode) -> Self {
        GraphName::NamedNode(node)
    }
}

impl From<BlankNode> for GraphName {
    fn from(node: BlankNode) -> Self {
        GraphName::BlankNode(node)
    }
}

impl From<Variable> for GraphName {
    fn from(variable: Variable) -> Self {
        GraphName::Variable(variable)
    }
}

impl From<crate::model::node::NamedOrBlankNode> for GraphName {
    fn from(node: crate::model::node::NamedOrBlankNode) -> Self {
        match node {
            crate::model::node::NamedOrBlankNode::NamedNode(n) => GraphName::NamedNode(n),
            crate::model::node::NamedOrBlankNode::BlankNode(b) => GraphName::BlankNode(b),
        }
    }
}

/// An RDF Quad
///
/// Represents an RDF statement with subject, predicate, object, and graph name.
/// This is used in RDF datasets where triples can belong to different named graphs.
#[derive(
    Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
pub struct Quad {
    subject: Subject,
    predicate: Predicate,
    object: Object,
    graph_name: GraphName,
}

impl Quad {
    /// Creates a new quad
    pub fn new(
        subject: impl Into<Subject>,
        predicate: impl Into<Predicate>,
        object: impl Into<Object>,
        graph_name: impl Into<GraphName>,
    ) -> Self {
        Quad {
            subject: subject.into(),
            predicate: predicate.into(),
            object: object.into(),
            graph_name: graph_name.into(),
        }
    }

    /// Creates a new quad in the default graph
    pub fn new_default_graph(
        subject: impl Into<Subject>,
        predicate: impl Into<Predicate>,
        object: impl Into<Object>,
    ) -> Self {
        Quad {
            subject: subject.into(),
            predicate: predicate.into(),
            object: object.into(),
            graph_name: GraphName::DefaultGraph,
        }
    }

    /// Creates a quad from a triple, placing it in the default graph
    pub fn from_triple(triple: Triple) -> Self {
        let (subject, predicate, object) = triple.into_parts();
        Quad::new_default_graph(subject, predicate, object)
    }

    /// Creates a quad from a triple with a specific graph name
    pub fn from_triple_in_graph(triple: Triple, graph_name: impl Into<GraphName>) -> Self {
        let (subject, predicate, object) = triple.into_parts();
        Quad::new(subject, predicate, object, graph_name)
    }

    /// Returns the subject of this quad
    pub fn subject(&self) -> &Subject {
        &self.subject
    }

    /// Returns the predicate of this quad
    pub fn predicate(&self) -> &Predicate {
        &self.predicate
    }

    /// Returns the object of this quad
    pub fn object(&self) -> &Object {
        &self.object
    }

    /// Returns the graph name of this quad
    pub fn graph_name(&self) -> &GraphName {
        &self.graph_name
    }

    /// Decomposes the quad into its components
    pub fn into_parts(self) -> (Subject, Predicate, Object, GraphName) {
        (self.subject, self.predicate, self.object, self.graph_name)
    }

    /// Converts this quad to a triple, discarding the graph name
    pub fn to_triple(&self) -> Triple {
        Triple::new(
            self.subject.clone(),
            self.predicate.clone(),
            self.object.clone(),
        )
    }

    /// Returns a reference to this quad
    pub fn as_ref(&self) -> QuadRef<'_> {
        QuadRef::from(self)
    }

    /// Returns true if this quad is in the default graph
    pub fn is_default_graph(&self) -> bool {
        matches!(self.graph_name, GraphName::DefaultGraph)
    }

    /// Returns the triple if this quad is in the default graph, None otherwise
    pub fn triple_in_default_graph(&self) -> Option<Triple> {
        if self.is_default_graph() {
            Some(self.to_triple())
        } else {
            None
        }
    }

    /// Returns true if this quad contains any variables
    pub fn has_variables(&self) -> bool {
        matches!(self.subject, Subject::Variable(_))
            || matches!(self.predicate, Predicate::Variable(_))
            || matches!(self.object, Object::Variable(_))
            || matches!(self.graph_name, GraphName::Variable(_))
    }

    /// Returns true if this quad is ground (contains no variables)
    pub fn is_ground(&self) -> bool {
        !self.has_variables()
    }
}

impl fmt::Display for Quad {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.graph_name {
            GraphName::DefaultGraph => {
                write!(f, "{} {} {} .", self.subject, self.predicate, self.object)
            }
            graph => write!(
                f,
                "{} {} {} {} .",
                self.subject, self.predicate, self.object, graph
            ),
        }
    }
}

/// A borrowed quad reference for zero-copy operations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct QuadRef<'a> {
    subject: SubjectRef<'a>,
    predicate: PredicateRef<'a>,
    object: ObjectRef<'a>,
    graph_name: GraphNameRef<'a>,
}

impl<'a> QuadRef<'a> {
    /// Creates a new quad reference
    pub fn new(
        subject: SubjectRef<'a>,
        predicate: PredicateRef<'a>,
        object: ObjectRef<'a>,
        graph_name: GraphNameRef<'a>,
    ) -> Self {
        QuadRef {
            subject,
            predicate,
            object,
            graph_name,
        }
    }

    /// Returns the subject
    pub fn subject(&self) -> SubjectRef<'a> {
        self.subject
    }

    /// Returns the predicate
    pub fn predicate(&self) -> PredicateRef<'a> {
        self.predicate
    }

    /// Returns the object
    pub fn object(&self) -> ObjectRef<'a> {
        self.object
    }

    /// Returns the graph name
    pub fn graph_name(&self) -> GraphNameRef<'a> {
        self.graph_name
    }

    /// Converts to an owned quad
    pub fn to_owned(&self) -> Quad {
        Quad {
            subject: self.subject.to_owned(),
            predicate: self.predicate.to_owned(),
            object: self.object.to_owned(),
            graph_name: self.graph_name.to_owned(),
        }
    }

    /// Converts to a triple reference, discarding the graph name
    pub fn to_triple_ref(&self) -> TripleRef<'a> {
        TripleRef::new(self.subject, self.predicate, self.object)
    }

    /// Returns the triple part of this quad (alias for to_triple_ref)
    pub fn triple(&self) -> TripleRef<'a> {
        self.to_triple_ref()
    }

    /// Converts to an owned quad (alias for to_owned)
    pub fn into_owned(self) -> Quad {
        self.to_owned()
    }
}

impl<'a> fmt::Display for QuadRef<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.graph_name {
            GraphNameRef::DefaultGraph => {
                write!(f, "{} {} {} .", self.subject, self.predicate, self.object)
            }
            graph => write!(
                f,
                "{} {} {} {} .",
                self.subject, self.predicate, self.object, graph
            ),
        }
    }
}

/// Borrowed graph name reference
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GraphNameRef<'a> {
    NamedNode(&'a NamedNode),
    BlankNode(&'a BlankNode),
    Variable(&'a Variable),
    DefaultGraph,
}

impl<'a> GraphNameRef<'a> {
    /// Returns true if this is the default graph
    pub fn is_default_graph(&self) -> bool {
        matches!(self, GraphNameRef::DefaultGraph)
    }

    /// Converts to an owned graph name
    pub fn to_owned(&self) -> GraphName {
        match self {
            GraphNameRef::NamedNode(n) => GraphName::NamedNode((*n).clone()),
            GraphNameRef::BlankNode(b) => GraphName::BlankNode((*b).clone()),
            GraphNameRef::Variable(v) => GraphName::Variable((*v).clone()),
            GraphNameRef::DefaultGraph => GraphName::DefaultGraph,
        }
    }
}

impl<'a> fmt::Display for GraphNameRef<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GraphNameRef::NamedNode(n) => write!(f, "{n}"),
            GraphNameRef::BlankNode(b) => write!(f, "{b}"),
            GraphNameRef::Variable(v) => write!(f, "{v}"),
            GraphNameRef::DefaultGraph => write!(f, "DEFAULT"),
        }
    }
}

// Conversion implementations
impl<'a> From<&'a GraphName> for GraphNameRef<'a> {
    fn from(graph_name: &'a GraphName) -> Self {
        match graph_name {
            GraphName::NamedNode(n) => GraphNameRef::NamedNode(n),
            GraphName::BlankNode(b) => GraphNameRef::BlankNode(b),
            GraphName::Variable(v) => GraphNameRef::Variable(v),
            GraphName::DefaultGraph => GraphNameRef::DefaultGraph,
        }
    }
}

impl<'a> From<&'a Quad> for QuadRef<'a> {
    fn from(quad: &'a Quad) -> Self {
        QuadRef {
            subject: quad.subject().into(),
            predicate: quad.predicate().into(),
            object: quad.object().into(),
            graph_name: quad.graph_name().into(),
        }
    }
}

impl<'a> From<QuadRef<'a>> for Quad {
    fn from(quad_ref: QuadRef<'a>) -> Self {
        quad_ref.to_owned()
    }
}

impl From<Triple> for Quad {
    fn from(triple: Triple) -> Self {
        Quad::from_triple(triple)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{Literal, NamedNode};

    #[test]
    fn test_quad_creation() {
        let subject = NamedNode::new("http://example.org/subject").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/predicate").expect("valid IRI");
        let object = Literal::new("object");
        let graph = NamedNode::new("http://example.org/graph").expect("valid IRI");

        let quad = Quad::new(
            subject.clone(),
            predicate.clone(),
            object.clone(),
            graph.clone(),
        );

        assert!(quad.is_ground());
        assert!(!quad.has_variables());
        assert!(!quad.is_default_graph());
    }

    #[test]
    fn test_quad_default_graph() {
        let subject = NamedNode::new("http://example.org/subject").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/predicate").expect("valid IRI");
        let object = Literal::new("object");

        let quad = Quad::new_default_graph(subject, predicate, object);

        assert!(quad.is_default_graph());
        assert!(quad.graph_name().is_default_graph());
    }

    #[test]
    fn test_quad_from_triple() {
        let subject = NamedNode::new("http://example.org/subject").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/predicate").expect("valid IRI");
        let object = Literal::new("object");

        let triple = Triple::new(subject.clone(), predicate.clone(), object.clone());
        let quad = Quad::from_triple(triple.clone());

        assert!(quad.is_default_graph());
        assert_eq!(quad.to_triple().subject(), triple.subject());
        assert_eq!(quad.to_triple().predicate(), triple.predicate());
        assert_eq!(quad.to_triple().object(), triple.object());
    }

    #[test]
    fn test_quad_with_variable() {
        let subject = Variable::new("x").expect("valid variable name");
        let predicate = NamedNode::new("http://example.org/predicate").expect("valid IRI");
        let object = Literal::new("object");
        let graph = Variable::new("g").expect("valid variable name");

        let quad = Quad::new(subject, predicate, object, graph);

        assert!(!quad.is_ground());
        assert!(quad.has_variables());
    }

    #[test]
    fn test_quad_ref() {
        let subject = NamedNode::new("http://example.org/s").expect("valid IRI");
        let predicate = NamedNode::new("http://example.org/p").expect("valid IRI");
        let object = Literal::new("o");
        let graph = NamedNode::new("http://example.org/g").expect("valid IRI");

        let quad = Quad::new(subject, predicate, object, graph);
        let quad_ref = QuadRef::from(&quad);
        let quad_owned = quad_ref.to_owned();

        assert_eq!(quad, quad_owned);
    }
}