Skip to content

Commit

Permalink
clippies
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed Jun 24, 2024
1 parent 32af844 commit 89d1a33
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 146 deletions.
4 changes: 0 additions & 4 deletions crates/api/src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ use crate::Term;
pub struct Annotation(String);

impl Annotation {
pub(crate) const fn new(inner: String) -> Self {
Self(inner)
}

pub(crate) fn as_str(&self) -> &str {
&self.0
}
Expand Down
1 change: 0 additions & 1 deletion crates/api/src/atom/atom_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! These tests are for the dynamically generated `symbol_parser` module.
//!
#![cfg(test)]
use approx::{assert_relative_eq, assert_ulps_eq};

Expand Down
16 changes: 8 additions & 8 deletions crates/api/src/measurement/num_traits/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ mod tests {

#[test]
fn from_primitive_test() {
let output = Measurement::from_u32(std::u32::MAX).unwrap();
let output = Measurement::from_u32(u32::MAX).unwrap();
assert_eq!(
Measurement::new(std::u32::MAX.into(), crate::unit::UNITY),
Measurement::new(u32::MAX.into(), crate::unit::UNITY),
output
);

let output = Measurement::from_i32(std::i32::MAX).unwrap();
let output = Measurement::from_i32(i32::MAX).unwrap();
assert_eq!(
Measurement::new(std::i32::MAX.into(), crate::unit::UNITY),
Measurement::new(i32::MAX.into(), crate::unit::UNITY),
output
);
}
Expand All @@ -91,15 +91,15 @@ mod tests {
fn numcast_test() {
use num_traits::NumCast;

let output = <Measurement as NumCast>::from(std::u32::MAX).unwrap();
let output = <Measurement as NumCast>::from(u32::MAX).unwrap();
assert_eq!(
Measurement::new(std::u32::MAX.into(), crate::unit::UNITY),
Measurement::new(u32::MAX.into(), crate::unit::UNITY),
output
);

let output = <Measurement as NumCast>::from(std::i32::MIN).unwrap();
let output = <Measurement as NumCast>::from(i32::MIN).unwrap();
assert_eq!(
Measurement::new(std::i32::MIN.into(), crate::unit::UNITY),
Measurement::new(i32::MIN.into(), crate::unit::UNITY),
output
);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/api/src/measurement/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,9 @@ mod tests {

#[test]
fn validate_add_arbitrary() {
let term_tree = term!(annotation: "tree".to_string());
let term_tree2 = term!(annotation: "tree".to_string());
let expected_term_tree = term!(annotation: "tree".to_string());
let term_tree = term!(annotation: "tree");
let term_tree2 = term!(annotation: "tree");
let expected_term_tree = term!(annotation: "tree");
let m1 = Measurement::new(10.0, Unit::new(vec![term_tree]));
let m2 = Measurement::new(7.0, Unit::new(vec![term_tree2]));
let expected = Measurement::new(17.0, Unit::new(vec![expected_term_tree]));
Expand All @@ -449,9 +449,9 @@ mod tests {

#[test]
fn validate_sub_arbitrary() {
let term_tree = term!(annotation: "tree".to_string());
let term_tree2 = term!(annotation: "tree".to_string());
let expected_term_tree = term!(annotation: "tree".to_string());
let term_tree = term!(annotation: "tree");
let term_tree2 = term!(annotation: "tree");
let expected_term_tree = term!(annotation: "tree");
let m1 = Measurement::new(10.0, Unit::new(vec![term_tree]));
let m2 = Measurement::new(7.0, Unit::new(vec![term_tree2]));
let expected = Measurement::new(3.0, Unit::new(vec![expected_term_tree]));
Expand Down
35 changes: 17 additions & 18 deletions crates/api/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ pub use builder::Builder;

use crate::{Annotation, Atom, Prefix};

use self::variants::*;
use self::variants::{
AssignFactor, AtomAnnotation, AtomExponent, AtomExponentAnnotation, FactorAnnotation,
FactorAtom, FactorAtomAnnotation, FactorAtomExponent, FactorAtomExponentAnnotation,
FactorExponent, FactorExponentAnnotation, FactorPrefixAtom, FactorPrefixAtomAnnotation,
FactorPrefixAtomExponent, FactorPrefixAtomExponentAnnotation, PowOutput, PrefixAtom,
PrefixAtomAnnotation, PrefixAtomExponent, PrefixAtomExponentAnnotation, SetAnnotation,
SetExponent,
};

pub const UNITY: Term = Term::Factor(1);
pub const UNITY_ARRAY: [Term; 1] = [UNITY];
Expand Down Expand Up @@ -374,6 +381,7 @@ impl Term {
}
}

#[allow(clippy::too_many_lines)]
pub(crate) fn set_exponent(&mut self, exponent: Exponent) -> &mut Self {
match self {
Self::Annotation(annotation) => match annotation.set_exponent(exponent) {
Expand Down Expand Up @@ -1087,47 +1095,38 @@ mod tests {
assert_eq!(UNITY.as_cow_str(), "1");

// None | Some(1), None, None, None, Some(ann)
assert_eq!(term!(annotation: "hi".to_string()).as_cow_str(), "{hi}");
assert_eq!(
term!(factor: 1, annotation: "hi".to_string()).as_cow_str(),
"{hi}"
);
assert_eq!(term!(annotation: "hi").as_cow_str(), "{hi}");
assert_eq!(term!(factor: 1, annotation: "hi").as_cow_str(), "{hi}");

// None, None, Some(atom), None | Some(1), None
assert_eq!(term!(Meter).as_cow_str(), "m");
assert_eq!(term!(Meter, exponent: 1).as_cow_str(), "m");

// None, None, Some(atom), None | Some(1), Some(ann)
assert_eq!(term!(Meter, annotation: "hi").as_cow_str(), "m{hi}");
assert_eq!(
term!(Meter, annotation: "hi".to_string()).as_cow_str(),
"m{hi}"
);
assert_eq!(
term!(Meter, exponent: 1, annotation: "hi".to_string()).as_cow_str(),
term!(Meter, exponent: 1, annotation: "hi").as_cow_str(),
"m{hi}"
);

assert_eq!(term!(Meter, exponent: 2).as_cow_str(), "m2");
assert_eq!(term!(Meter, exponent: -1).as_cow_str(), "m-1");

assert_eq!(
term!(Meter, exponent: 2, annotation: "hi".to_string()).as_cow_str(),
term!(Meter, exponent: 2, annotation: "hi").as_cow_str(),
"m2{hi}"
);

assert_eq!(term!(Kilo, Meter).as_cow_str(), "km");

assert_eq!(
term!(Kilo, Meter, annotation: "hi".to_string()).as_cow_str(),
"km{hi}"
);
assert_eq!(term!(Kilo, Meter, annotation: "hi").as_cow_str(), "km{hi}");

assert_eq!(
term!(Kilo, Meter, exponent: 1, annotation: "hi".to_string()).as_cow_str(),
term!(Kilo, Meter, exponent: 1, annotation: "hi").as_cow_str(),
"km{hi}"
);
assert_eq!(
term!(Kilo, Meter, exponent: 2, annotation: "hi".to_string()).as_cow_str(),
term!(Kilo, Meter, exponent: 2, annotation: "hi").as_cow_str(),
"km2{hi}"
);
}
Expand Down
150 changes: 58 additions & 92 deletions crates/api/src/term/builder.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,74 @@
use crate::{Annotation, Atom, Prefix, Term};

use super::{variants::*, Exponent, Factor};
use super::{
variants::{
AtomAnnotation, AtomExponent, AtomExponentAnnotation, FactorAnnotation, FactorAtom,
FactorAtomAnnotation, FactorAtomExponent, FactorAtomExponentAnnotation, FactorExponent,
FactorExponentAnnotation, FactorPrefixAtom, FactorPrefixAtomAnnotation,
FactorPrefixAtomExponent, FactorPrefixAtomExponentAnnotation, PrefixAtom,
PrefixAtomAnnotation, PrefixAtomExponent, PrefixAtomExponentAnnotation,
},
Exponent, Factor,
};

#[derive(Default, Debug)]
pub struct Builder {
factor: Option<Factor>,
prefix: Option<Prefix>,
atom: Option<Atom>,
exponent: Option<Exponent>,
annotation: Option<String>,
annotation: Option<Annotation>,
}

impl Builder {
#[must_use]
pub fn factor(self, factor: Factor) -> Self {
Self {
factor: Some(factor),
..self
}
}

#[must_use]
pub fn prefix(self, prefix: Prefix) -> Self {
Self {
prefix: Some(prefix),
..self
}
}

#[must_use]
pub fn atom(self, atom: Atom) -> Self {
Self {
atom: Some(atom),
..self
}
}

#[must_use]
pub fn exponent(self, exponent: Exponent) -> Self {
Self {
exponent: Some(exponent),
..self
}
}

#[must_use]
pub fn annotation<T>(self, annotation: T) -> Self
where
T: ToString,
Annotation: From<T>,
{
Self {
annotation: Some(annotation.to_string()),
annotation: Some(Annotation::from(annotation)),
..self
}
}

/// # Panics
///
/// This will panic if it is called in such a way that would result in an invalid `Term`.
///
#[must_use]
pub fn build(self) -> Term {
match (
self.factor,
Expand All @@ -59,116 +82,59 @@ impl Builder {
| (None | Some(_), Some(_), None, None, Some(_))
| (Some(_), Some(_), None, None | Some(_), None)
| (Some(_), Some(_), None, Some(_), Some(_)) => panic!("Invalid term fields"),
(None, None, None, None, Some(annotation)) => Annotation::new(annotation).into(),
(None, None, None, None, Some(annotation)) => annotation.into(),
(None, None, Some(atom), None, None) => Term::Atom(atom),
(None, None, Some(atom), None, Some(annotation)) => AtomAnnotation {
atom,
annotation: Annotation::new(annotation),
(None, None, Some(atom), None, Some(annotation)) => {
AtomAnnotation::new(atom, annotation).into()
}
.into(),
(None, None, Some(atom), Some(exponent), None) => {
AtomExponent { atom, exponent }.into()
AtomExponent::new(atom, exponent).into()
}
(None, None, Some(atom), Some(exponent), Some(annotation)) => AtomExponentAnnotation {
atom,
exponent,
annotation: Annotation::new(annotation),
(None, None, Some(atom), Some(exponent), Some(annotation)) => {
AtomExponentAnnotation::new(atom, exponent, annotation).into()
}
.into(),
(None, Some(prefix), Some(atom), None, None) => PrefixAtom { prefix, atom }.into(),
(None, Some(prefix), Some(atom), None, Some(annotation)) => PrefixAtomAnnotation {
prefix,
atom,
annotation: Annotation::new(annotation),
}
.into(),
(None, Some(prefix), Some(atom), Some(exponent), None) => PrefixAtomExponent {
prefix,
atom,
exponent,
}
.into(),
(None, Some(prefix), Some(atom), None, Some(annotation)) => {
PrefixAtomAnnotation::new(prefix, atom, annotation).into()
}
(None, Some(prefix), Some(atom), Some(exponent), None) => {
PrefixAtomExponent::new(prefix, atom, exponent).into()
}
(None, Some(prefix), Some(atom), Some(exponent), Some(annotation)) => {
PrefixAtomExponentAnnotation {
prefix,
atom,
exponent,
annotation: Annotation::new(annotation),
}
.into()
PrefixAtomExponentAnnotation::new(prefix, atom, exponent, annotation).into()
}
(Some(factor), None, None, None, None) => Term::Factor(factor),
(Some(factor), None, None, None, Some(annotation)) => FactorAnnotation {
factor,
annotation: Annotation::new(annotation),
(Some(factor), None, None, None, Some(annotation)) => {
FactorAnnotation { factor, annotation }.into()
}
.into(),
(Some(factor), None, None, Some(exponent), None) => {
FactorExponent { factor, exponent }.into()
}
(Some(factor), None, None, Some(exponent), Some(annotation)) => {
FactorExponentAnnotation {
factor,
exponent,
annotation: Annotation::new(annotation),
}
.into()
FactorExponentAnnotation::new(factor, exponent, annotation).into()
}
(Some(factor), None, Some(atom), None, None) => FactorAtom { factor, atom }.into(),
(Some(factor), None, Some(atom), None, Some(annotation)) => FactorAtomAnnotation {
factor,
atom,
annotation: Annotation::new(annotation),
}
.into(),
(Some(factor), None, Some(atom), Some(exponent), None) => FactorAtomExponent {
factor,
atom,
exponent,
}
.into(),
(Some(factor), None, Some(atom), None, Some(annotation)) => {
FactorAtomAnnotation::new(factor, atom, annotation).into()
}
(Some(factor), None, Some(atom), Some(exponent), None) => {
FactorAtomExponent::new(factor, atom, exponent).into()
}
(Some(factor), None, Some(atom), Some(exponent), Some(annotation)) => {
FactorAtomExponentAnnotation {
factor,
atom,
exponent,
annotation: Annotation::new(annotation),
}
.into()
}
(Some(factor), Some(prefix), Some(atom), None, None) => FactorPrefixAtom {
factor,
prefix,
atom,
}
.into(),
FactorAtomExponentAnnotation::new(factor, atom, exponent, annotation).into()
}
(Some(factor), Some(prefix), Some(atom), None, None) => {
FactorPrefixAtom::new(factor, prefix, atom).into()
}
(Some(factor), Some(prefix), Some(atom), None, Some(annotation)) => {
FactorPrefixAtomAnnotation {
factor,
prefix,
atom,
annotation: Annotation::new(annotation),
}
.into()
FactorPrefixAtomAnnotation::new(factor, prefix, atom, annotation).into()
}
(Some(factor), Some(prefix), Some(atom), Some(exponent), None) => {
FactorPrefixAtomExponent {
factor,
prefix,
atom,
exponent,
}
.into()
FactorPrefixAtomExponent::new(factor, prefix, atom, exponent).into()
}
(Some(factor), Some(prefix), Some(atom), Some(exponent), Some(annotation)) => {
FactorPrefixAtomExponentAnnotation {
factor,
prefix,
atom,
exponent,
annotation: Annotation::new(annotation),
}
.into()
FactorPrefixAtomExponentAnnotation::new(factor, prefix, atom, exponent, annotation)
.into()
}
}
}
Expand Down
Loading

0 comments on commit 89d1a33

Please sign in to comment.