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. 44
      lib/src/sparql/json_results.rs
  2. 27
      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/) //! 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::error::{invalid_data_error, invalid_input_error};
use crate::model::vocab::rdf;
use crate::model::*; use crate::model::*;
use crate::sparql::error::EvaluationError; use crate::sparql::error::EvaluationError;
use crate::sparql::model::*; use crate::sparql::model::*;
@ -351,7 +352,9 @@ impl<R: BufRead> ResultsIterator<R> {
state = None; state = None;
} }
Some(State::Datatype) => { 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; state = None;
} }
_ => (), // impossible _ => (), // impossible
@ -386,28 +389,25 @@ impl<R: BufRead> ResultsIterator<R> {
"uri serialization should have a 'value' key", "uri serialization should have a 'value' key",
) )
})?; })?;
Ok(match datatype { Ok(match lang {
Some(datatype) => Literal::new_typed_literal( Some(lang) => {
value, if let Some(datatype) = datatype {
NamedNode::new(datatype).map_err(|e| { if datatype.as_ref() != rdf::LANG_STRING {
invalid_data_error(format!( return Err(invalid_data_error(format!(
"Invalid datatype value: {}", "xml:lang value '{}' provided with the datatype {}",
e lang, datatype
)) )))
})?, }
),
None => match lang {
Some(lang) => {
Literal::new_language_tagged_literal(value, lang)
.map_err(|e| {
invalid_data_error(format!(
"Invalid xml:lang value: {}",
e
))
})?
} }
None => Literal::new_simple_literal(value), Literal::new_language_tagged_literal(value, &lang).map_err(|e| {
}, invalid_data_error(format!("Invalid xml:lang value '{}': {}", lang, e))
})?
}
None => if let Some(datatype) = datatype {
Literal::new_typed_literal(value, datatype)
} else {
Literal::new_simple_literal(value)
}
} }
.into()) .into())
} }

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

Loading…
Cancel
Save