@@ -7,7 +7,7 @@ use std::fmt::Write;
7
7
use std:: io;
8
8
use std:: rc:: Rc ;
9
9
10
- use sophia_api:: dataset:: { DTerm , Dataset } ;
10
+ use sophia_api:: dataset:: { DTerm , SetDataset } ;
11
11
use sophia_api:: quad:: { iter_spog, Quad , Spog } ;
12
12
use sophia_api:: term:: { BnodeId , Term } ;
13
13
@@ -25,7 +25,7 @@ use crate::hash::{HashFunction, Sha256, Sha384};
25
25
/// - quads are sorted in codepoint order.
26
26
///
27
27
/// See also [`normalize_with`].
28
- pub fn normalize < D : Dataset , W : io:: Write > ( d : & D , w : W ) -> Result < ( ) , C14nError < D :: Error > > {
28
+ pub fn normalize < D : SetDataset , W : io:: Write > ( d : & D , w : W ) -> Result < ( ) , C14nError < D :: Error > > {
29
29
normalize_with :: < Sha256 , D , W > ( d, w, DEFAULT_DEPTH_FACTOR , DEFAULT_PERMUTATION_LIMIT )
30
30
}
31
31
@@ -37,7 +37,10 @@ pub fn normalize<D: Dataset, W: io::Write>(d: &D, w: W) -> Result<(), C14nError<
37
37
/// - quads are sorted in codepoint order.
38
38
///
39
39
/// See also [`normalize_with`].
40
- pub fn normalize_sha384 < D : Dataset , W : io:: Write > ( d : & D , w : W ) -> Result < ( ) , C14nError < D :: Error > > {
40
+ pub fn normalize_sha384 < D : SetDataset , W : io:: Write > (
41
+ d : & D ,
42
+ w : W ,
43
+ ) -> Result < ( ) , C14nError < D :: Error > > {
41
44
normalize_with :: < Sha384 , D , W > ( d, w, DEFAULT_DEPTH_FACTOR , DEFAULT_PERMUTATION_LIMIT )
42
45
}
43
46
@@ -49,7 +52,7 @@ pub fn normalize_sha384<D: Dataset, W: io::Write>(d: &D, w: W) -> Result<(), C14
49
52
/// - quads are sorted in codepoint order.
50
53
///
51
54
/// See also [`normalize`].
52
- pub fn normalize_with < H : HashFunction , D : Dataset , W : io:: Write > (
55
+ pub fn normalize_with < H : HashFunction , D : SetDataset , W : io:: Write > (
53
56
d : & D ,
54
57
mut w : W ,
55
58
depth_factor : f32 ,
@@ -61,7 +64,7 @@ pub fn normalize_with<H: HashFunction, D: Dataset, W: io::Write>(
61
64
// we sort the quads, but comparing the terms based on ther NQ serialization,
62
65
// which amounts to sorting the N-Quads lines without materializing them
63
66
quads. sort_unstable_by ( |q1, q2| {
64
- for ( t1, t2) in iter_spog ( q1. spog ( ) ) . zip ( iter_spog ( q2. spog ( ) ) ) {
67
+ for ( t1, t2) in iter_spog_opt ( q1. spog ( ) ) . zip ( iter_spog_opt ( q2. spog ( ) ) ) {
65
68
buf1. clear ( ) ;
66
69
buf2. clear ( ) ;
67
70
let o = cmp_c14n_terms ( t1, t2, & mut buf1, & mut buf2) ;
@@ -95,7 +98,7 @@ pub fn normalize_with<H: HashFunction, D: Dataset, W: io::Write>(
95
98
/// Implements <https://www.w3.org/TR/rdf-canon/#canon-algorithm>
96
99
///
97
100
/// See also [`normalize`].
98
- pub fn relabel < D : Dataset > ( d : & D ) -> Result < ( C14nQuads < D > , C14nIdMap ) , C14nError < D :: Error > > {
101
+ pub fn relabel < D : SetDataset > ( d : & D ) -> Result < ( C14nQuads < D > , C14nIdMap ) , C14nError < D :: Error > > {
99
102
relabel_with :: < Sha256 , D > ( d, DEFAULT_DEPTH_FACTOR , DEFAULT_PERMUTATION_LIMIT )
100
103
}
101
104
@@ -109,7 +112,9 @@ pub fn relabel<D: Dataset>(d: &D) -> Result<(C14nQuads<D>, C14nIdMap), C14nError
109
112
/// Implements <https://www.w3.org/TR/rdf-canon/#canon-algorithm>
110
113
///
111
114
/// See also [`normalize`].
112
- pub fn relabel_sha384 < D : Dataset > ( d : & D ) -> Result < ( C14nQuads < D > , C14nIdMap ) , C14nError < D :: Error > > {
115
+ pub fn relabel_sha384 < D : SetDataset > (
116
+ d : & D ,
117
+ ) -> Result < ( C14nQuads < D > , C14nIdMap ) , C14nError < D :: Error > > {
113
118
relabel_with :: < Sha384 , D > ( d, DEFAULT_DEPTH_FACTOR , DEFAULT_PERMUTATION_LIMIT )
114
119
}
115
120
@@ -135,7 +140,7 @@ pub fn relabel_sha384<D: Dataset>(d: &D) -> Result<(C14nQuads<D>, C14nIdMap), C1
135
140
/// Implements <https://www.w3.org/TR/rdf-canon/#canon-algorithm>
136
141
///
137
142
/// See also [`relabel`], [`normalize_with`].
138
- pub fn relabel_with < ' a , H : HashFunction , D : Dataset > (
143
+ pub fn relabel_with < ' a , H : HashFunction , D : SetDataset > (
139
144
d : & ' a D ,
140
145
depth_factor : f32 ,
141
146
permutation_limit : usize ,
@@ -497,6 +502,14 @@ fn smaller_path(path1: &str, path2: &str) -> bool {
497
502
}
498
503
}
499
504
505
+ /// Iter over all the components of a [`Quad`] as Option.
506
+ ///
507
+ /// Compared to [`iter_spog`], this function always return 4 components.
508
+ fn iter_spog_opt < T : Quad > ( q : T ) -> impl Iterator < Item = Option < T :: Term > > {
509
+ let ( spo, g) = q. to_spog ( ) ;
510
+ spo. into_iter ( ) . map ( Some ) . chain ( std:: iter:: once ( g) )
511
+ }
512
+
500
513
#[ cfg( test) ]
501
514
mod test {
502
515
use super :: * ;
@@ -701,15 +714,15 @@ _:c14n4 <http://example.com/#p> _:c14n3 .
701
714
assert ! ( got == exp) ;
702
715
}
703
716
704
- pub fn c14n_nquads < D : Dataset > ( d : & D ) -> Result < String , C14nError < D :: Error > > {
717
+ pub fn c14n_nquads < D : SetDataset > ( d : & D ) -> Result < String , C14nError < D :: Error > > {
705
718
let mut output = Vec :: < u8 > :: new ( ) ;
706
719
normalize ( d, & mut output) ?;
707
720
Ok ( unsafe { String :: from_utf8_unchecked ( output) } )
708
721
}
709
722
710
723
/// Simplisitic Quad parser, useful for writing test cases.
711
724
/// It is based on eq_quad below.
712
- fn ez_quads < ' a > ( lines : & [ & ' a str ] ) -> Vec < Spog < SimpleTerm < ' a > > > {
725
+ fn ez_quads < ' a > ( lines : & [ & ' a str ] ) -> std :: collections :: HashSet < Spog < SimpleTerm < ' a > > > {
713
726
lines. iter ( ) . map ( |line| ez_quad ( line) ) . collect ( )
714
727
}
715
728
0 commit comments