ran clippy and fmt

pull/13/head
Dustin Whitney 6 years ago
parent 75b6c22d37
commit 88773fc942
  1. 3
      lib/src/sparql/eval.rs
  2. 49
      lib/src/sparql/mod.rs
  3. 8
      lib/src/sparql/plan.rs
  4. 5
      lib/src/sparql/plan_builder.rs
  5. 44
      lib/tests/custom_functions_test_cases.rs
  6. 4
      lib/tests/service_test_cases.rs
  7. 1
      lib/tests/support/mod.rs

@ -1422,7 +1422,8 @@ impl<'a, S: StoreConnection + 'a> SimpleEvaluator<S> {
.and_then(|encoded| self.dataset.decode_term(encoded).ok())
})
.collect::<Vec<_>>();
self.custom_functions_handler.handle(name, &parameters)
self.custom_functions_handler
.handle(name, &parameters)
.and_then(|term| self.dataset.encoder().encode_term(&term).ok())
}
}

@ -9,7 +9,7 @@ mod plan;
mod plan_builder;
mod xml_results;
use crate::model::{NamedNode,Term};
use crate::model::{NamedNode, Term};
use crate::sparql::algebra::QueryVariants;
use crate::sparql::eval::SimpleEvaluator;
use crate::sparql::parser::read_sparql_query;
@ -70,7 +70,12 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler, options.custom_functions_handler),
evaluator: SimpleEvaluator::new(
dataset,
base_iri,
options.service_handler,
options.custom_functions_handler,
),
}
}
QueryVariants::Ask {
@ -81,7 +86,12 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Ask {
plan,
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler, options.custom_functions_handler),
evaluator: SimpleEvaluator::new(
dataset,
base_iri,
options.service_handler,
options.custom_functions_handler,
),
}
}
QueryVariants::Construct {
@ -98,7 +108,12 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
&construct,
variables,
)?,
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler, options.custom_functions_handler),
evaluator: SimpleEvaluator::new(
dataset,
base_iri,
options.service_handler,
options.custom_functions_handler,
),
}
}
QueryVariants::Describe {
@ -109,7 +124,12 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?;
SimplePreparedQueryAction::Describe {
plan,
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler, options.custom_functions_handler),
evaluator: SimpleEvaluator::new(
dataset,
base_iri,
options.service_handler,
options.custom_functions_handler,
),
}
}
}))
@ -131,7 +151,12 @@ impl<'a, S: StoreConnection + 'a> SimplePreparedQuery<S> {
Ok(Self(SimplePreparedQueryAction::Select {
plan,
variables,
evaluator: SimpleEvaluator::new(dataset, base_iri, options.service_handler, options.custom_functions_handler),
evaluator: SimpleEvaluator::new(
dataset,
base_iri,
options.service_handler,
options.custom_functions_handler,
),
}))
}
}
@ -185,7 +210,6 @@ impl ServiceHandler for EmptyServiceHandler {
}
}
/// Handler for custom functions.
///
/// For example, the following query uses a custom function to reverse a string:
@ -207,14 +231,14 @@ pub trait CustomFunctionsHandler {
/// and would recieve one parameter with the value mapped to ?name
///
/// Returns `None` if there is an error or the given NamedNode isn't recognized
fn handle(&self, node: &NamedNode, parameters: &Vec<Option<Term>>) -> Option<Term>;
fn handle(&self, node: &NamedNode, parameters: &[Option<Term>]) -> Option<Term>;
}
#[derive(Default)]
struct EmptyCustomFunctionsHandler {}
impl CustomFunctionsHandler for EmptyCustomFunctionsHandler {
fn handle(&self, _node: &NamedNode, _parameters: &Vec<Option<Term>>) -> Option<Term> {
fn handle(&self, _node: &NamedNode, _parameters: &[Option<Term>]) -> Option<Term> {
None
}
}
@ -258,12 +282,13 @@ impl<'a> QueryOptions<'a> {
}
/// Set a CustomFunctionHandler to add your own functions
pub fn with_custom_functions_handler(mut self, custom_functions_handler: Box<dyn CustomFunctionsHandler>) -> Self {
pub fn with_custom_functions_handler(
mut self,
custom_functions_handler: Box<dyn CustomFunctionsHandler>,
) -> Self {
self.custom_functions_handler = custom_functions_handler;
self
}
}
/// A parsed [SPARQL query](https://www.w3.org/TR/sparql11-query/)

@ -1,7 +1,7 @@
use crate::model::NamedNode;
use crate::sparql::GraphPattern;
use crate::sparql::model::Variable;
use crate::sparql::eval::StringOrStoreString;
use crate::sparql::model::Variable;
use crate::sparql::GraphPattern;
use crate::store::numeric_encoder::{
EncodedQuad, EncodedTerm, Encoder, MemoryStrStore, StrContainer, StrLookup,
ENCODED_DEFAULT_GRAPH,
@ -309,7 +309,7 @@ pub enum PlanExpression {
StringCast(Box<PlanExpression>),
CustomFunction {
name: NamedNode,
parameters: Vec<PlanExpression>
parameters: Vec<PlanExpression>,
},
}
@ -420,7 +420,7 @@ impl PlanExpression {
e.add_variables(set);
}
}
PlanExpression::CustomFunction{ parameters, .. } => {
PlanExpression::CustomFunction { parameters, .. } => {
for p in parameters {
p.add_variables(set);
}

@ -646,7 +646,10 @@ impl<E: Encoder> PlanBuilder<E> {
)?
} else {
let parameters = self.expression_list(parameters, variables, graph_name)?;
PlanExpression::CustomFunction { name: name.clone(), parameters }
PlanExpression::CustomFunction {
name: name.clone(),
parameters,
}
}
}
},

