Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: apply non-default clippy fixes #180

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowed-wildcard-imports = [ "super" ]
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ url = "2.4.1"

[profile.release]
lto = true

[workspace.lints.clippy]
enum_glob_use = "allow"

2 changes: 1 addition & 1 deletion c14n/src/_c14n_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum C14nTerm<T: Term> {
Blank(BnodeId<Rc<str>>),
Other(T),
}
use C14nTerm::*;
use C14nTerm::{Blank, Other};

impl<T: Term> Term for C14nTerm<T> {
type BorrowTerm<'x> = &'x Self where Self: 'x;
Expand Down
4 changes: 2 additions & 2 deletions c14n/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl HashFunction for Sha256 {
type Output = [u8; 32];

fn initialize() -> Self {
Sha256(sha2::Sha256::new())
Self(sha2::Sha256::new())
}

fn update(&mut self, data: impl AsRef<[u8]>) {
Expand All @@ -42,7 +42,7 @@ impl HashFunction for Sha384 {
type Output = [u8; 48];

fn initialize() -> Self {
Sha384(sha2::Sha384::new())
Self(sha2::Sha384::new())
}

fn update(&mut self, data: impl AsRef<[u8]>) {
Expand Down
2 changes: 1 addition & 1 deletion c14n/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//!
//! TODO list:
//! - [x] check that UTF-8 byte-by-byte ordering is indeed equivalent to code point ordering.
//! - [ ] use c14n in sophia_isomorphism, replacing the current incomplete algorithm
//! - [ ] use c14n in `sophia_isomorphism`, replacing the current incomplete algorithm
//!
//! [Sophia]: https://docs.rs/sophia/latest/sophia/
//! [RDF]: https://www.w3.org/TR/rdf-primer/
Expand Down
66 changes: 33 additions & 33 deletions c14n/src/rdfc10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub fn relabel_with<'a, H: HashFunction, D: SetDataset>(
}
}
// Step 3
for (bnid, quads) in state.b2q.iter() {
for (bnid, quads) in &state.b2q {
let hash = hash_first_degree_quads::<H, _>(bnid, &quads[..]);
let bnid2 = Rc::clone(bnid);
state.h2b.entry(hash).or_default().push(bnid2);
Expand All @@ -187,7 +187,7 @@ pub fn relabel_with<'a, H: HashFunction, D: SetDataset>(
let mut next_h2b = BTreeMap::new();
// TODO once BTreeMap::drain_filter is stabilize,
// use it in the loop below instead of reinserting elements into a new map
for (hash, bnids) in state.h2b.into_iter() {
for (hash, bnids) in state.h2b {
debug_assert!(!bnids.is_empty());
if bnids.len() > 1 {
next_h2b.insert(hash, bnids);
Expand Down Expand Up @@ -254,7 +254,7 @@ struct C14nState<'a, H: HashFunction, T: Term> {
canonical: BnodeIssuer,
/// Not specified in the spec: memozing the results of hash 1st degree
b2h: BTreeMap<Rc<str>, H::Output>,
/// Not specified in the spec: maximum recursion factor in hash_n_degree_quads
/// Not specified in the spec: maximum recursion factor in `hash_n_degree_quads`
depth_factor: f32,
/// Not specified in the spec: maximum number of nodes on which permutations will be computed
permutation_limit: usize,
Expand All @@ -272,7 +272,7 @@ impl<'a, H: HashFunction, T: Term> C14nState<'a, H, T> {
}
}

/// Implements https://www.w3.org/TR/rdf-canon/#hash-related-blank-node
/// Implements <https://www.w3.org/TR/rdf-canon/#hash-related-blank-node>
fn hash_related_bnode(
&self,
related: &str,
Expand Down Expand Up @@ -301,7 +301,7 @@ impl<'a, H: HashFunction, T: Term> C14nState<'a, H, T> {
input.finalize()
}

/// Implements https://www.w3.org/TR/rdf-canon/#hash-nd-quads
/// Implements <https://www.w3.org/TR/rdf-canon/#hash-nd-quads>
fn hash_n_degree_quads<E: std::error::Error + Send + Sync + 'static>(
&self,
identifier: &str,
Expand Down Expand Up @@ -336,7 +336,7 @@ impl<'a, H: HashFunction, T: Term> C14nState<'a, H, T> {
let mut data_to_hash = H::initialize();
// Step 5
let mut ret_issuer: Option<BnodeIssuer> = None;
for (related_hash, mut blank_node) in hn.into_iter() {
for (related_hash, mut blank_node) in hn {
data_to_hash.update(hex(&related_hash));
let mut chosen_path = String::new();
let mut chosen_issuer: Option<BnodeIssuer> = None;
Expand Down Expand Up @@ -420,15 +420,15 @@ struct BnodeIssuer {
}

impl BnodeIssuer {
fn new(prefix: BnodeId<&'static str>) -> Self {
BnodeIssuer {
const fn new(prefix: BnodeId<&'static str>) -> Self {
Self {
prefix,
issued: BTreeMap::new(),
issued_order: vec![],
}
}

/// Implements https://www.w3.org/TR/rdf-canon/#issue-identifier
/// Implements <https://www.w3.org/TR/rdf-canon/#issue-identifier>
/// modified to also return a boolean indicating whether the issued identifier
/// was newly created (true) or if it existed before (false)
fn issue(&mut self, bnid: &str) -> (&str, bool) {
Expand All @@ -445,7 +445,7 @@ impl BnodeIssuer {
}
}

/// Implements https://www.w3.org/TR/rdf-canon/#hash-1d-quads
/// Implements <https://www.w3.org/TR/rdf-canon/#hash-1d-quads>
/// with the difference that the C14n state is not passed;
/// instead, the quad list corresponding to bnid is passed directly
fn hash_first_degree_quads<H: HashFunction, Q: Quad>(bnid: &str, quads: &[&Q]) -> H::Output {
Expand All @@ -465,7 +465,7 @@ fn hash_first_degree_quads<H: HashFunction, Q: Quad>(bnid: &str, quads: &[&Q]) -
.collect();
nquads.sort_unstable();
let mut hasher = H::initialize();
for line in nquads.into_iter() {
for line in nquads {
hasher.update(&line);
}
let ret = hasher.finalize();
Expand All @@ -491,13 +491,13 @@ fn nq_for_hash<T: Term>(term: T, buffer: &mut String, ref_bnid: &str) {
fn hex(hash: &impl AsRef<[u8]>) -> String {
let mut digest = String::with_capacity(64);
for b in hash.as_ref() {
write!(&mut digest, "{:02x}", b).unwrap();
write!(&mut digest, "{b:02x}").unwrap();
}
digest
}

fn smaller_path(path1: &str, path2: &str) -> bool {
use std::cmp::Ordering::*;
use std::cmp::Ordering::{Equal, Greater, Less};
match Ord::cmp(&path1.len(), &path2.len()) {
Less => true,
Equal => path1 < path2,
Expand Down Expand Up @@ -528,13 +528,13 @@ mod test {
"_:e0 <http://example.com/#s> <http://example.com/#u> .",
"_:e1 <http://example.com/#t> <http://example.com/#u> .",
]);
let exp = r#"<http://example.com/#p> <http://example.com/#q> _:c14n0 .
let exp = r"<http://example.com/#p> <http://example.com/#q> _:c14n0 .
<http://example.com/#p> <http://example.com/#r> _:c14n1 .
_:c14n0 <http://example.com/#s> <http://example.com/#u> .
_:c14n1 <http://example.com/#t> <http://example.com/#u> .
"#;
";
let got = c14n_nquads(&dataset).unwrap();
println!(">>>> GOT\n{}>>>> EXPECTED\n{}<<<<", got, exp);
println!(">>>> GOT\n{got}>>>> EXPECTED\n{exp}<<<<");
assert!(got == exp);
}

Expand All @@ -549,14 +549,14 @@ _:c14n1 <http://example.com/#t> <http://example.com/#u> .
"_:e1 <http://example.com/#p> _:e3 .",
"_:e2 <http://example.com/#r> _:e3 .",
]);
let exp = r#"<http://example.com/#p> <http://example.com/#q> _:c14n2 .
let exp = r"<http://example.com/#p> <http://example.com/#q> _:c14n2 .
<http://example.com/#p> <http://example.com/#q> _:c14n3 .
_:c14n0 <http://example.com/#r> _:c14n1 .
_:c14n2 <http://example.com/#p> _:c14n1 .
_:c14n3 <http://example.com/#p> _:c14n0 .
"#;
";
let got = c14n_nquads(&dataset).unwrap();
println!(">>>> GOT\n{}>>>> EXPECTED\n{}<<<<", got, exp);
println!(">>>> GOT\n{got}>>>> EXPECTED\n{exp}<<<<");
assert!(got == exp);
}

Expand All @@ -571,14 +571,14 @@ _:c14n3 <http://example.com/#p> _:c14n0 .
"_:e3 <http://example.com/#p> _:e4 .",
"_:e4 <http://example.com/#p> _:e0 .",
]);
let exp = r#"_:c14n0 <http://example.com/#p> _:c14n4 .
let exp = r"_:c14n0 <http://example.com/#p> _:c14n4 .
_:c14n1 <http://example.com/#p> _:c14n0 .
_:c14n2 <http://example.com/#p> _:c14n1 .
_:c14n3 <http://example.com/#p> _:c14n2 .
_:c14n4 <http://example.com/#p> _:c14n3 .
"#;
";
let got = c14n_nquads(&dataset).unwrap();
println!(">>>> GOT\n{}>>>> EXPECTED\n{}<<<<", got, exp);
println!(">>>> GOT\n{got}>>>> EXPECTED\n{exp}<<<<");
assert!(got == exp);
}

Expand Down Expand Up @@ -630,7 +630,7 @@ _:c14n4 <http://example.com/#p> _:c14n3 .
"_:e4 <http://example.com/#p> _:e2 .",
"_:e4 <http://example.com/#p> _:e3 .",
]);
let exp = r#"_:c14n0 <http://example.com/#p> _:c14n1 .
let exp = r"_:c14n0 <http://example.com/#p> _:c14n1 .
_:c14n0 <http://example.com/#p> _:c14n2 .
_:c14n0 <http://example.com/#p> _:c14n3 .
_:c14n0 <http://example.com/#p> _:c14n4 .
Expand All @@ -650,9 +650,9 @@ _:c14n4 <http://example.com/#p> _:c14n0 .
_:c14n4 <http://example.com/#p> _:c14n1 .
_:c14n4 <http://example.com/#p> _:c14n2 .
_:c14n4 <http://example.com/#p> _:c14n3 .
"#;
";
let got = c14n_nquads(&dataset).unwrap();
println!(">>>> GOT\n{}>>>> EXPECTED\n{}<<<<", got, exp);
println!(">>>> GOT\n{got}>>>> EXPECTED\n{exp}<<<<");
assert!(got == exp);
}

Expand Down Expand Up @@ -700,14 +700,14 @@ _:c14n4 <http://example.com/#p> _:c14n3 .
"_:e3 <http://example.com/#p> _:e4 .",
"_:e4 <http://example.com/#p> _:e2 .",
]);
let exp = r#"_:c14n0 <http://example.com/#p> _:c14n1 .
let exp = r"_:c14n0 <http://example.com/#p> _:c14n1 .
_:c14n1 <http://example.com/#p> _:c14n0 .
_:c14n2 <http://example.com/#p> _:c14n4 .
_:c14n3 <http://example.com/#p> _:c14n2 .
_:c14n4 <http://example.com/#p> _:c14n3 .
"#;
";
let got = c14n_nquads(&dataset).unwrap();
println!(">>>> GOT\n{}>>>> EXPECTED\n{}<<<<", got, exp);
println!(">>>> GOT\n{got}>>>> EXPECTED\n{exp}<<<<");
assert!(got == exp);
}

Expand All @@ -729,7 +729,7 @@ _:c14n4 <http://example.com/#p> _:c14n3 .
<tag:a> <tag:p> _:c14n0 .
"#;
let got = c14n_nquads(&dataset).unwrap();
println!(">>>> GOT\n{}>>>> EXPECTED\n{}<<<<", got, exp);
println!(">>>> GOT\n{got}>>>> EXPECTED\n{exp}<<<<");
assert!(got == exp);
}

Expand All @@ -740,7 +740,7 @@ _:c14n4 <http://example.com/#p> _:c14n3 .
}

/// Simplistic Quad parser, useful for writing test cases.
/// It is based on eq_quad below.
/// It is based on `eq_quad` below.
fn ez_quads<'a>(lines: &[&'a str]) -> std::collections::HashSet<Spog<SimpleTerm<'a>>> {
lines.iter().map(|line| ez_quad(line)).collect()
}
Expand Down Expand Up @@ -804,15 +804,15 @@ _:c14n4 <http://example.com/#p> _:c14n3 .
"_:e0 <http://example.com/#s> <http://example.com/#u> .",
"_:e1 <http://example.com/#t> <http://example.com/#u> .",
]);
let exp = r#"<http://example.com/#p> <http://example.com/#q> _:c14n1 .
let exp = r"<http://example.com/#p> <http://example.com/#q> _:c14n1 .
<http://example.com/#p> <http://example.com/#r> _:c14n0 .
_:c14n0 <http://example.com/#t> <http://example.com/#u> .
_:c14n1 <http://example.com/#s> <http://example.com/#u> .
"#;
";
let mut got = Vec::<u8>::new();
normalize_sha384(&dataset, &mut got).unwrap();
let got = unsafe { String::from_utf8_unchecked(got) };
println!(">>>> GOT\n{}>>>> EXPECTED\n{}<<<<", got, exp);
println!(">>>> GOT\n{got}>>>> EXPECTED\n{exp}<<<<");
assert!(got == exp);
}
}
12 changes: 6 additions & 6 deletions iri/benches/bench1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! This benchmark is used to compare the time it takes to create
//! * borrowing MownStr's vs. standard &str references
//! * owning MownStr's vs. Strings
//! * borrowing `MownStr`'s vs. standard &str references
//! * owning `MownStr`'s vs. Strings
//!
//! The results of `borrowed_mownstr` should therefore be compared to `refs`,
//! and that of `owned_mownstr` should be compared to `strings`.

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use sophia_iri::resolve::*;
use sophia_iri::resolve::{BaseIri, BaseIriRef};

