Skip to content

Commit

Permalink
Merge branch 'develop' into feature/PLCC-299-parser-performance
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed Mar 26, 2024
2 parents aa4ed9f + 54951ce commit 8d1d073
Show file tree
Hide file tree
Showing 30 changed files with 2,101 additions and 663 deletions.
6 changes: 4 additions & 2 deletions crates/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Added `unit` constant: `UNITY`
- Added `term` constants: `UNITY`, `UNITY_ARRAY`, and `UNITY_ARRAY_REF`.
- Added `measurement!()` macro for wrapping `Measurement::try_new().unwrap()`.
- Added `crate::parser::term::Factor` type alias for `u32`.
- Added `crate::parser::term::Exponent` type alias for `i32`.
- Added `crate::term::Factor` type alias for `u32`.
- Added `crate::term::Exponent` type alias for `i32`.
- (Internal) Added constants for many but not all internal `Definition`s.

### Changed

- Rust minimum version updated to `1.64.0`.
- (Internal) Rust Codegen is now using `quote` instead of `handlebars` (the pest parser grammar is
still generated using `handlebars`).
- (Internal) `Definition` is now an enum and is generic over value type `V`.
- (Internal) `Definition` resolution uses `uncreachable!()` for all `Definition` unit strings.

### Deprecated

Expand Down
765 changes: 322 additions & 443 deletions crates/api/src/atom.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use approx::{assert_relative_eq, assert_ulps_eq};

use crate::{
term::UNITY_ARRAY, Atom, Classification, Composable, Composition, Dimension, UcumSymbol,
UcumUnit,
term::UNITY_ARRAY, ucum_unit::UcumUnit, Atom, Classification, Composable, Composition,
Dimension, UcumSymbol,
};

macro_rules! validate_definition {
Expand Down
48 changes: 25 additions & 23 deletions crates/api/src/atom/definition.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(clippy::large_enum_variant)]
#![allow(clippy::result_large_err)]

pub(super) mod consts;

use std::{borrow::Cow, str::FromStr};

use num_traits::One;
Expand All @@ -15,25 +17,25 @@ use super::function_set::FunctionSet;
///
pub(crate) enum Definition<V> {
Base,
NonDimensional(V),
NonDimensionalSpecial {
Value(V),
ValueSpecial {
value: V,
function_set: FunctionSet<V>,
},
Dimensional {
ValueTerms {
value: V,
terms: Cow<'static, [Term]>,
},
DimensionalSpecial {
ValueTermsSpecial {
value: V,
terms: Cow<'static, [Term]>,
function_set: FunctionSet<V>,
},
}

impl<V> Definition<V> {
pub(crate) fn new_dimensional(value: V, expression: &'static str) -> Self {
Self::Dimensional {
pub(crate) fn new_value_terms(value: V, expression: &'static str) -> Self {
Self::ValueTerms {
value,
terms: crate::Unit::from_str(expression).map_or_else(
|_| unreachable!("expected valid unit definition string: {expression}"),
Expand All @@ -42,12 +44,12 @@ impl<V> Definition<V> {
}
}

pub(crate) fn new_dimensional_special(
pub(crate) fn new_value_terms_special(
value: V,
expression: &'static str,
function_set: FunctionSet<V>,
) -> Self {
Self::DimensionalSpecial {
Self::ValueTermsSpecial {
value,
terms: crate::Unit::from_str(expression).map_or_else(
|_| unreachable!("expected valid unit definition string: {expression}"),
Expand All @@ -63,20 +65,19 @@ impl<V> Definition<V> {
{
match self {
Self::Base => <V as One>::one(),
Self::NonDimensional(value) => value.clone(),
Self::NonDimensionalSpecial { value, .. }
| Self::Dimensional { value, .. }
| Self::DimensionalSpecial { value, .. } => (*value).clone(),
Self::Value(value) => value.clone(),
Self::ValueSpecial { value, .. }
| Self::ValueTerms { value, .. }
| Self::ValueTermsSpecial { value, .. } => (*value).clone(),
}
}

pub(crate) const fn terms(&self) -> &Cow<'static, [Term]> {
match self {
Self::Base => &Cow::Borrowed(term::UNITY_ARRAY_REF),
Self::NonDimensional(_) | Self::NonDimensionalSpecial { .. } => {
Self::Value(_) | Self::ValueSpecial { .. } | Self::Base => {
&Cow::Borrowed(term::UNITY_ARRAY_REF)
}
Self::Dimensional { terms, .. } | Self::DimensionalSpecial { terms, .. } => terms,
Self::ValueTerms { terms, .. } | Self::ValueTermsSpecial { terms, .. } => terms,
}
}
}
Expand All @@ -85,10 +86,10 @@ impl Reducible<f64> for Definition<f64> {
fn reduce_value(&self, other_value: f64) -> f64 {
match self {
Self::Base => One::one(),
Self::NonDimensional(value) => *value,
Self::Dimensional { value, terms } => value * terms.reduce_value(other_value),
Self::NonDimensionalSpecial { function_set, .. }
| Self::DimensionalSpecial { function_set, .. } => {
Self::Value(value) => *value,
Self::ValueTerms { value, terms } => value * terms.reduce_value(other_value),
Self::ValueSpecial { function_set, .. }
| Self::ValueTermsSpecial { function_set, .. } => {
(function_set.convert_to)(other_value)
}
}
Expand All @@ -97,12 +98,13 @@ impl Reducible<f64> for Definition<f64> {
fn calculate_magnitude(&self, other_value: f64) -> f64 {
match self {
Self::Base => One::one(),
Self::NonDimensional(value) => *value,
Self::Dimensional { value, terms } => value * terms.calculate_magnitude(other_value),
Self::NonDimensionalSpecial { function_set, .. }
| Self::DimensionalSpecial { function_set, .. } => {
Self::Value(value) => *value,
Self::ValueTerms { value, terms } => value * terms.calculate_magnitude(other_value),
Self::ValueSpecial { function_set, .. }
| Self::ValueTermsSpecial { function_set, .. } => {
(function_set.convert_from)(other_value)
}
}
}
}

Loading

0 comments on commit 8d1d073

Please sign in to comment.