Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions crates/medmodels-core/src/medrecord/attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
use crate::{
errors::MedRecordError,
prelude::{
Attributes, EdgeIndex, Group, MedRecordAttribute, MedRecordValue, NodeIndex, SchemaType,
},
MedRecord,
};

macro_rules! impl_attributes_mut {
(
$struct_name:ident,
$index_type:ty,
$index_field:ident,
$entity:literal,
$contains_fn:ident,
$groups_of_fn:ident,
$get_attributes_fn:ident,
$get_attributes_mut_fn:ident,
$schema_update_fn:ident,
$schema_validate_fn:ident
) => {
pub struct $struct_name<'a> {
$index_field: &'a $index_type,
medrecord: &'a mut MedRecord,
}

impl<'a> $struct_name<'a> {
pub(crate) fn new(
$index_field: &'a $index_type,
medrecord: &'a mut MedRecord,
) -> Result<Self, MedRecordError> {
if !medrecord.$contains_fn($index_field) {
return Err(MedRecordError::IndexError(format!(
concat!("Cannot find ", $entity, " with index {}"),
$index_field
)));
}

Ok(Self {
$index_field,
medrecord,
})
}

fn get_groups(&self) -> Vec<Group> {
self.medrecord
.$groups_of_fn(self.$index_field)
.expect(concat!($entity, " must exist."))
.cloned()
.collect()
}

fn handle_schema(
&mut self,
attributes: &Attributes,
groups: &[Group],
) -> Result<(), MedRecordError> {
let schema = &mut self.medrecord.schema;

match schema.schema_type() {
SchemaType::Inferred => {
if groups.is_empty() {
schema.$schema_update_fn(attributes, None, false);
} else {
for group in groups {
schema.$schema_update_fn(attributes, Some(group), false);
}
}
}
SchemaType::Provided => {
if groups.is_empty() {
schema.$schema_validate_fn(self.$index_field, attributes, None)?;
} else {
for group in groups {
schema.$schema_validate_fn(
self.$index_field,
attributes,
Some(group),
)?;
}
}
}
}

Ok(())
}

fn set_attributes(&mut self, attributes: Attributes) {
*self
.medrecord
.graph
.$get_attributes_mut_fn(self.$index_field)
.expect(concat!($entity, " must exist.")) = attributes;
}

pub fn replace_attributes(
&mut self,
attributes: Attributes,
) -> Result<(), MedRecordError> {
let groups = self.get_groups();
self.handle_schema(&attributes, &groups)?;
self.set_attributes(attributes);
Ok(())
}

pub fn update_attribute(
&mut self,
attribute: &MedRecordAttribute,
value: MedRecordValue,
) -> Result<(), MedRecordError> {
let groups = self.get_groups();

let mut attributes = self
.medrecord
.$get_attributes_fn(self.$index_field)
.expect(concat!($entity, " must exist."))
.clone();
attributes
.entry(attribute.clone())
.and_modify(|v| *v = value.clone())
.or_insert(value);

self.handle_schema(&attributes, &groups)?;
self.set_attributes(attributes);
Ok(())
}

pub fn remove_attribute(
&mut self,
attribute: &MedRecordAttribute,
) -> Result<MedRecordValue, MedRecordError> {
let groups = self.get_groups();

let mut attributes = self
.medrecord
.$get_attributes_fn(self.$index_field)
.expect(concat!($entity, " must exist."))
.clone();
let removed_value = attributes.remove(attribute);

let Some(removed_value) = removed_value else {
return Err(MedRecordError::KeyError(format!(
concat!("Attribute {} does not exist on ", $entity, " {}"),
attribute, self.$index_field
)));
};

self.handle_schema(&attributes, &groups)?;
self.set_attributes(attributes);
Ok(removed_value)
}
}
};
}

impl_attributes_mut!(
NodeAttributesMut,
NodeIndex,
node_index,
"node",
contains_node,
groups_of_node,
node_attributes,
node_attributes_mut,
update_node,
validate_node
);

impl_attributes_mut!(
EdgeAttributesMut,
EdgeIndex,
edge_index,
"edge",
contains_edge,
groups_of_edge,
edge_attributes,
edge_attributes_mut,
update_edge,
validate_edge
);
45 changes: 22 additions & 23 deletions crates/medmodels-core/src/medrecord/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod attributes;
pub mod datatypes;
mod example_dataset;
mod graph;
Expand All @@ -15,6 +16,7 @@ pub use self::{
use crate::{
errors::MedRecordError,
medrecord::{
attributes::{EdgeAttributesMut, NodeAttributesMut},
overview::{GroupOverview, Overview, DEFAULT_TRUNCATE_DETAILS},
polars::DataFramesExport,
},
Expand Down Expand Up @@ -402,13 +404,11 @@ impl MedRecord {
.map_err(MedRecordError::from)
}

pub fn node_attributes_mut(
&mut self,
node_index: &NodeIndex,
) -> Result<&mut Attributes, MedRecordError> {
self.graph
.node_attributes_mut(node_index)
.map_err(MedRecordError::from)
pub fn node_attributes_mut<'a>(
&'a mut self,
node_index: &'a NodeIndex,
) -> Result<NodeAttributesMut<'a>, MedRecordError> {
NodeAttributesMut::new(node_index, self)
}

pub fn outgoing_edges(
Expand Down Expand Up @@ -439,13 +439,11 @@ impl MedRecord {
.map_err(MedRecordError::from)
}

pub fn edge_attributes_mut(
&mut self,
edge_index: &EdgeIndex,
) -> Result<&mut Attributes, MedRecordError> {
self.graph
.edge_attributes_mut(edge_index)
.map_err(MedRecordError::from)
pub fn edge_attributes_mut<'a>(
&'a mut self,
edge_index: &'a EdgeIndex,
) -> Result<EdgeAttributesMut<'a>, MedRecordError> {
EdgeAttributesMut::new(edge_index, self)
}

pub fn edge_endpoints(
Expand Down Expand Up @@ -1314,17 +1312,18 @@ mod test {
fn test_node_attributes_mut() {
let mut medrecord = create_medrecord();

let attributes = medrecord.node_attributes_mut(&"0".into()).unwrap();

assert_eq!(&create_nodes()[0].1, attributes);
let node_index = "0".into();
let mut attributes = medrecord.node_attributes_mut(&node_index).unwrap();

let new_attributes = HashMap::from([("0".into(), "1".into()), ("2".into(), "3".into())]);

attributes.clone_from(&new_attributes);
attributes
.replace_attributes(new_attributes.clone())
.unwrap();

assert_eq!(
&new_attributes,
medrecord.node_attributes(&"0".into()).unwrap()
medrecord.node_attributes(&node_index).unwrap()
);
}

Expand Down Expand Up @@ -1407,13 +1406,13 @@ mod test {
fn test_edge_attributes_mut() {
let mut medrecord = create_medrecord();

let attributes = medrecord.edge_attributes_mut(&0).unwrap();

assert_eq!(&create_edges()[0].2, attributes);
let mut attributes = medrecord.edge_attributes_mut(&0).unwrap();

let new_attributes = HashMap::from([("0".into(), "1".into()), ("2".into(), "3".into())]);

attributes.clone_from(&new_attributes);
attributes
.replace_attributes(new_attributes.clone())
.unwrap();

assert_eq!(&new_attributes, medrecord.edge_attributes(&0).unwrap());
}
Expand Down
Loading