Skip to content

Commit

Permalink
Merge pull request #206 from rruckley/PartyByRole-168
Browse files Browse the repository at this point in the history
Add Party by role trait function
  • Loading branch information
rruckley authored Jan 6, 2025
2 parents 137f3a8 + 3c8c51e commit 52333f9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ serde = { version = "1.0.217", features = ["derive"]}
serde_json = "1.0.134"
sha256 = { version = "1.5", default-features = false }
uuid = { version = "1.11.0", features = ["v4"]}
tmflib-derive = { version = "0.1.28" }
tmflib-derive = { version = "0.1.29" }
# tmflib-derive = { path = "tmflib-derive"}
hex = "0.4.3"
base32 = "0.5.1"
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ pub trait HasRelatedParty : HasId {
fn add_party(&mut self, party : RelatedParty);
/// Remote a party
fn remove_party(&mut self, idx : usize) -> Result<RelatedParty,String>;
/// Get a list of RelatedParty entries by role
fn get_by_role(&self, role : String) -> Option<Vec<&RelatedParty>>;
}

/// Trait for generating an event
Expand Down
58 changes: 58 additions & 0 deletions src/tmf620/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,39 @@ pub struct Catalog {
r#type : Option<String>,
}

// impl HasRelatedParty for Catalog {
// fn add_party(&mut self, party : RelatedParty) {
// match self.related_party.as_mut() {
// Some(v) => v.push(party),
// None => self.related_party = Some(vec![party]),
// }
// }
// fn get_by_role(&self, role : String) -> Option<Vec<&RelatedParty>> {
// match &self.related_party {
// Some(rp) => {
// let out = rp.iter()
// .filter(|p| p.role.is_some())
// .filter(|p| p.role.clone().unwrap() == role)
// .collect();
// Some(out)
// },
// None => None,
// }
// }
// fn get_party(&self, idx : usize ) -> Option<&RelatedParty> {
// match self.related_party.as_ref() {
// Some(rp) => {
// // Simple return results of get()
// rp.get(idx)
// },
// None => None,
// }
// }
// fn remove_party(&mut self, idx : usize) -> Result<RelatedParty,String> {
// Ok(self.related_party.as_mut().unwrap().remove(idx))
// }
// }

impl Catalog {
/// Create a new instance of catalog struct
pub fn new(name : impl Into<String>) -> Catalog {
Expand Down Expand Up @@ -287,5 +320,30 @@ mod tests {
println!("{:?}",catalogeventtype);
}

#[test]
fn test_catalog_getpartybyrole() {
let mut cat = Catalog::new(CAT_NAME);

let org = Organization::new("An Organisation");

cat.add_party(RelatedParty::from(&org));

let party = cat.get_by_role(Organization::get_class());

assert_eq!(party.is_some(),true);

let party_vec = party.unwrap();

assert_eq!(party_vec.len(),1);

let party_vec_first = party_vec.first();

assert_eq!(party_vec_first.is_some(),true);

let related_party = party_vec_first.unwrap();

assert_eq!(related_party.name,Some("An Organisation".to_string()));
}


}
18 changes: 9 additions & 9 deletions tmflib-derive/Cargo.lock

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

4 changes: 2 additions & 2 deletions tmflib-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tmflib-derive"
version = "0.1.28"
version = "0.1.29"
edition = "2021"
authors = ["Ryan Ruckley <[email protected]>"]
description = "Derive macro for the tmflib::HasId trait"
Expand All @@ -13,4 +13,4 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0.92"
quote = "1.0.38"
syn = "2.0.94"
syn = "2.0.95"
12 changes: 12 additions & 0 deletions tmflib-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ pub fn hasrelatedparty_derive(input: TokenStream) -> TokenStream {
fn remove_party(&mut self, idx : usize) -> Result<RelatedParty,String> {
Ok(self.related_party.as_mut().unwrap().remove(idx))
}
fn get_by_role(&self, role : String) -> Option<Vec<&RelatedParty>> {
match &self.related_party {
Some(rp) => {
let out = rp.iter()
.filter(|p| p.role.is_some())
.filter(|p| p.role.clone().unwrap() == role)
.collect();
Some(out)
},
None => None,
}
}
}
};
out.into()
Expand Down

0 comments on commit 52333f9

Please sign in to comment.