@ -6,11 +6,10 @@ use support::*;
#[test]
fn simple_custom_function_test() {
struct TestHandler;
impl CustomFunctionsHandler for TestHandler {
fn handle(&self, node: &NamedNode, parameters: &Vec<Option<Term>>) -> Option<Term> {
fn handle(&self, node: &NamedNode, parameters: &[Option<Term>]) -> Option<Term> {
let reverse = NamedNode::parse("http://example.com#REVERSE").ok()?;
if *node == reverse {
let param = &parameters[0];
@ -38,27 +37,35 @@ fn simple_custom_function_test() {
BIND(ex:REVERSE(?name) as ?reverse)
}
ORDER BY ?name
"#.to_string();
"#
.to_string();
let options = QueryOptions::default().with_custom_functions_handler(Box::new(TestHandler));
let triples = br#"
<http://example.com/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
<http://example.com/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
"#.as_ref();
"#
.as_ref();
let results = do_query(triples, query, options).unwrap();
let collected = results.into_values_iter().map(move |b| b.unwrap()).collect::<Vec<_>>();
let collected = results
.into_values_iter()
.map(move |b| b.unwrap())
.collect::<Vec<_>>();
let solution = vec![
vec![ Some(literal(String::from("Alice"))), Some(literal(String::from("ecilA"))) ],
vec![ Some(literal(String::from("Bob"))), Some(literal(String::from("boB"))) ],
vec![
Some(literal(String::from("Alice"))),
Some(literal(String::from("ecilA"))),
],
vec![
Some(literal(String::from("Bob"))),
Some(literal(String::from("boB"))),
],
];
assert_eq!(collected, solution);
}
#[test]
fn simple_default_custom_function_test() {
let query = r#"
PREFIX ex: <http://example.com#>
SELECT ?name ?reverse
@ -68,20 +75,23 @@ fn simple_default_custom_function_test() {
BIND(ex:REVERSE(?name) as ?reverse)
}
ORDER BY ?name
"#.to_string();
"#
.to_string();
let options = QueryOptions::default();
let triples = br#"
<http://example.com/bob> <http://xmlns.com/foaf/0.1/name> "Bob" .
<http://example.com/alice> <http://xmlns.com/foaf/0.1/name> "Alice" .
"#.as_ref();
"#
.as_ref();
let results = do_query(triples, query, options).unwrap();
let collected = results.into_values_iter().map(move |b| b.unwrap()).collect::<Vec<_>>();
let collected = results
.into_values_iter()
.map(move |b| b.unwrap())
.collect::<Vec<_>>();
let solution = vec![
vec![ Some(literal(String::from("Alice"))), None ],
vec![ Some(literal(String::from("Bob"))), None ],
vec![Some(literal(String::from("Alice"))), None],
vec![Some(literal(String::from("Bob"))), None],
];
assert_eq!(collected, solution);
}

@ -1,8 +1,6 @@
use failure::format_err;
use rudf::model::*;
use rudf::sparql::{
BindingsIterator, GraphPattern, QueryOptions, ServiceHandler,
};
use rudf::sparql::{BindingsIterator, GraphPattern, QueryOptions, ServiceHandler};
use rudf::Result;
mod support;

@ -6,7 +6,6 @@ use rudf::sparql::{BindingsIterator, GraphPattern, PreparedQuery, QueryOptions,
use rudf::{GraphSyntax, MemoryRepository, Repository, RepositoryConnection, Result};
use std::io::BufRead;
pub(crate) fn ex(id: String) -> Term {
Term::NamedNode(NamedNode::parse(format!("http://example.com/{}", &id)).unwrap())
}

Loading…
Cancel
Save