rudof_rdf/rdf_core/build_rdf.rs
1use iri_s::IriS;
2use prefixmap::PrefixMap;
3use std::io::Write;
4
5use crate::rdf_core::{NeighsRDF, RDFFormat};
6
7/// Trait for building and modifying RDF graphs.
8///
9/// This trait provides methods for constructing RDF graphs by adding triples,
10/// managing namespace prefixes, creating blank nodes, and serializing the
11/// graph to various RDF formats. It extends [`NeighsRDF`] with mutation
12/// capabilities.
13pub trait BuildRDF: NeighsRDF {
14 /// Creates a new empty RDF graph.
15 ///
16 /// Returns a graph with no triples, no prefix declarations, and no base IRI.
17 /// This is the starting point for building RDF data.
18 fn empty() -> Self;
19
20 /// Sets the base IRI for resolving relative IRI references.
21 ///
22 /// # Arguments
23 ///
24 /// * `base` - Optional base IRI for resolving relative references
25 fn add_base(&mut self, base: &Option<IriS>) -> Result<(), Self::Err>;
26
27 /// Adds a namespace prefix declaration to the graph.
28 ///
29 /// # Arguments
30 ///
31 /// * `alias` - The prefix alias (e.g., "foaf", "ex", "rdf")
32 /// * `iri` - The full namespace IRI this prefix represents
33 fn add_prefix(&mut self, alias: &str, iri: &IriS) -> Result<(), Self::Err>;
34
35 /// Adds multiple prefix declarations from a prefix map.
36 ///
37 /// # Arguments
38 ///
39 /// * `prefix_map` - A map of prefix aliases to namespace IRIs
40 fn add_prefix_map(&mut self, prefix_map: PrefixMap) -> Result<(), Self::Err>;
41
42 /// Adds an RDF triple to the graph.
43 ///
44 /// # Type Parameters
45 ///
46 /// * `S` - Type convertible to the subject representation
47 /// * `P` - Type convertible to the predicate (IRI) representation
48 /// * `O` - Type convertible to the object (term) representation
49 ///
50 /// # Arguments
51 ///
52 /// * `subj` - The subject of the triple (IRI or blank node)
53 /// * `pred` - The predicate of the triple (must be an IRI)
54 /// * `obj` - The object of the triple (IRI, blank node, or literal)
55 fn add_triple<S, P, O>(&mut self, subj: S, pred: P, obj: O) -> Result<(), Self::Err>
56 where
57 S: Into<Self::Subject>,
58 P: Into<Self::IRI>,
59 O: Into<Self::Term>;
60
61 /// Removes an RDF triple from the graph.
62 ///
63 /// # Type Parameters
64 ///
65 /// * `S` - Type convertible to the subject representation
66 /// * `P` - Type convertible to the predicate (IRI) representation
67 /// * `O` - Type convertible to the object (term) representation
68 ///
69 /// # Arguments
70 ///
71 /// * `subj` - The subject of the triple to remove
72 /// * `pred` - The predicate of the triple to remove
73 /// * `obj` - The object of the triple to remove
74 fn remove_triple<S, P, O>(&mut self, subj: S, pred: P, obj: O) -> Result<(), Self::Err>
75 where
76 S: Into<Self::Subject>,
77 P: Into<Self::IRI>,
78 O: Into<Self::Term>;
79
80 /// Adds an `rdf:type` declaration to the graph.
81 ///
82 /// # Type Parameters
83 ///
84 /// * `S` - Type convertible to the subject representation
85 /// * `T` - Type convertible to the term representation (typically an IRI)
86 ///
87 /// # Arguments
88 ///
89 /// * `node` - The resource being typed (subject)
90 /// * `type_` - The type/class of the resource (typically an IRI)
91 fn add_type<S, T>(&mut self, node: S, type_: T) -> Result<(), Self::Err>
92 where
93 S: Into<Self::Subject>,
94 T: Into<Self::Term>;
95
96 /// Adds an Blank node to the RDF graph and get the node identifier
97 fn add_bnode(&mut self) -> Result<Self::BNode, Self::Err>;
98
99 /// Serializes the graph to an RDF format.
100 ///
101 /// # Arguments
102 ///
103 /// * `format` - The RDF serialization format to use
104 /// * `writer` - The destination for serialized output
105 fn serialize<W: Write>(&self, format: &RDFFormat, writer: &mut W) -> Result<(), Self::Err>;
106}