Skip to content

Commit

Permalink
impl Composable, IsCompatibleWith<Term> for Term variants, Atom, Anno…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
turboladen committed Aug 19, 2024
1 parent abe36fb commit d9a383f
Show file tree
Hide file tree
Showing 22 changed files with 405 additions and 20 deletions.
14 changes: 13 additions & 1 deletion crates/api/src/annotation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use crate::Term;
use crate::{Composable, IsCompatibleWith, Term};

#[derive(Default, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Annotation(String);
Expand Down Expand Up @@ -32,3 +32,15 @@ where
Self(value.to_string())
}
}

impl Composable for Annotation {
fn composition(&self) -> crate::Composition {
crate::Composition::new_dimless()
}
}

impl IsCompatibleWith<Term> for Annotation {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition() && Some(self.as_str()) == rhs.annotation()
}
}
6 changes: 6 additions & 0 deletions crates/api/src/atom/is_compatible_with.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use crate::{Atom, Composable, Term};

impl crate::IsCompatibleWith for Atom {}

impl crate::IsCompatibleWith<Term> for Atom {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition() && rhs.annotation().is_none()
}
}
5 changes: 5 additions & 0 deletions crates/api/src/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ impl Composition {
&& self.temperature.is_none()
&& self.time.is_none()
}

#[must_use]
pub const fn is_dimless(&self) -> bool {
self.is_empty()
}
}

// ╭──────────────╮
Expand Down
24 changes: 23 additions & 1 deletion crates/api/src/term/is_compatible_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,29 @@ use super::annotation_composable::AnnotationComposable;
///
impl IsCompatibleWith for Term {
fn is_compatible_with(&self, rhs: &Self) -> bool {
self.composition() == rhs.composition() && self.annotation() == rhs.annotation()
match self {
Self::Annotation(inner) => inner.is_compatible_with(rhs),
Self::Atom(inner) => inner.is_compatible_with(rhs),
Self::AtomAnnotation(inner) => inner.is_compatible_with(rhs),
Self::AtomExponent(inner) => inner.is_compatible_with(rhs),
Self::AtomExponentAnnotation(inner) => inner.is_compatible_with(rhs),
Self::PrefixAtom(inner) => inner.is_compatible_with(rhs),
Self::PrefixAtomAnnotation(inner) => inner.is_compatible_with(rhs),
Self::PrefixAtomExponent(inner) => inner.is_compatible_with(rhs),
Self::PrefixAtomExponentAnnotation(inner) => inner.is_compatible_with(rhs),
Self::Factor(_) => rhs.composition().is_dimless() && rhs.annotation().is_none(),
Self::FactorAnnotation(inner) => inner.is_compatible_with(rhs),
Self::FactorExponent(inner) => inner.is_compatible_with(rhs),
Self::FactorExponentAnnotation(inner) => inner.is_compatible_with(rhs),
Self::FactorAtom(inner) => inner.is_compatible_with(rhs),
Self::FactorAtomAnnotation(inner) => inner.is_compatible_with(rhs),
Self::FactorAtomExponent(inner) => inner.is_compatible_with(rhs),
Self::FactorAtomExponentAnnotation(inner) => inner.is_compatible_with(rhs),
Self::FactorPrefixAtom(inner) => inner.is_compatible_with(rhs),
Self::FactorPrefixAtomAnnotation(inner) => inner.is_compatible_with(rhs),
Self::FactorPrefixAtomExponent(inner) => inner.is_compatible_with(rhs),
Self::FactorPrefixAtomExponentAnnotation(inner) => inner.is_compatible_with(rhs),
}
}
}

Expand Down
20 changes: 19 additions & 1 deletion crates/api/src/term/variants/atom_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, mem};

use num_traits::{Inv, Pow};

use crate::{composable::ComposablyEq, Atom};
use crate::{composable::ComposablyEq, Atom, Composable, IsCompatibleWith, UcumUnit};

use super::{
Annotation, AssignFactor, AtomExponentAnnotation, Exponent, Factor, FactorAtomAnnotation,
Expand Down Expand Up @@ -35,6 +35,11 @@ impl AtomAnnotation {
pub const fn annotation(&self) -> &Annotation {
&self.annotation
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
self.atom.scalar()
}
}

impl From<AtomAnnotation> for Term {
Expand Down Expand Up @@ -191,3 +196,16 @@ impl ComposablyEq for AtomAnnotation {
}
}
}

