oxirs-core 0.2.3

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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! SPARQL DESCRIBE query builder.
//!
//! Provides a fluent builder API for constructing SPARQL DESCRIBE queries.
//! Supports IRI targets, variable targets, WHERE patterns, FROM clauses, and LIMIT.

/// Target type for a DESCRIBE query.
///
/// A DESCRIBE query can target explicit IRIs, variables bound in a WHERE clause,
/// or a combination of both.
#[derive(Debug, Clone, PartialEq)]
pub enum DescribeTarget {
    /// A concrete IRI to describe.
    Iri(String),
    /// A variable whose bindings will be described.
    Variable(String),
}

/// Builder for SPARQL DESCRIBE queries.
///
/// Constructs valid SPARQL 1.1 DESCRIBE query strings with optional FROM,
/// WHERE, and LIMIT clauses.
///
/// # Examples
///
/// ```rust
/// use oxirs_core::sparql::describe_builder::DescribeBuilder;
///
/// let query = DescribeBuilder::new()
///     .add_iri("http://example.org/subject")
///     .with_limit(10)
///     .build();
///
/// assert!(query.contains("DESCRIBE"));
/// assert!(query.contains("<http://example.org/subject>"));
/// ```
#[derive(Debug, Clone, Default)]
pub struct DescribeBuilder {
    targets: Vec<DescribeTarget>,
    from_graphs: Vec<String>,
    where_pattern: Option<String>,
    limit: Option<usize>,
}

impl DescribeBuilder {
    /// Create a new empty DESCRIBE builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a concrete IRI as a DESCRIBE target.
    pub fn add_iri(mut self, iri: &str) -> Self {
        self.targets.push(DescribeTarget::Iri(iri.to_string()));
        self
    }

    /// Add a variable as a DESCRIBE target.
    ///
    /// The variable name should be given without the leading `?`.
    pub fn add_variable(mut self, var: &str) -> Self {
        self.targets.push(DescribeTarget::Variable(var.to_string()));
        self
    }

    /// Set the WHERE graph pattern.
    pub fn with_where(mut self, pattern: &str) -> Self {
        self.where_pattern = Some(pattern.to_string());
        self
    }

    /// Add a FROM clause (named default graph).
    pub fn with_from(mut self, graph: &str) -> Self {
        self.from_graphs.push(graph.to_string());
        self
    }

    /// Set a LIMIT on the number of described resources.
    pub fn with_limit(mut self, n: usize) -> Self {
        self.limit = Some(n);
        self
    }

    /// Build the SPARQL DESCRIBE query string.
    ///
    /// Returns a complete SPARQL query string. If no targets are given,
    /// produces `DESCRIBE *` (wildcard form).
    pub fn build(&self) -> String {
        let mut parts: Vec<String> = Vec::new();

        // DESCRIBE keyword and targets
        if self.targets.is_empty() {
            parts.push("DESCRIBE *".to_string());
        } else {
            let target_strs: Vec<String> = self
                .targets
                .iter()
                .map(|t| match t {
                    DescribeTarget::Iri(iri) => format!("<{}>", iri),
                    DescribeTarget::Variable(var) => format!("?{}", var),
                })
                .collect();
            parts.push(format!("DESCRIBE {}", target_strs.join(" ")));
        }

        // FROM clauses
        for graph in &self.from_graphs {
            parts.push(format!("FROM <{}>", graph));
        }

        // WHERE clause
        if let Some(pattern) = &self.where_pattern {
            parts.push(format!("WHERE {{\n  {}\n}}", pattern));
        }

        // LIMIT clause
        if let Some(limit) = self.limit {
            parts.push(format!("LIMIT {}", limit));
        }

        parts.join("\n")
    }

    /// Return the list of targets configured in this builder.
    pub fn targets(&self) -> &[DescribeTarget] {
        &self.targets
    }

    /// Return the WHERE pattern if one has been set.
    pub fn where_pattern(&self) -> Option<&str> {
        self.where_pattern.as_deref()
    }

