Skip to content

Commit

Permalink
Add SetAnnotation
Browse files Browse the repository at this point in the history
  • Loading branch information
turboladen committed Jun 23, 2024
1 parent 6abdec7 commit 32af844
Show file tree
Hide file tree
Showing 24 changed files with 372 additions and 291 deletions.
55 changes: 55 additions & 0 deletions crates/api/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,61 @@ impl Term {
self
}

pub(crate) fn set_annotation<T>(&mut self, new_annotation: T) -> &mut Self
where
Annotation: From<T>,
{
match self {
Self::Annotation(annotation) => {
*annotation = Annotation::from(new_annotation);
}
Self::Atom(atom) => {
*self = AtomAnnotation::new(*atom, Annotation::from(new_annotation)).into();
}
Self::AtomAnnotation(inner) => {
inner.set_annotation(new_annotation);
}
Self::AtomExponent(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::AtomExponentAnnotation(inner) => inner.set_annotation(new_annotation),
Self::PrefixAtom(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::PrefixAtomAnnotation(inner) => inner.set_annotation(new_annotation),
Self::PrefixAtomExponent(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::PrefixAtomExponentAnnotation(inner) => inner.set_annotation(new_annotation),
Self::FactorAnnotation(inner) => inner.set_annotation(new_annotation),
Self::FactorAtomExponentAnnotation(inner) => inner.set_annotation(new_annotation),
Self::FactorAtomAnnotation(inner) => inner.set_annotation(new_annotation),
Self::FactorExponentAnnotation(inner) => inner.set_annotation(new_annotation),
Self::FactorPrefixAtomAnnotation(inner) => inner.set_annotation(new_annotation),
Self::Factor(factor) => {
*self = FactorAnnotation::new(*factor, Annotation::from(new_annotation)).into();
}
Self::FactorExponent(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::FactorAtom(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::FactorAtomExponent(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::FactorPrefixAtom(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::FactorPrefixAtomExponent(inner) => {
*self = inner.set_annotation(new_annotation).into();
}
Self::FactorPrefixAtomExponentAnnotation(inner) => inner.set_annotation(new_annotation),
}

self
}

/// A `Term` is a unity `Term` if represents "1", which technically means
/// here:
///
Expand Down
10 changes: 6 additions & 4 deletions crates/api/src/term/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,12 @@ pub(super) trait SetExponent {
fn set_exponent(&mut self, exponent: Exponent) -> Self::Output;
}

/// Trait for doing the opposite of `AssignExponent`.
///
pub(crate) trait UnassignExponent {
fn unassign_exponent(self) -> Term;
pub(super) trait SetAnnotation {
type Output;

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>;
}

// ╭────────────╮
Expand Down
13 changes: 12 additions & 1 deletion crates/api/src/term/variants/atom_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::Atom;

use super::{
Annotation, AssignFactor, AtomExponentAnnotation, Exponent, Factor, FactorAtomAnnotation,
PowOutput, SetExponent, Term,
PowOutput, SetAnnotation, SetExponent, Term,
};

// ╭────────────────╮
Expand Down Expand Up @@ -131,3 +131,14 @@ impl<'a> Inv for &'a mut AtomAnnotation {
self.pow(-1).unwrap_rest()
}
}

impl<'a> SetAnnotation for &'a mut AtomAnnotation {
type Output = ();

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
self.annotation = annotation.into();
}
}
23 changes: 14 additions & 9 deletions crates/api/src/term/variants/atom_exponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::fmt;

use num_traits::{Inv, Pow};

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

use super::{
AssignFactor, Exponent, Factor, FactorAtomExponent, InvOutput, PowOutput, SetExponent, Term,
UnassignExponent,
AssignFactor, AtomExponentAnnotation, Exponent, Factor, FactorAtomExponent, InvOutput,
PowOutput, SetAnnotation, SetExponent, Term,
};

// ╭──────────────╮
Expand Down Expand Up @@ -92,12 +92,6 @@ impl<'a> Pow<Exponent> for &'a mut AtomExponent {
}
}

impl UnassignExponent for AtomExponent {
fn unassign_exponent(self) -> Term {
Term::Atom(self.atom)
}
}

impl Inv for AtomExponent {
type Output = InvOutput<Atom, Self>;

Expand All @@ -121,3 +115,14 @@ impl<'a> Inv for &'a mut AtomExponent {
}
}
}

impl SetAnnotation for AtomExponent {
type Output = AtomExponentAnnotation;

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
Self::Output::new(self.atom, self.exponent, Annotation::from(annotation))
}
}
22 changes: 12 additions & 10 deletions crates/api/src/term/variants/atom_exponent_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::Atom;

use super::{
Annotation, AssignFactor, AtomAnnotation, Exponent, Factor, FactorAtomExponentAnnotation,
InvOutput, PowOutput, SetExponent, Term, UnassignExponent,
InvOutput, PowOutput, SetAnnotation, SetExponent, Term,
};

// ╭────────────────────────╮
Expand Down Expand Up @@ -79,15 +79,6 @@ impl SetExponent for AtomExponentAnnotation {
}
}

impl UnassignExponent for AtomExponentAnnotation {
fn unassign_exponent(self) -> Term {
Term::AtomAnnotation(AtomAnnotation {
atom: self.atom,
annotation: self.annotation,
})
}
}

impl Pow<Exponent> for AtomExponentAnnotation {
type Output = PowOutput<Annotation, AtomAnnotation, Self>;

Expand Down Expand Up @@ -159,3 +150,14 @@ impl<'a> Inv for &'a mut AtomExponentAnnotation {
}
}
}

impl<'a> SetAnnotation for &'a mut AtomExponentAnnotation {
type Output = ();

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
self.annotation = annotation.into();
}
}
16 changes: 15 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,10 @@ use std::{fmt, mem};

