Page MenuHomeDevCentral
Paste P308

Transform sparql module from fantoir-datasource in standalone crate
ActivePublic

Authored by dereckson on Jan 14 2023, 19:02.
impl SparqlResults {
pub fn into_bool (self) -> Option<bool> {
match self {
SparqlResults::Solutions(_) => None,
SparqlResults::Boolean(bool) => Some(bool),
}
}
}
/* -------------------------------------------------------------
Tests
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#[cfg(test)]
mod tests {
use super::*;
#[test]
pub fn test_parse_boolean_results () {
let boolean_results = r#"
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head />
<boolean>true</boolean>
</sparql>
"#;
let results = parse_sparql_results(boolean_results);
let actual = results.into_bool();
assert!(actual.is_some());
assert!(actual.unwrap());
}
}

Event Timeline

To avoid match/let ... = ...() in the middle of a HOF chain, I've prepared helper methods to read the inner content of an enum:

A SparqlEnum result can be transformed into_bool (UPDATE, INSERT, ...) or into_solutions (SELECT).

Wikidata endpoint only supports SELECT requests, and our code only need to select, so into_bool is never read.

If we release a SPARQL client, we need this.