    /// Return the FROM graphs configured in this builder.
    pub fn from_graphs(&self) -> &[String] {
        &self.from_graphs
    }

    /// Return the LIMIT value if set.
    pub fn limit(&self) -> Option<usize> {
        self.limit
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- Basic construction ---

    #[test]
    fn test_new_is_default() {
        let b = DescribeBuilder::new();
        assert!(b.targets().is_empty());
        assert!(b.where_pattern().is_none());
        assert!(b.from_graphs().is_empty());
        assert!(b.limit().is_none());
    }

    #[test]
    fn test_empty_target_produces_wildcard() {
        let q = DescribeBuilder::new().build();
        assert!(q.starts_with("DESCRIBE *"));
    }

    #[test]
    fn test_single_iri() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .build();
        assert!(q.contains("DESCRIBE"));
        assert!(q.contains("<http://example.org/a>"));
        assert!(!q.contains('*'));
    }

    #[test]
    fn test_multiple_iris() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .add_iri("http://example.org/b")
            .build();
        assert!(q.contains("<http://example.org/a>"));
        assert!(q.contains("<http://example.org/b>"));
    }

    #[test]
    fn test_single_variable() {
        let q = DescribeBuilder::new().add_variable("x").build();
        assert!(q.contains("?x"));
        assert!(!q.contains('*'));
    }

    #[test]
    fn test_multiple_variables() {
        let q = DescribeBuilder::new()
            .add_variable("x")
            .add_variable("y")
            .build();
        assert!(q.contains("?x"));
        assert!(q.contains("?y"));
    }

    #[test]
    fn test_iri_and_variable_mixed() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .add_variable("x")
            .build();
        assert!(q.contains("<http://example.org/a>"));
        assert!(q.contains("?x"));
    }

    // --- WHERE clause ---

    #[test]
    fn test_with_where_present() {
        let q = DescribeBuilder::new()
            .add_variable("x")
            .with_where("?x a <http://example.org/Type>")
            .build();
        assert!(q.contains("WHERE"));
        assert!(q.contains("?x a <http://example.org/Type>"));
    }

    #[test]
    fn test_without_where_absent() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .build();
        assert!(!q.contains("WHERE"));
    }

    #[test]
    fn test_where_overwrites_previous() {
        let b = DescribeBuilder::new()
            .add_variable("x")
            .with_where("?x a <http://example.org/Old>")
            .with_where("?x a <http://example.org/New>");
        let q = b.build();
        assert!(q.contains("<http://example.org/New>"));
        assert!(!q.contains("<http://example.org/Old>"));
    }

    #[test]
    fn test_where_with_multiple_patterns() {
        let pattern = "?x <http://schema.org/name> ?name . ?x <http://schema.org/age> ?age";
        let q = DescribeBuilder::new()
            .add_variable("x")
            .with_where(pattern)
            .build();
        assert!(q.contains("<http://schema.org/name>"));
        assert!(q.contains("<http://schema.org/age>"));
    }

    // --- FROM clause ---

    #[test]
    fn test_single_from() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .with_from("http://example.org/graph1")
            .build();
        assert!(q.contains("FROM <http://example.org/graph1>"));
    }

    #[test]
    fn test_multiple_from() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .with_from("http://example.org/graph1")
            .with_from("http://example.org/graph2")
            .build();
        assert!(q.contains("FROM <http://example.org/graph1>"));
        assert!(q.contains("FROM <http://example.org/graph2>"));
    }

    #[test]
    fn test_no_from_by_default() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .build();
        assert!(!q.contains("FROM"));
    }

    // --- LIMIT clause ---

    #[test]
    fn test_with_limit() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .with_limit(50)
            .build();
        assert!(q.contains("LIMIT 50"));
    }

    #[test]
    fn test_limit_zero() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .with_limit(0)
            .build();
        assert!(q.contains("LIMIT 0"));
    }

    #[test]
    fn test_limit_overwrite() {
        let b = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .with_limit(10)
            .with_limit(99);
        assert_eq!(b.limit(), Some(99));
        let q = b.build();
        assert!(q.contains("LIMIT 99"));
        assert!(!q.contains("LIMIT 10"));
    }

    #[test]
    fn test_no_limit_by_default() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .build();
        assert!(!q.contains("LIMIT"));
    }

    // --- Combinations ---

    #[test]
    fn test_full_query_iri_from_where_limit() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/subject")
            .with_from("http://example.org/graph")
            .with_where("?x a <http://example.org/Type>")
            .with_limit(100)
            .build();
        assert!(q.contains("DESCRIBE <http://example.org/subject>"));
        assert!(q.contains("FROM <http://example.org/graph>"));
        assert!(q.contains("WHERE"));
        assert!(q.contains("LIMIT 100"));
    }

    #[test]
    fn test_full_query_variable_from_where_limit() {
        let q = DescribeBuilder::new()
            .add_variable("resource")
            .with_from("http://example.org/graph")
            .with_where("?resource a <http://example.org/Type>")
            .with_limit(20)
            .build();
        assert!(q.contains("DESCRIBE ?resource"));
        assert!(q.contains("FROM <http://example.org/graph>"));
        assert!(q.contains("WHERE"));
        assert!(q.contains("LIMIT 20"));
    }

    #[test]
    fn test_variable_with_where_no_from_no_limit() {
        let q = DescribeBuilder::new()
            .add_variable("s")
            .with_where("?s <http://purl.org/dc/terms/title> ?t")
            .build();
        assert!(q.contains("?s"));
        assert!(q.contains("WHERE"));
        assert!(!q.contains("FROM"));
        assert!(!q.contains("LIMIT"));
    }

    #[test]
    fn test_wildcard_with_where() {
        let q = DescribeBuilder::new()
            .with_where("?x a <http://example.org/T>")
            .build();
        assert!(q.contains("DESCRIBE *"));
        assert!(q.contains("WHERE"));
    }

    #[test]
    fn test_wildcard_with_limit() {
        let q = DescribeBuilder::new().with_limit(5).build();
        assert!(q.contains("DESCRIBE *"));
        assert!(q.contains("LIMIT 5"));
    }

    // --- Accessor methods ---

    #[test]
    fn test_targets_accessor() {
        let b = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .add_variable("x");
        let targets = b.targets();
        assert_eq!(targets.len(), 2);
        assert_eq!(
            targets[0],
            DescribeTarget::Iri("http://example.org/a".to_string())
        );
        assert_eq!(targets[1], DescribeTarget::Variable("x".to_string()));
    }

    #[test]
    fn test_where_pattern_accessor_none() {
        let b = DescribeBuilder::new();
        assert!(b.where_pattern().is_none());
    }

    #[test]
    fn test_where_pattern_accessor_some() {
        let b = DescribeBuilder::new().with_where("?x a <http://example.org/T>");
        assert_eq!(b.where_pattern(), Some("?x a <http://example.org/T>"));
    }

    #[test]
    fn test_from_graphs_accessor() {
        let b = DescribeBuilder::new()
            .with_from("http://example.org/g1")
            .with_from("http://example.org/g2");
        assert_eq!(b.from_graphs().len(), 2);
        assert_eq!(b.from_graphs()[0], "http://example.org/g1");
    }

    #[test]
    fn test_limit_accessor_none() {
        let b = DescribeBuilder::new();
        assert!(b.limit().is_none());
    }

    #[test]
    fn test_limit_accessor_some() {
        let b = DescribeBuilder::new().with_limit(42);
        assert_eq!(b.limit(), Some(42));
    }

    // --- Clone and Debug ---

    #[test]
    fn test_clone_independence() {
        let b1 = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .with_limit(10);
        let b2 = b1.clone().add_iri("http://example.org/b");
        assert_eq!(b1.targets().len(), 1);
        assert_eq!(b2.targets().len(), 2);
    }

    #[test]
    fn test_debug_format() {
        let b = DescribeBuilder::new().add_iri("http://example.org/a");
        let debug = format!("{:?}", b);
        assert!(debug.contains("DescribeBuilder"));
    }

    // --- Query structure ---

    #[test]
    fn test_query_starts_with_describe() {
        let q = DescribeBuilder::new().build();
        assert!(q.starts_with("DESCRIBE"));
    }

    #[test]
    fn test_from_appears_before_where() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .with_from("http://example.org/g")
            .with_where("?x a <http://example.org/T>")
            .build();
        let from_pos = q.find("FROM").unwrap_or(usize::MAX);
        let where_pos = q.find("WHERE").unwrap_or(usize::MAX);
        assert!(from_pos < where_pos);
    }

    #[test]
    fn test_limit_appears_after_where() {
        let q = DescribeBuilder::new()
            .add_variable("x")
            .with_where("?x a <http://example.org/T>")
            .with_limit(5)
            .build();
        let where_pos = q.find("WHERE").unwrap_or(usize::MAX);
        let limit_pos = q.find("LIMIT").unwrap_or(usize::MAX);
        assert!(where_pos < limit_pos);
    }

    #[test]
    fn test_iri_targets_wrapped_in_angle_brackets() {
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/resource")
            .build();
        assert!(q.contains("<http://example.org/resource>"));
    }

    #[test]
    fn test_variable_targets_prefixed_with_question_mark() {
        let q = DescribeBuilder::new().add_variable("myVar").build();
        assert!(q.contains("?myVar"));
    }

    #[test]
    fn test_multiple_from_clauses_all_present() {
        let graphs = vec!["http://g1.org/", "http://g2.org/", "http://g3.org/"];
        let mut b = DescribeBuilder::new().add_iri("http://example.org/r");
        for g in &graphs {
            b = b.with_from(g);
        }
        let q = b.build();
        for g in &graphs {
            assert!(q.contains(g));
        }
    }

    #[test]
    fn test_describe_target_iri_equality() {
        let t1 = DescribeTarget::Iri("http://example.org/a".to_string());
        let t2 = DescribeTarget::Iri("http://example.org/a".to_string());
        assert_eq!(t1, t2);
    }

    #[test]
    fn test_describe_target_variable_equality() {
        let t1 = DescribeTarget::Variable("x".to_string());
        let t2 = DescribeTarget::Variable("x".to_string());
        assert_eq!(t1, t2);
    }

    #[test]
    fn test_describe_target_iri_ne_variable() {
        let t1 = DescribeTarget::Iri("x".to_string());
        let t2 = DescribeTarget::Variable("x".to_string());
        assert_ne!(t1, t2);
    }

    #[test]
    fn test_target_ordering_preserved() {
        let b = DescribeBuilder::new()
            .add_iri("http://example.org/first")
            .add_variable("second")
            .add_iri("http://example.org/third");
        let targets = b.targets();
        match &targets[0] {
            DescribeTarget::Iri(s) => assert_eq!(s, "http://example.org/first"),
            _ => panic!("Expected IRI at index 0"),
        }
        match &targets[1] {
            DescribeTarget::Variable(s) => assert_eq!(s, "second"),
            _ => panic!("Expected Variable at index 1"),
        }
    }

    #[test]
    fn test_chaining_is_fluent() {
        // Verify method chaining doesn't require intermediate bindings
        let q = DescribeBuilder::new()
            .add_iri("http://example.org/a")
            .add_variable("x")
            .with_from("http://example.org/g")
            .with_where("?x <http://schema.org/name> ?n")
            .with_limit(10)
            .build();
        assert!(q.contains("DESCRIBE"));
        assert!(q.contains("FROM"));
        assert!(q.contains("WHERE"));
        assert!(q.contains("LIMIT 10"));
    }
}