use num_traits::{Inv, Pow};

use super::{Annotation, Exponent, Factor, FactorExponentAnnotation, PowOutput, SetExponent, Term};
use super::{
Annotation, Exponent, Factor, FactorExponentAnnotation, PowOutput, SetAnnotation, SetExponent,
Term,
};

// ╭──────────────────╮
// │ FactorAnnotation │
Expand Down Expand Up @@ -111,3 +114,14 @@ impl<'a> Inv for &'a mut FactorAnnotation {
self.pow(-1).unwrap_rest()
}
}

impl<'a> SetAnnotation for &'a mut FactorAnnotation {
type Output = ();

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
self.annotation = annotation.into();
}
}
18 changes: 16 additions & 2 deletions crates/api/src/term/variants/factor_atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use std::fmt;

use num_traits::{Inv, Pow};

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

use super::{Exponent, Factor, FactorAtomExponent, PowOutput, SetExponent, Term};
use super::{
Exponent, Factor, FactorAtomAnnotation, FactorAtomExponent, PowOutput, SetAnnotation,
SetExponent, Term,
};

// ╭────────────╮
// │ FactorAtom │
Expand Down Expand Up @@ -91,3 +94,14 @@ impl<'a> Inv for &'a mut FactorAtom {
self.pow(-1).unwrap_rest()
}
}

impl SetAnnotation for FactorAtom {
type Output = FactorAtomAnnotation;

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
FactorAtomAnnotation::new(self.factor, self.atom, Annotation::from(annotation))
}
}
14 changes: 13 additions & 1 deletion crates/api/src/term/variants/factor_atom_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use num_traits::{Inv, Pow};
use crate::Atom;

use super::{
Annotation, Exponent, Factor, FactorAtomExponentAnnotation, PowOutput, SetExponent, Term,
Annotation, Exponent, Factor, FactorAtomExponentAnnotation, PowOutput, SetAnnotation,
SetExponent, Term,
};

// ╭──────────────────────╮
Expand Down Expand Up @@ -121,3 +122,14 @@ impl<'a> Inv for &'a mut FactorAtomAnnotation {
self.pow(-1).unwrap_rest()
}
}

impl<'a> SetAnnotation for &'a mut FactorAtomAnnotation {
type Output = ();

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
self.annotation = annotation.into();
}
}
30 changes: 19 additions & 11 deletions crates/api/src/term/variants/factor_atom_exponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::fmt;

use num_traits::{Inv, Pow};

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

use super::{
Exponent, Factor, FactorAtom, InvOutput, PowOutput, SetExponent, Term, UnassignExponent,
Exponent, Factor, FactorAtom, FactorAtomExponentAnnotation, InvOutput, PowOutput,
SetAnnotation, SetExponent, Term,
};

// ╭────────────────────╮
Expand Down Expand Up @@ -72,15 +73,6 @@ impl SetExponent for FactorAtomExponent {
}
}

impl UnassignExponent for FactorAtomExponent {
fn unassign_exponent(self) -> Term {
Term::FactorAtom(FactorAtom {
factor: self.factor,
atom: self.atom,
})
}
}

impl Pow<Exponent> for FactorAtomExponent {
type Output = PowOutput<Factor, FactorAtom, Self>;

Expand Down Expand Up @@ -126,3 +118,19 @@ impl<'a> Inv for &'a mut FactorAtomExponent {
}
}
}

impl SetAnnotation for FactorAtomExponent {
type Output = FactorAtomExponentAnnotation;

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
FactorAtomExponentAnnotation::new(
self.factor,
self.atom,
self.exponent,
Annotation::from(annotation),
)
}
}
25 changes: 13 additions & 12 deletions crates/api/src/term/variants/factor_atom_exponent_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use num_traits::{Inv, Pow};
use crate::Atom;

use super::{
Annotation, Exponent, Factor, FactorAtomAnnotation, InvOutput, PowOutput, SetExponent, Term,
UnassignExponent,
Annotation, Exponent, Factor, FactorAtomAnnotation, InvOutput, PowOutput, SetAnnotation,
SetExponent, Term,
};

// ╭──────────────────────────────╮
Expand Down Expand Up @@ -96,16 +96,6 @@ impl SetExponent for FactorAtomExponentAnnotation {
}
}

impl UnassignExponent for FactorAtomExponentAnnotation {
fn unassign_exponent(self) -> Term {
Term::FactorAtomAnnotation(FactorAtomAnnotation {
factor: self.factor,
atom: self.atom,
annotation: self.annotation,
})
}
}

impl Pow<Exponent> for FactorAtomExponentAnnotation {
type Output = PowOutput<Annotation, FactorAtomAnnotation, Self>;

Expand Down Expand Up @@ -183,3 +173,14 @@ impl<'a> Inv for &'a mut FactorAtomExponentAnnotation {
}
}
}

impl<'a> SetAnnotation for &'a mut FactorAtomExponentAnnotation {
type Output = ();

fn set_annotation<T>(self, annotation: T) -> Self::Output
where
Annotation: From<T>,
{
self.annotation = annotation.into();
}
}
Loading

0 comments on commit 32af844

Please sign in to comment.