From 1e37577b717a25e2b4db57d4f6dcb909a76fccd8 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Wed, 7 Feb 2024 00:49:16 -0500 Subject: [PATCH] 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 --- lib/oxrdf/src/dataset.rs | 8 +++--- lib/oxrdf/src/graph.rs | 2 +- lib/oxrdfxml/src/error.rs | 14 +++++----- lib/oxsdatatypes/src/decimal.rs | 9 ++++--- lib/oxttl/src/lexer.rs | 2 +- lib/oxttl/src/line_formats.rs | 4 +-- lib/oxttl/src/n3.rs | 4 +-- lib/oxttl/src/terse.rs | 4 +-- lib/oxttl/src/toolkit/error.rs | 2 +- lib/sparesults/src/error.rs | 14 +++++----- lib/sparesults/src/solution.rs | 2 +- lib/spargebra/src/parser.rs | 46 +++++++++++++-------------------- lib/src/sparql/eval.rs | 2 +- 13 files changed, 51 insertions(+), 62 deletions(-) diff --git a/lib/oxrdf/src/dataset.rs b/lib/oxrdf/src/dataset.rs index 8412a8aa..ed6249a4 100644 --- a/lib/oxrdf/src/dataset.rs +++ b/lib/oxrdf/src/dataset.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>> Extend 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() diff --git a/lib/oxrdf/src/graph.rs b/lib/oxrdf/src/graph.rs index 33f67132..5459b65c 100644 --- a/lib/oxrdf/src/graph.rs +++ b/lib/oxrdf/src/graph.rs @@ -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() diff --git a/lib/oxrdfxml/src/error.rs b/lib/oxrdfxml/src/error.rs index cb9eb9c4..be2e161d 100644 --- a/lib/oxrdfxml/src/error.rs +++ b/lib/oxrdfxml/src/error.rs @@ -61,10 +61,9 @@ impl From 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 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) } diff --git a/lib/oxsdatatypes/src/decimal.rs b/lib/oxsdatatypes/src/decimal.rs index b526e541..ee84b604 100644 --- a/lib/oxsdatatypes/src/decimal.rs +++ b/lib/oxsdatatypes/src/decimal.rs @@ -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")?) diff --git a/lib/oxttl/src/lexer.rs b/lib/oxttl/src/lexer.rs index 1eac849e..f60c230e 100644 --- a/lib/oxttl/src/lexer.rs +++ b/lib/oxttl/src/lexer.rs @@ -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, diff --git a/lib/oxttl/src/line_formats.rs b/lib/oxttl/src/line_formats.rs index 5932f7a2..e522bd53 100644 --- a/lib/oxttl/src/line_formats.rs +++ b/lib/oxttl/src/line_formats.rs @@ -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(); diff --git a/lib/oxttl/src/n3.rs b/lib/oxttl/src/n3.rs index 59ba9cf3..8b70a01e 100644 --- a/lib/oxttl/src/n3.rs +++ b/lib/oxttl/src/n3.rs @@ -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(); diff --git a/lib/oxttl/src/terse.rs b/lib/oxttl/src/terse.rs index ebeff436..c233c735 100644 --- a/lib/oxttl/src/terse.rs +++ b/lib/oxttl/src/terse.rs @@ -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(); diff --git a/lib/oxttl/src/toolkit/error.rs b/lib/oxttl/src/toolkit/error.rs index e279dab4..2f632352 100644 --- a/lib/oxttl/src/toolkit/error.rs +++ b/lib/oxttl/src/toolkit/error.rs @@ -78,7 +78,7 @@ impl From 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...). diff --git a/lib/sparesults/src/error.rs b/lib/sparesults/src/error.rs index b8fe2eff..8ece28d0 100644 --- a/lib/sparesults/src/error.rs +++ b/lib/sparesults/src/error.rs @@ -70,10 +70,9 @@ impl From 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 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) } diff --git a/lib/sparesults/src/solution.rs b/lib/sparesults/src/solution.rs index 826a9eea..a1364861 100644 --- a/lib/sparesults/src/solution.rs +++ b/lib/sparesults/src/solution.rs @@ -171,8 +171,8 @@ impl>, S: Into>>> 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 { diff --git a/lib/spargebra/src/parser.rs b/lib/spargebra/src/parser.rs index 03a71932..bcb4d491 100644 --- a/lib/spargebra/src/parser.rs +++ b/lib/spargebra/src/parser.rs @@ -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 { - 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) -> Result { - 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 { + 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, IriParseError> { if let Some(base_iri) = &self.base_iri { base_iri.resolve(&iri) diff --git a/lib/src/sparql/eval.rs b/lib/src/sparql/eval.rs index 19c6884d..174f41fa 100644 --- a/lib/src/sparql/eval.rs +++ b/lib/src/sparql/eval.rs @@ -111,8 +111,8 @@ impl EncodedTuple { } impl IntoIterator for EncodedTuple { - type IntoIter = std::vec::IntoIter>; type Item = Option; + type IntoIter = std::vec::IntoIter>; fn into_iter(self) -> Self::IntoIter { self.inner.into_iter()