Skip to content

Commit 43759d4

Browse files
feat: add deserialisation of Vec
1 parent 58e1122 commit 43759d4

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

examples/deserialize.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ struct Foo {
1111
id: IriBuf,
1212
#[ld("ex:name")]
1313
name: String,
14+
#[ld("ex:email")]
15+
emails: Vec<String>,
1416
}
1517

1618
fn main() {
@@ -21,6 +23,7 @@ fn main() {
2123
let value_1 = Foo {
2224
id: id_1.clone(),
2325
name: "John Smith".to_string(),
26+
emails: vec!["john.smith@mail.me".to_string()],
2427
};
2528

2629
to_quads(generator::Blank::new(), &value_1)
@@ -30,7 +33,7 @@ fn main() {
3033
let quad = rdf_types::Quad(
3134
Term::Id(rdf_quad.0),
3235
Term::iri(rdf_quad.1),
33-
rdf_quad.2.into(),
36+
rdf_quad.2,
3437
rdf_quad.3.map(Term::Id),
3538
);
3639

@@ -42,6 +45,7 @@ fn main() {
4245
let value_2 = Foo {
4346
id: id_2.clone(),
4447
name: "Joe Dalton".to_string(),
48+
emails: vec!["joe.dalton@mail.me".to_string()],
4549
};
4650

4751
to_quads(generator::Blank::new(), &value_2)
@@ -51,7 +55,7 @@ fn main() {
5155
let quad = rdf_types::Quad(
5256
Term::Id(rdf_quad.0),
5357
Term::iri(rdf_quad.1),
54-
rdf_quad.2.into(),
58+
rdf_quad.2,
5559
rdf_quad.3.map(Term::Id),
5660
);
5761

src/predicate.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,36 @@ where
334334
}
335335
}
336336
}
337+
338+
impl<I: Interpretation, V: Vocabulary, T: LinkedDataDeserializeSubject<I, V>>
339+
LinkedDataDeserializePredicateObjects<I, V> for Vec<T>
340+
where
341+
I: ReverseIriInterpretation<Iri = V::Iri>,
342+
{
343+
fn deserialize_objects_in<'a, D>(
344+
vocabulary: &V,
345+
interpretation: &I,
346+
dataset: &D,
347+
graph: Option<&I::Resource>,
348+
objects: impl IntoIterator<Item = &'a I::Resource>,
349+
context: Context<I>,
350+
) -> Result<Self, FromLinkedDataError>
351+
where
352+
I::Resource: 'a,
353+
D: PatternMatchingDataset<Resource = I::Resource>,
354+
{
355+
objects
356+
.into_iter()
357+
.map(|object| {
358+
T::deserialize_subject_in(
359+
vocabulary,
360+
interpretation,
361+
dataset,
362+
graph,
363+
object,
364+
context,
365+
)
366+
})
367+
.collect::<Result<Vec<_>, _>>()
368+
}
369+
}

src/subject.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,13 @@ pub trait LinkedDataDeserializeSubject<I: Interpretation = (), V: Vocabulary = (
199199
)
200200
}
201201

202-
fn deserialize_subjects<D>(
202+
fn deserialize_subjects_in<D>(
203203
vocabulary: &V,
204204
interpretation: &I,
205205
dataset: &D,
206206
graph: Option<&I::Resource>,
207207
resources: impl IntoIterator<Item = I::Resource>,
208+
context: Context<I>,
208209
) -> Result<Vec<Self>, FromLinkedDataError>
209210
where
210211
D: PatternMatchingDataset<Resource = I::Resource>,
@@ -218,11 +219,31 @@ pub trait LinkedDataDeserializeSubject<I: Interpretation = (), V: Vocabulary = (
218219
dataset,
219220
graph,
220221
&resource,
221-
Context::default(),
222+
context,
222223
)
223224
})
224225
.collect::<Result<Vec<_>, _>>()
225226
}
227+
228+
fn deserialize_subjects<D>(
229+
vocabulary: &V,
230+
interpretation: &I,
231+
dataset: &D,
232+
graph: Option<&I::Resource>,
233+
resources: impl IntoIterator<Item = I::Resource>,
234+
) -> Result<Vec<Self>, FromLinkedDataError>
235+
where
236+
D: PatternMatchingDataset<Resource = I::Resource>,
237+
{
238+
Self::deserialize_subjects_in(
239+
vocabulary,
240+
interpretation,
241+
dataset,
242+
graph,
243+
resources,
244+
Context::default(),
245+
)
246+
}
226247
}
227248

228249
impl<I: Interpretation, V: Vocabulary> LinkedDataDeserializeSubject<I, V> for IriBuf

0 commit comments

Comments
 (0)