impl Composable for AtomAnnotation {
fn composition(&self) -> crate::Composition {
self.atom.composition()
}
}

impl IsCompatibleWith<Term> for AtomAnnotation {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition()
&& Some(self.annotation.as_str()) == rhs.annotation()
}
}
19 changes: 18 additions & 1 deletion crates/api/src/term/variants/atom_exponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use num_traits::{Inv, Pow};

use crate::{composable::ComposablyEq, Annotation, Atom};
use crate::{composable::ComposablyEq, Annotation, Atom, Composable, IsCompatibleWith, UcumUnit};

use super::{
AssignFactor, AtomExponentAnnotation, Exponent, Factor, FactorAtom, FactorAtomExponent,
Expand Down Expand Up @@ -35,6 +35,11 @@ impl AtomExponent {
pub const fn exponent(&self) -> i32 {
self.exponent
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
self.atom.scalar().powi(self.exponent)
}
}

impl From<AtomExponent> for Term {
Expand Down Expand Up @@ -165,3 +170,15 @@ impl ComposablyEq for AtomExponent {
}
}
}

impl Composable for AtomExponent {
fn composition(&self) -> crate::Composition {
self.atom.composition() * self.exponent
}
}

impl IsCompatibleWith<Term> for AtomExponent {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition() && rhs.annotation().is_none()
}
}
20 changes: 19 additions & 1 deletion crates/api/src/term/variants/atom_exponent_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, mem};

use num_traits::{Inv, Pow};

use crate::{composable::ComposablyEq, Atom};
use crate::{composable::ComposablyEq, Atom, Composable, IsCompatibleWith, UcumUnit};

use super::{
Annotation, AssignFactor, AtomAnnotation, Exponent, Factor, FactorAtomAnnotation,
Expand Down Expand Up @@ -45,6 +45,11 @@ impl AtomExponentAnnotation {
pub const fn annotation(&self) -> &Annotation {
&self.annotation
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
self.atom.scalar().powi(self.exponent)
}
}

impl From<AtomExponentAnnotation> for Term {
Expand Down Expand Up @@ -216,3 +221,16 @@ impl ComposablyEq for AtomExponentAnnotation {
}
}
}

impl Composable for AtomExponentAnnotation {
fn composition(&self) -> crate::Composition {
self.atom.composition() * self.exponent
}
}

impl IsCompatibleWith<Term> for AtomExponentAnnotation {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition()
&& Some(self.annotation.as_str()) == rhs.annotation()
}
}
20 changes: 19 additions & 1 deletion crates/api/src/term/variants/factor_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, mem};

use num_traits::{Inv, Pow};

use crate::composable::ComposablyEq;
use crate::{composable::ComposablyEq, Composable, IsCompatibleWith};

use super::{
Annotation, Exponent, Factor, FactorExponentAnnotation, PowOutput, SetAnnotation, SetExponent,
Expand Down Expand Up @@ -33,6 +33,11 @@ impl FactorAnnotation {
pub const fn annotation(&self) -> &Annotation {
&self.annotation
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
f64::from(self.factor)
}
}

impl From<FactorAnnotation> for Term {
Expand Down Expand Up @@ -168,3 +173,16 @@ impl ComposablyEq for FactorAnnotation {
}
}
}

impl Composable for FactorAnnotation {
fn composition(&self) -> crate::Composition {
crate::Composition::new_dimless()
}
}

impl IsCompatibleWith<Term> for FactorAnnotation {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition()
&& Some(self.annotation.as_str()) == rhs.annotation()
}
}
19 changes: 18 additions & 1 deletion crates/api/src/term/variants/factor_atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use num_traits::{Inv, Pow};

use crate::{composable::ComposablyEq, Annotation, Atom};
use crate::{composable::ComposablyEq, Annotation, Atom, Composable, IsCompatibleWith, UcumUnit};

