Optimize some code, lints

* A few match simplifications
* Order trait impl to match trait itself
* A few unneeded parenthesis
* One dup code de-duplication with a new func
pull/768/head
Yuri Astrakhan 11 months ago committed by Thomas Tanon
parent 1e4326a2c5
commit 1e37577b71
  1. 8
      lib/oxrdf/src/dataset.rs
  2. 2
      lib/oxrdf/src/graph.rs
  3. 14
      lib/oxrdfxml/src/error.rs
  4. 9
      lib/oxsdatatypes/src/decimal.rs
  5. 2
      lib/oxttl/src/lexer.rs
  6. 4
      lib/oxttl/src/line_formats.rs
  7. 4
      lib/oxttl/src/n3.rs
  8. 4
      lib/oxttl/src/terse.rs
  9. 2
      lib/oxttl/src/toolkit/error.rs
  10. 14
      lib/sparesults/src/error.rs
  11. 2
      lib/sparesults/src/solution.rs
  12. 46
      lib/spargebra/src/parser.rs
  13. 2
      lib/src/sparql/eval.rs

@ -925,8 +925,8 @@ impl PartialEq for Dataset {
impl Eq for Dataset {}
impl<'a> IntoIterator for &'a Dataset {
type IntoIter = Iter<'a>;
type Item = QuadRef<'a>;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
@ -1283,8 +1283,8 @@ impl<'a> GraphView<'a> {
}
impl<'a> IntoIterator for GraphView<'a> {
type IntoIter = GraphViewIter<'a>;
type Item = TripleRef<'a>;
type IntoIter = GraphViewIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
@ -1292,8 +1292,8 @@ impl<'a> IntoIterator for GraphView<'a> {
}
impl<'a, 'b> IntoIterator for &'b GraphView<'a> {
type IntoIter = GraphViewIter<'a>;
type Item = TripleRef<'a>;
type IntoIter = GraphViewIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
@ -1494,8 +1494,8 @@ impl<'a, 'b, T: Into<TripleRef<'b>>> Extend<T> for GraphViewMut<'a> {
}
impl<'a> IntoIterator for &'a GraphViewMut<'a> {
type IntoIter = GraphViewIter<'a>;
type Item = TripleRef<'a>;
type IntoIter = GraphViewIter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()

@ -229,8 +229,8 @@ impl PartialEq for Graph {
impl Eq for Graph {}
impl<'a> IntoIterator for &'a Graph {
type IntoIter = Iter<'a>;
type Item = TripleRef<'a>;
type IntoIter = Iter<'a>;
fn into_iter(self) -> Self::IntoIter {
self.iter()

@ -61,10 +61,9 @@ impl From<quick_xml::Error> for ParseError {
#[inline]
fn from(error: quick_xml::Error) -> Self {
match error {
quick_xml::Error::Io(error) => Self::Io(match Arc::try_unwrap(error) {
Ok(error) => error,
Err(error) => io::Error::new(error.kind(), error),
}),
quick_xml::Error::Io(error) => {
Self::Io(Arc::try_unwrap(error).unwrap_or_else(|e| io::Error::new(e.kind(), e)))
}
_ => Self::Syntax(SyntaxError {
inner: SyntaxErrorKind::Xml(error),
}),
@ -137,10 +136,9 @@ impl From<SyntaxError> for io::Error {
fn from(error: SyntaxError) -> Self {
match error.inner {
SyntaxErrorKind::Xml(error) => match error {
quick_xml::Error::Io(error) => match Arc::try_unwrap(error) {
Ok(error) => error,
Err(error) => Self::new(error.kind(), error),
},
quick_xml::Error::Io(error) => {
Arc::try_unwrap(error).unwrap_or_else(|e| Self::new(e.kind(), e))
}
quick_xml::Error::UnexpectedEof(error) => {
Self::new(io::ErrorKind::UnexpectedEof, error)
}

@ -882,14 +882,16 @@ mod tests {
);
assert_eq!(Decimal::MAX.checked_round(), None);
assert_eq!(
(Decimal::MAX.checked_sub(Decimal::from_str("0.5")?))
Decimal::MAX
.checked_sub(Decimal::from_str("0.5")?)
.unwrap()
.checked_round(),
Some(Decimal::from_str("170141183460469231731")?)
);
assert_eq!(Decimal::MIN.checked_round(), None);
assert_eq!(
(Decimal::MIN.checked_add(Decimal::from_str("0.5")?))
Decimal::MIN
.checked_add(Decimal::from_str("0.5")?)
.unwrap()
.checked_round(),
Some(Decimal::from_str("-170141183460469231731")?)
@ -958,7 +960,8 @@ mod tests {
);
assert_eq!(Decimal::MIN.checked_floor(), None);
assert_eq!(
(Decimal::MIN.checked_add(Decimal::from_str("1")?))
Decimal::MIN
.checked_add(Decimal::from_str("1")?)
.unwrap()
.checked_floor(),
Some(Decimal::from_str("-170141183460469231731")?)

@ -49,8 +49,8 @@ pub struct N3Lexer {
// TODO: simplify by not giving is_end and fail with an "unexpected eof" is none is returned when is_end=true?
impl TokenRecognizer for N3Lexer {
type Options = N3LexerOptions;
type Token<'a> = N3Token<'a>;
type Options = N3LexerOptions;
fn recognize_next_token<'a>(
&mut self,

@ -39,9 +39,9 @@ enum NQuadsState {
}
impl RuleRecognizer for NQuadsRecognizer {
type Context = NQuadsRecognizerContext;
type Output = Quad;
type TokenRecognizer = N3Lexer;
type Output = Quad;
type Context = NQuadsRecognizerContext;
fn error_recovery_state(mut self) -> Self {
self.stack.clear();

@ -723,9 +723,9 @@ struct N3RecognizerContext {
}
impl RuleRecognizer for N3Recognizer {
type Context = N3RecognizerContext;
type Output = N3Quad;
type TokenRecognizer = N3Lexer;
type Output = N3Quad;
type Context = N3RecognizerContext;
fn error_recovery_state(mut self) -> Self {
self.stack.clear();

@ -35,9 +35,9 @@ impl TriGRecognizerContext {
}
impl RuleRecognizer for TriGRecognizer {
type Context = TriGRecognizerContext;
type Output = Quad;
type TokenRecognizer = N3Lexer;
type Output = Quad;
type Context = TriGRecognizerContext;
fn error_recovery_state(mut self) -> Self {
self.stack.clear();

@ -78,7 +78,7 @@ impl From<SyntaxError> for io::Error {
/// A parsing error.
///
/// It is the union of [`SyntaxError`] and [`std::io::Error`].
/// It is the union of [`SyntaxError`] and [`io::Error`].
#[derive(Debug)]
pub enum ParseError {
/// I/O error during parsing (file not found...).

@ -70,10 +70,9 @@ impl From<quick_xml::Error> for ParseError {
#[inline]
fn from(error: quick_xml::Error) -> Self {
match error {
quick_xml::Error::Io(error) => Self::Io(match Arc::try_unwrap(error) {
Ok(error) => error,
Err(error) => io::Error::new(error.kind(), error),
}),
quick_xml::Error::Io(error) => {
Self::Io(Arc::try_unwrap(error).unwrap_or_else(|e| io::Error::new(e.kind(), e)))
}
_ => Self::Syntax(SyntaxError {
inner: SyntaxErrorKind::Xml(error),
}),
@ -184,10 +183,9 @@ impl From<SyntaxError> for io::Error {
match error.inner {
SyntaxErrorKind::Json(error) => Self::new(io::ErrorKind::InvalidData, error),
SyntaxErrorKind::Xml(error) => match error {
quick_xml::Error::Io(error) => match Arc::try_unwrap(error) {
Ok(error) => error,
Err(error) => Self::new(error.kind(), error),
},
quick_xml::Error::Io(error) => {
Arc::try_unwrap(error).unwrap_or_else(|e| Self::new(e.kind(), e))
}
quick_xml::Error::UnexpectedEof(error) => {
Self::new(io::ErrorKind::UnexpectedEof, error)
}

@ -171,8 +171,8 @@ impl<V: Into<Arc<[Variable]>>, S: Into<Vec<Option<Term>>>> From<(V, S)> for Quer
}
impl<'a> IntoIterator for &'a QuerySolution {
type IntoIter = Iter<'a>;
type Item = (&'a Variable, &'a Term);
type IntoIter = Iter<'a>;
#[inline]
fn into_iter(self) -> Self::IntoIter {

@ -17,20 +17,7 @@ use std::{char, fmt};
/// Parses a SPARQL query with an optional base IRI to resolve relative IRIs in the query.
pub fn parse_query(query: &str, base_iri: Option<&str>) -> Result<Query, ParseError> {
let mut state = ParserState {
base_iri: if let Some(base_iri) = base_iri {
Some(Iri::parse(base_iri.to_owned()).map_err(|e| ParseError {
inner: ParseErrorKind::InvalidBaseIri(e),
})?)
} else {
None
},
namespaces: HashMap::default(),
used_bnodes: HashSet::default(),
currently_used_bnodes: HashSet::default(),
aggregates: Vec::new(),
};
let mut state = ParserState::from_base_iri(base_iri)?;
parser::QueryUnit(query, &mut state).map_err(|e| ParseError {
inner: ParseErrorKind::Parser(e),
})
@ -38,20 +25,7 @@ pub fn parse_query(query: &str, base_iri: Option<&str>) -> Result<Query, ParseEr
/// Parses a SPARQL update with an optional base IRI to resolve relative IRIs in the query.
pub fn parse_update(update: &str, base_iri: Option<&str>) -> Result<Update, ParseError> {
let mut state = ParserState {
base_iri: if let Some(base_iri) = base_iri {
Some(Iri::parse(base_iri.to_owned()).map_err(|e| ParseError {
inner: ParseErrorKind::InvalidBaseIri(e),
})?)
} else {
None
},
namespaces: HashMap::default(),
used_bnodes: HashSet::default(),
currently_used_bnodes: HashSet::default(),
aggregates: Vec::new(),
};
let mut state = ParserState::from_base_iri(base_iri)?;
let operations = parser::UpdateInit(update, &mut state).map_err(|e| ParseError {
inner: ParseErrorKind::Parser(e),
})?;
@ -720,6 +694,22 @@ pub struct ParserState {
}
impl ParserState {
pub(crate) fn from_base_iri(base_iri: Option<&str>) -> Result<Self, ParseError> {
Ok(Self {
base_iri: if let Some(base_iri) = base_iri {
Some(Iri::parse(base_iri.to_owned()).map_err(|e| ParseError {
inner: ParseErrorKind::InvalidBaseIri(e),
})?)
} else {
None
},
namespaces: HashMap::default(),
used_bnodes: HashSet::default(),
currently_used_bnodes: HashSet::default(),
aggregates: Vec::new(),
})
}
fn parse_iri(&self, iri: String) -> Result<Iri<String>, IriParseError> {
if let Some(base_iri) = &self.base_iri {
base_iri.resolve(&iri)

@ -111,8 +111,8 @@ impl EncodedTuple {
}
impl IntoIterator for EncodedTuple {
type IntoIter = std::vec::IntoIter<Option<EncodedTerm>>;
type Item = Option<EncodedTerm>;
type IntoIter = std::vec::IntoIter<Option<EncodedTerm>>;
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()

Loading…
Cancel
Save