Skip to content

Commit

Permalink
feat: implement more operations
Browse files Browse the repository at this point in the history
  • Loading branch information
JabobKrauskopf committed Oct 9, 2024
1 parent a50e1b1 commit ec2070a
Show file tree
Hide file tree
Showing 25 changed files with 6,804 additions and 462 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ serde = { version = "1.0.203", features = ["derive"] }
polars = { version = "0.40.0", features = ["polars-io"] }
chrono = { version = "0.4.38", features = ["serde"] }

medmodels = { version = "0.1.1", path = "crates/medmodels" }
medmodels-core = { version = "0.1.1", path = "crates/medmodels-core" }
medmodels-utils = { version = "0.1.1", path = "crates/medmodels-utils" }
medmodels = { version = "0.1.2", path = "crates/medmodels" }
medmodels-core = { version = "0.1.2", path = "crates/medmodels-core" }
medmodels-utils = { version = "0.1.2", path = "crates/medmodels-utils" }
1 change: 1 addition & 0 deletions crates/medmodels-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ serde = { workspace = true }
chrono = { workspace = true }
ron = "0.8.1"
roaring = "0.10.6"
itertools = "0.13.0"
223 changes: 211 additions & 12 deletions crates/medmodels-core/src/medrecord/datatypes/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
use super::{Contains, EndsWith, MedRecordValue, StartsWith};
use crate::errors::MedRecordError;
use super::{
Abs, Contains, EndsWith, Lowercase, MedRecordValue, Mod, Pow, Slice, StartsWith, Trim, TrimEnd,
TrimStart, Uppercase,
};
use crate::errors::{MedRecordError, MedRecordResult};
use medmodels_utils::implement_from_for_wrapper;
use serde::{Deserialize, Serialize};
use std::{cmp::Ordering, fmt::Display, hash::Hash};
use std::{
cmp::Ordering,
fmt::Display,
hash::Hash,
ops::{Add, Mul, Sub},
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MedRecordAttribute {
Expand Down Expand Up @@ -43,15 +51,6 @@ impl TryFrom<MedRecordValue> for MedRecordAttribute {
}
}

impl Display for MedRecordAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String(value) => write!(f, "{}", value),
Self::Int(value) => write!(f, "{}", value),
}
}
}

impl PartialEq for MedRecordAttribute {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand Down Expand Up @@ -80,6 +79,130 @@ impl PartialOrd for MedRecordAttribute {
}
}

impl Display for MedRecordAttribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::String(value) => write!(f, "{}", value),
Self::Int(value) => write!(f, "{}", value),
}
}
}

// TODO: Add tests
impl Add for MedRecordAttribute {
type Output = MedRecordResult<Self>;

fn add(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(MedRecordAttribute::String(value), MedRecordAttribute::String(rhs)) => {
Ok(MedRecordAttribute::String(value + rhs.as_str()))
}
(MedRecordAttribute::String(value), MedRecordAttribute::Int(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot add {} to {}", rhs, value)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::String(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot add {} to {}", rhs, value)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::Int(rhs)) => {
Ok(MedRecordAttribute::Int(value + rhs))
}
}
}
}

// TODO: Add tests
impl Sub for MedRecordAttribute {
type Output = MedRecordResult<Self>;

fn sub(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(MedRecordAttribute::String(value), MedRecordAttribute::String(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot subtract {} from {}", rhs, value)),
),
(MedRecordAttribute::String(value), MedRecordAttribute::Int(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot subtract {} from {}", rhs, value)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::String(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot subtract {} from {}", rhs, value)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::Int(rhs)) => {
Ok(MedRecordAttribute::Int(value - rhs))
}
}
}
}

// TODO: Add tests
impl Mul for MedRecordAttribute {
type Output = MedRecordResult<Self>;

fn mul(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(MedRecordAttribute::String(value), MedRecordAttribute::String(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot multiply {} by {}", value, rhs)),
),
(MedRecordAttribute::String(value), MedRecordAttribute::Int(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot multiply {} by {}", value, rhs)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::String(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot multiply {} by {}", value, rhs)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::Int(rhs)) => {
Ok(MedRecordAttribute::Int(value * rhs))
}
}
}
}

