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
//! Domain types for the standalone SPARQL 1.1 Update protocol.
//!
//! This sibling module hosts the data structures used by the
//! [`update_protocol`](crate::update_protocol) facade: concrete triples,
//! pattern terms, triple patterns, the top-level [`SparqlUpdate`] enum,
//! and the result / error types returned by the parser and executor.
// ---------------------------------------------------------------------------
// Domain types
// ---------------------------------------------------------------------------
/// A concrete RDF triple (no variables).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Triple {
pub s: String,
pub p: String,
pub o: String,
}
impl Triple {
/// Convenience constructor.
pub fn new(s: impl Into<String>, p: impl Into<String>, o: impl Into<String>) -> Self {
Self {
s: s.into(),
p: p.into(),
o: o.into(),
}
}
}
/// A position in a triple pattern – can be an IRI, a plain literal, a blank
/// node, or a variable (placeholder for pattern matching).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PatternTerm {
Iri(String),
Literal(String),
Variable(String),
BlankNode(String),
}
impl PatternTerm {
/// Returns `true` when this term is a variable (used during template instantiation).
pub fn is_variable(&self) -> bool {
matches!(self, PatternTerm::Variable(_))
}
/// Returns the variable name if this is a `Variable` variant.
pub fn variable_name(&self) -> Option<&str> {
if let PatternTerm::Variable(name) = self {
Some(name.as_str())
} else {
None
}
}
}
/// A triple pattern where any position may be a variable.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TriplePattern {
pub s: PatternTerm,
pub p: PatternTerm,
pub o: PatternTerm,
}
impl TriplePattern {
/// Construct a new triple pattern.
pub fn new(s: PatternTerm, p: PatternTerm, o: PatternTerm) -> Self {
Self { s, o, p }
}
}
// ---------------------------------------------------------------------------
// DROP / CLEAR target type
// ---------------------------------------------------------------------------
/// Scope qualifier for `DROP` operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DropType {
/// A specific named graph identified by IRI.
Graph,
/// The default graph.
Default,
/// All named graphs.
Named,
/// Every graph in the dataset (default + all named).
All,
}
/// Scope qualifier for `CLEAR` operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClearType {
/// A specific named graph identified by IRI.
Graph,
/// The default graph.
Default,
/// All named graphs.
Named,
/// Every graph in the dataset.
All,
}
// ---------------------------------------------------------------------------
// Top-level update enum
// ---------------------------------------------------------------------------
/// A single SPARQL 1.1 update operation.
#[derive(Debug, Clone, PartialEq)]
pub enum SparqlUpdate {
/// `INSERT DATA { … }` — adds concrete triples to the default graph.
InsertData(Vec<Triple>),
/// `DELETE DATA { … }` — removes concrete triples from the default graph.
DeleteData(Vec<Triple>),
/// `INSERT { template } WHERE { where_clause }` — pattern-based insert.
InsertWhere {
template: Vec<TriplePattern>,
where_clause: Vec<TriplePattern>,
},
/// `DELETE { template } WHERE { where_clause }` — pattern-based delete.
DeleteWhere {
template: Vec<TriplePattern>,
where_clause: Vec<TriplePattern>,
},
/// `DELETE { delete } INSERT { insert } WHERE { where_clause }` — combined modify.
Modify {
delete: Vec<TriplePattern>,
insert: Vec<TriplePattern>,
where_clause: Vec<TriplePattern>,
},
/// `CREATE [SILENT] GRAPH <iri>`.
CreateGraph { iri: String, silent: bool },
/// `DROP [SILENT] (GRAPH <iri> | DEFAULT | NAMED | ALL)`.
DropGraph {
iri: Option<String>,
silent: bool,
drop_type: DropType,
},
/// `CLEAR [SILENT] (GRAPH <iri> | DEFAULT | NAMED | ALL)`.
ClearGraph {
iri: Option<String>,
silent: bool,
clear_type: ClearType,
},
/// `COPY [SILENT] <source> TO <target>`.
CopyGraph {
source: String,
target: String,
silent: bool,
},
/// `MOVE [SILENT] <source> TO <target>`.
MoveGraph {
source: String,
target: String,
silent: bool,
},
/// `ADD [SILENT] <source> TO <target>`.
AddGraph {
source: String,
target: String,
silent: bool,
},
/// `LOAD [SILENT] <iri> [INTO GRAPH <into>]`.
Load {
iri: String,
into: Option<String>,
silent: bool,
},
}
// ---------------------------------------------------------------------------
// Parse error
// ---------------------------------------------------------------------------
/// Error returned by `SparqlUpdateParser`.
#[derive(Debug, Clone, PartialEq)]
pub struct ParseError {
pub message: String,
/// Byte offset inside the input string where the error was detected.
pub position: usize,
}
impl ParseError {
/// Construct a `ParseError` at the given byte position.
pub(crate) fn at(position: usize, message: impl Into<String>) -> Self {
Self {
message: message.into(),
position,
}
}
}
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"parse error at position {}: {}",
self.position, self.message
)
}
}
impl std::error::Error for ParseError {}
// ---------------------------------------------------------------------------
// UpdateResult
// ---------------------------------------------------------------------------
/// Summary of the changes made by a single `UpdateExecutor::execute` call.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UpdateResult {
/// Number of triples inserted into the default graph or named graphs.
pub triples_inserted: usize,
/// Number of triples deleted from the default graph or named graphs.
pub triples_deleted: usize,
/// Number of distinct graphs affected (created, cleared, populated, etc.).
pub graphs_affected: usize,
}
// ---------------------------------------------------------------------------
// ArqError (thin wrapper)
// ---------------------------------------------------------------------------
/// Error type for the update executor.
#[derive(Debug, Clone, PartialEq)]
pub struct ArqError(pub String);
impl std::fmt::Display for ArqError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ARQ error: {}", self.0)
}
}
impl std::error::Error for ArqError {}