From 5022a095732ea0adcc1d79bf91da5e87077b2c67 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Champin Date: Thu, 9 Apr 2020 22:38:15 +0200 Subject: [PATCH] add destruct method to Literal and Quad This is (again) useful for building bridges to other APIs. Note that NamedNode already has this (into_string), that BlankNode kind of has it (id) and that Quad has incomplete versions of it (subject_owned, predicate_owned...). So I don't think those are too disruptive. --- lib/src/model/literal.rs | 11 +++++++++++ lib/src/model/triple.rs | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/lib/src/model/literal.rs b/lib/src/model/literal.rs index 8870e30c..7ac6143d 100644 --- a/lib/src/model/literal.rs +++ b/lib/src/model/literal.rs @@ -133,6 +133,17 @@ impl Literal { _ => false, } } + + /// Extract components from this literal + pub fn destruct(self) -> (String, Option, Option) { + match self.0 { + LiteralContent::String(s) => (s, None, None), + LiteralContent::LanguageTaggedString { value, language } => { + (value, None, Some(language)) + } + LiteralContent::TypedLiteral { value, datatype } => (value, Some(datatype), None), + } + } } impl fmt::Display for Literal { diff --git a/lib/src/model/triple.rs b/lib/src/model/triple.rs index ef58448e..1ec60799 100644 --- a/lib/src/model/triple.rs +++ b/lib/src/model/triple.rs @@ -287,6 +287,11 @@ impl Quad { pub fn into_triple(self) -> Triple { Triple::new(self.subject, self.predicate, self.object) } + + /// Extract components from this quad + pub fn destruct(self) -> (NamedOrBlankNode, NamedNode, Term, Option) { + (self.subject, self.predicate, self.object, self.graph_name) + } } impl fmt::Display for Quad {