use super::{
AtomExponent, Exponent, Factor, FactorAtomAnnotation, FactorAtomExponent, PowOutput,
Expand Down Expand Up @@ -33,6 +33,11 @@ impl FactorAtom {
pub const fn atom(&self) -> Atom {
self.atom
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
f64::from(self.factor) * self.atom.scalar()
}
}

impl From<FactorAtom> for Term {
Expand Down Expand Up @@ -147,3 +152,15 @@ impl ComposablyEq for FactorAtom {
}
}
}

impl Composable for FactorAtom {
fn composition(&self) -> crate::Composition {
self.atom.composition()
}
}

impl IsCompatibleWith<Term> for FactorAtom {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition() && rhs.annotation().is_none()
}
}
20 changes: 19 additions & 1 deletion crates/api/src/term/variants/factor_atom_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, mem};

use num_traits::{Inv, Pow};

use crate::{composable::ComposablyEq, Atom};
use crate::{composable::ComposablyEq, Atom, Composable, IsCompatibleWith, UcumUnit};

use super::{
Annotation, AtomAnnotation, AtomExponentAnnotation, Exponent, Factor,
Expand Down Expand Up @@ -43,6 +43,11 @@ impl FactorAtomAnnotation {
pub const fn annotation(&self) -> &Annotation {
&self.annotation
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
f64::from(self.factor) * self.atom.scalar()
}
}

impl From<FactorAtomAnnotation> for Term {
Expand Down Expand Up @@ -188,3 +193,16 @@ impl ComposablyEq for FactorAtomAnnotation {
}
}
}

impl Composable for FactorAtomAnnotation {
fn composition(&self) -> crate::Composition {
self.atom.composition()
}
}

impl IsCompatibleWith<Term> for FactorAtomAnnotation {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition()
&& Some(self.annotation.as_str()) == rhs.annotation()
}
}
19 changes: 18 additions & 1 deletion crates/api/src/term/variants/factor_atom_exponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use num_traits::{Inv, Pow};

use crate::{composable::ComposablyEq, Annotation, Atom};
use crate::{composable::ComposablyEq, Annotation, Atom, Composable, IsCompatibleWith, UcumUnit};

use super::{
AtomExponent, Exponent, Factor, FactorAtom, FactorAtomExponentAnnotation, InvOutput, PowOutput,
Expand Down Expand Up @@ -43,6 +43,11 @@ impl FactorAtomExponent {
pub const fn exponent(&self) -> Exponent {
self.exponent
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
f64::from(self.factor) * self.atom.scalar().powi(self.exponent)
}
}

impl From<FactorAtomExponent> for Term {
Expand Down Expand Up @@ -181,3 +186,15 @@ impl ComposablyEq for FactorAtomExponent {
}
}
}

impl Composable for FactorAtomExponent {
fn composition(&self) -> crate::Composition {
self.atom.composition() * self.exponent
}
}

impl IsCompatibleWith<Term> for FactorAtomExponent {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition() && rhs.annotation().is_none()
}
}
20 changes: 19 additions & 1 deletion crates/api/src/term/variants/factor_atom_exponent_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fmt, mem};

use num_traits::{Inv, Pow};

use crate::{composable::ComposablyEq, Atom};
use crate::{composable::ComposablyEq, Atom, Composable, IsCompatibleWith, UcumUnit};

use super::{
Annotation, AtomAnnotation, AtomExponentAnnotation, Exponent, Factor, FactorAtomAnnotation,
Expand Down Expand Up @@ -55,6 +55,11 @@ impl FactorAtomExponentAnnotation {
pub const fn annotation(&self) -> &Annotation {
&self.annotation
}

#[must_use]
pub fn to_scalar(&self) -> f64 {
f64::from(self.factor) * self.atom.scalar().powi(self.exponent)
}
}

impl From<FactorAtomExponentAnnotation> for Term {
Expand Down Expand Up @@ -244,3 +249,16 @@ impl ComposablyEq for FactorAtomExponentAnnotation {
}
}
}

impl Composable for FactorAtomExponentAnnotation {
fn composition(&self) -> crate::Composition {
self.atom.composition() * self.exponent
}
}

impl IsCompatibleWith<Term> for FactorAtomExponentAnnotation {
fn is_compatible_with(&self, rhs: &Term) -> bool {
self.composition() == rhs.composition()
&& Some(self.annotation.as_str()) == rhs.annotation()
}
}
Loading

0 comments on commit d9a383f

Please sign in to comment.