// TODO: Add tests
impl Pow for MedRecordAttribute {
fn pow(self, rhs: Self) -> MedRecordResult<Self> {
match (self, rhs) {
(MedRecordAttribute::String(value), MedRecordAttribute::String(rhs)) => {
Err(MedRecordError::AssertionError(format!(
"Cannot raise {} to the power of {}",
value, rhs
)))
}
(MedRecordAttribute::String(value), MedRecordAttribute::Int(rhs)) => {
Err(MedRecordError::AssertionError(format!(
"Cannot raise {} to the power of {}",
value, rhs
)))
}
(MedRecordAttribute::Int(value), MedRecordAttribute::String(rhs)) => {
Err(MedRecordError::AssertionError(format!(
"Cannot raise {} to the power of {}",
value, rhs
)))
}
(MedRecordAttribute::Int(value), MedRecordAttribute::Int(rhs)) => {
Ok(MedRecordAttribute::Int(value.pow(rhs as u32)))
}
}
}
}

// TODO: Add tests
impl Mod for MedRecordAttribute {
fn r#mod(self, rhs: Self) -> MedRecordResult<Self> {
match (self, rhs) {
(MedRecordAttribute::String(value), MedRecordAttribute::String(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot mod {} by {}", value, rhs)),
),
(MedRecordAttribute::String(value), MedRecordAttribute::Int(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot mod {} by {}", value, rhs)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::String(rhs)) => Err(
MedRecordError::AssertionError(format!("Cannot mod {} by {}", value, rhs)),
),
(MedRecordAttribute::Int(value), MedRecordAttribute::Int(rhs)) => {
Ok(MedRecordAttribute::Int(value % rhs))
}
}
}
}

impl StartsWith for MedRecordAttribute {
fn starts_with(&self, other: &Self) -> bool {
match (self, other) {
Expand Down Expand Up @@ -137,6 +260,82 @@ impl Contains for MedRecordAttribute {
}
}

// TODO: Add tests
impl Slice for MedRecordAttribute {
fn slice(self, range: std::ops::Range<usize>) -> Self {
match self {
MedRecordAttribute::String(value) => value[range].into(),
MedRecordAttribute::Int(value) => value.to_string()[range].into(),
}
}
}

// TODO: Add tests
impl Abs for MedRecordAttribute {
fn abs(self) -> Self {
match self {
MedRecordAttribute::Int(value) => MedRecordAttribute::Int(value.abs()),
_ => self,
}
}
}

// TODO: Add tests
impl Trim for MedRecordAttribute {
fn trim(self) -> Self {
match self {
MedRecordAttribute::String(value) => {
MedRecordAttribute::String(value.trim().to_string())
}
_ => self,
}
}
}

// TODO: Add tests
impl TrimStart for MedRecordAttribute {
fn trim_start(self) -> Self {
match self {
MedRecordAttribute::String(value) => {
MedRecordAttribute::String(value.trim_start().to_string())
}
_ => self,
}
}
}

// TODO: Add tests
impl TrimEnd for MedRecordAttribute {
fn trim_end(self) -> Self {
match self {
MedRecordAttribute::String(value) => {
MedRecordAttribute::String(value.trim_end().to_string())
}
_ => self,
}
}
}

// TODO: Add tests
impl Lowercase for MedRecordAttribute {
fn lowercase(self) -> Self {
match self {
MedRecordAttribute::String(value) => MedRecordAttribute::String(value.to_lowercase()),
_ => self,
}
}
}

// TODO: Add tests
impl Uppercase for MedRecordAttribute {
fn uppercase(self) -> Self {
match self {
MedRecordAttribute::String(value) => MedRecordAttribute::String(value.to_uppercase()),
_ => self,
}
}
}

#[cfg(test)]
mod test {
use super::MedRecordAttribute;
Expand Down
Loading

0 comments on commit ec2070a

Please sign in to comment.