SPARQL result: allows both lang and datatype

As soon as datatype = rdf:langString
pull/171/head
Tpt 3 years ago
parent 5aeea55884
commit 7764f41d37
  1. 40
      lib/src/sparql/json_results.rs
  2. 25
      lib/src/sparql/xml_results.rs

@ -1,6 +1,7 @@
//! Implementation of [SPARQL Query Results JSON Format](https://www.w3.org/TR/sparql11-results-json/)
use crate::error::{invalid_data_error, invalid_input_error};
use crate::model::vocab::rdf;
use crate::model::*;
use crate::sparql::error::EvaluationError;
use crate::sparql::model::*;
@ -351,7 +352,9 @@ impl<R: BufRead> ResultsIterator<R> {
state = None;
}
Some(State::Datatype) => {
datatype = Some(s.to_owned());
datatype = Some(NamedNode::new(s).map_err(|e| {
invalid_data_error(format!("Invalid datatype value: {}", e))
})?);
state = None;
}
_ => (), // impossible
@ -386,28 +389,25 @@ impl<R: BufRead> ResultsIterator<R> {
"uri serialization should have a 'value' key",
)
})?;
Ok(match datatype {
Some(datatype) => Literal::new_typed_literal(
value,
NamedNode::new(datatype).map_err(|e| {
invalid_data_error(format!(
"Invalid datatype value: {}",
e
))
})?,
),
None => match lang {
Ok(match lang {
Some(lang) => {
Literal::new_language_tagged_literal(value, lang)
.map_err(|e| {
invalid_data_error(format!(
"Invalid xml:lang value: {}",
e
))
if let Some(datatype) = datatype {
if datatype.as_ref() != rdf::LANG_STRING {
return Err(invalid_data_error(format!(
"xml:lang value '{}' provided with the datatype {}",
lang, datatype
)))
}
}
Literal::new_language_tagged_literal(value, &lang).map_err(|e| {
invalid_data_error(format!("Invalid xml:lang value '{}': {}", lang, e))
})?
}
None => Literal::new_simple_literal(value),
},
None => if let Some(datatype) = datatype {
Literal::new_typed_literal(value, datatype)
} else {
Literal::new_simple_literal(value)
}
}
.into())
}

@ -1,6 +1,7 @@
//! Implementation of [SPARQL Query Results XML Format](http://www.w3.org/TR/rdf-sparql-XMLres/)
use crate::error::{invalid_data_error, invalid_input_error};
use crate::model::vocab::rdf;
use crate::model::*;
use crate::sparql::error::EvaluationError;
use crate::sparql::model::*;
@ -628,14 +629,26 @@ fn build_literal(
lang: Option<String>,
datatype: Option<NamedNode>,
) -> Result<Literal, EvaluationError> {
match datatype {
Some(datatype) => Ok(Literal::new_typed_literal(value, datatype)),
None => match lang {
Some(lang) => Literal::new_language_tagged_literal(value, &lang).map_err(|e| {
match lang {
Some(lang) => {
if let Some(datatype) = datatype {
if datatype.as_ref() != rdf::LANG_STRING {
return Err(invalid_data_error(format!(
"xml:lang value '{}' provided with the datatype {}",
lang, datatype
))
.into());
}
}
Literal::new_language_tagged_literal(value, &lang).map_err(|e| {
invalid_data_error(format!("Invalid xml:lang value '{}': {}", lang, e)).into()
})
}
None => Ok(if let Some(datatype) = datatype {
Literal::new_typed_literal(value, datatype)
} else {
Literal::new_simple_literal(value)
}),
None => Ok(Literal::new_simple_literal(value)),
},
}
}

Loading…
Cancel
Save