fn parse(c: &mut Criterion) {
c.bench_with_input(
Expand All @@ -25,7 +25,7 @@ fn parse(c: &mut Criterion) {
black_box(BaseIriRef::new(*iri).is_ok());
}
}
})
});
},
);
}
Expand All @@ -42,7 +42,7 @@ fn resolve_from_scratch(c: &mut Criterion) {
black_box(&base.resolve(*rel).unwrap());
}
}
})
});
},
);
}
Expand All @@ -58,7 +58,7 @@ fn resolve_mutualized(c: &mut Criterion) {
black_box(i.0.resolve(*rel).unwrap());
}
}
})
});
},
);
}
Expand Down
4 changes: 4 additions & 0 deletions iri/src/_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use regex::Regex;
/// is not `None`.
/// Future implementations may be smarter about this.
#[inline]
#[must_use]
pub fn is_valid_suffixed_iri_ref(ns: &str, suffix: Option<&str>) -> bool {
match suffix {
None => is_valid_iri_ref(ns),
Expand All @@ -22,18 +23,21 @@ pub fn is_valid_suffixed_iri_ref(ns: &str, suffix: Option<&str>) -> bool {

/// Check whether `txt` is a valid (absolute or relative) IRI reference.
#[inline]
#[must_use]
pub fn is_valid_iri_ref(txt: &str) -> bool {
IRI_REGEX.is_match(txt) || IRELATIVE_REF_REGEX.is_match(txt)
}

/// Check whether `txt` is an absolute IRI reference.
#[inline]
#[must_use]
pub fn is_absolute_iri_ref(txt: &str) -> bool {
IRI_REGEX.is_match(txt)
}

/// Check whether `txt` is a relative IRI reference.
#[inline]
#[must_use]
pub fn is_relative_iri_ref(txt: &str) -> bool {
IRELATIVE_REF_REGEX.is_match(txt)
}
Expand Down
6 changes: 3 additions & 3 deletions iri/src/_serde.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use super::{Iri, IriRef, Result};
use serde::{
de::{Error, Unexpected},
Deserialize, Serialize,
Expand All @@ -11,7 +11,7 @@ impl<'a, T: Borrow<str> + Deserialize<'a>> Deserialize<'a> for Iri<T> {
D: serde::Deserializer<'a>,
{
let inner: T = T::deserialize(deserializer)?;
Iri::new(inner)
Self::new(inner)
.map_err(|err| D::Error::invalid_value(Unexpected::Str(&err.0), &"valid IRI"))
}
}
Expand All @@ -31,7 +31,7 @@ impl<'a, T: Borrow<str> + Deserialize<'a>> Deserialize<'a> for IriRef<T> {
D: serde::Deserializer<'a>,
{
let inner: T = T::deserialize(deserializer)?;
IriRef::new(inner)
Self::new(inner)
.map_err(|err| D::Error::invalid_value(Unexpected::Str(&err.0), &"valid IRI reference"))
}
}
Expand Down
4 changes: 2 additions & 2 deletions iri/src/_wrap_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ macro_rules! wrap {
"If it is not, it may result in undefined behaviour.",
)]
#[allow(dead_code)]
pub const fn new_unchecked_const(inner: &'static $bid) -> Self {
#[must_use] pub const fn new_unchecked_const(inner: &'static $bid) -> Self {
$wid(inner)
}
}
Expand Down Expand Up @@ -415,7 +415,7 @@ pub mod test_wrap_borrowing {
// only check that this compiles
#[allow(dead_code)]
fn new_unchecked() {
let _: Foo<String> = Foo::new_unchecked("".into());
let _: Foo<String> = Foo::new_unchecked(String::new());
}

// only check that this compiles
Expand Down
Loading