Skip to content

Commit

Permalink
Merge pull request #175 from rruckley/Remove-Unwrap-174
Browse files Browse the repository at this point in the history
Remove unwrap() where possible
  • Loading branch information
rruckley authored Aug 20, 2024
2 parents 1a02c70 + ce3e514 commit 8f93502
Show file tree
Hide file tree
Showing 41 changed files with 263 additions and 166 deletions.
1 change: 1 addition & 0 deletions examples/cart_to_order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Shopping Cart to Product Order
use tmflib::HasRelatedParty;
use tmflib::common::related_party::RelatedParty;
use tmflib::common::note::Note;
#[cfg(feature = "tmf620-v4")]
Expand Down
1 change: 1 addition & 0 deletions examples/create_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tmflib::tmf632::organization_v4::Organization;
#[cfg(feature = "tmf632-v5")]
use tmflib::tmf632::organization_v5::Organization;
use tmflib::tmf629::customer::Customer;
use tmflib::HasRelatedParty;


fn main() {
Expand Down
2 changes: 0 additions & 2 deletions examples/create_customer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ fn main() {

customer.upgrade_to_code(None);
dbg!(customer);

// println!("Customer Code: {}",customer.get_characteristic("code").unwrap().value);
}
1 change: 1 addition & 0 deletions examples/create_shopping_cart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use tmflib::tmf632::individual_v4::Individual;
use tmflib::tmf632::individual_v5::Individual;
use tmflib::tmf663::shopping_cart::ShoppingCart;
use tmflib::tmf663::cart_item::CartItem;
use tmflib::HasRelatedParty;


fn main() {
Expand Down
4 changes: 1 addition & 3 deletions examples/create_work_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,5 @@ fn main() {

wo.add_item(woi);

// dbg!(wo);
let json = serde_json::to_string(&wo).unwrap();
println!("JSON>>\n{}\n<<JSON",json);
dbg!(wo);
}
9 changes: 6 additions & 3 deletions examples/service_to_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tmflib::tmf632::individual_v5::Individual;
use tmflib::common::note::Note;
use tmflib::common::related_party::RelatedParty;
use tmflib::tmf641::service_order_item::{ServiceOrderItem,ServiceRefOrValue};
use tmflib::{HasRelatedParty,HasNote};

fn main() {
let mut ss = ServiceSpecification::new("Access");
Expand Down Expand Up @@ -45,17 +46,19 @@ fn main() {
// Create new ServiceOrder
let mut so = ServiceOrder::new();
// Add a sample note
so.note.as_mut().unwrap().push(Note::new("This is a Note."));
so.add_note(Note::new("This is a Note."));
// Create a related party
let ind = Individual::new("John Q. Citizen");
// Add related party reference to ServiceOrder
so.related_party.as_mut().unwrap().push(RelatedParty::from(&ind));
// This should use trait functions to add a party.
so.add_party(RelatedParty::from(&ind));
// Set the Category
so.category = Some("Fixed Product".to_string());
// Set the external Id
so.external_id = Some("PON1234983".to_string());
// Add sample Service Order Item
so.servce_order_item.as_mut().unwrap().push(soi);
// This should use an add_item() function
so.add_item(soi);

// Now transform the Service Order into a Product Order for downstream parties
let po = ProductOrder::from(so);
Expand Down
20 changes: 11 additions & 9 deletions examples/validate_customer_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
//!
use tmflib::{tmf629::customer::Customer, HasName,HasId};

fn main() {
fn main() -> Result<(),String> {

let mut cust1 = Customer::default();

cust1.set_name("NATIONAL BEVERAGE COMPANY LIMITED");
cust1.id = Some(String::from("123456"));
cust1.generate_code(None);

let code1 = cust1.get_characteristic("code");
let hash = cust1.get_characteristic("hash");
let code1 = cust1.get_characteristic("code").ok_or(String::from("No Value"))?;
let hash = cust1.get_characteristic("hash").ok_or(String::from("No Value"))?;

println!("Customer: {} + ID: {} Offset=0\t generates Code: {}",cust1.get_name(),cust1.get_id(),code1.unwrap().value);
println!("Customer: {},\tBase32: {}",cust1.get_name(),hash.unwrap().value);
println!("Customer: {} + ID: {} Offset=0\t generates Code: {}",cust1.get_name(),cust1.get_id(),code1.value);
println!("Customer: {},\tBase32: {}",cust1.get_name(),hash.value);

cust1.generate_code(Some(1));

let code1 = cust1.get_characteristic("code");
let hash = cust1.get_characteristic("hash");
let code1 = cust1.get_characteristic("code").ok_or(String::from("No Value"))?;
let hash = cust1.get_characteristic("hash").ok_or(String::from("No Value"))?;

println!("Customer: {} + ID: {} Offset=1\t generates Code: {}",cust1.get_name(),cust1.get_id(),code1.unwrap().value);
println!("Customer: {},\tBase32: {}",cust1.get_name(),hash.unwrap().value);
println!("Customer: {} + ID: {} Offset=1\t generates Code: {}",cust1.get_name(),cust1.get_id(),code1.value);
println!("Customer: {},\tBase32: {}",cust1.get_name(),hash.value);

Ok(())
}
9 changes: 4 additions & 5 deletions src/common/attachment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,27 @@ mod test {

let attachment = AttachmentRefOrValue::from(&document);

assert_eq!(attachment.name.unwrap(),document.get_name());
assert_eq!(attachment.get_name(),document.get_name());
}

#[test]
fn test_attachmenttype_deserialize() {
let attach_type : AttachmentType = serde_json::from_str(ATTACH_TYPE_JSON).unwrap();
let attach_type : AttachmentType = serde_json::from_str(ATTACH_TYPE_JSON).expect("Could not parse test json");

assert_eq!(attach_type,AttachmentType::InLine);
}

#[test]
fn test_attachmentsize_deserialize() {
let attach_size : AttachmentSize = serde_json::from_str(ATTACH_SIZE).unwrap();
let attach_size : AttachmentSize = serde_json::from_str(ATTACH_SIZE).expect("Could not parse test json");

assert_eq!(attach_size.amount,123.4);
assert_eq!(attach_size.units.as_str(),"bytes");
}

#[test]
fn test_attach_deserialize() {
let _attach : AttachmentRefOrValue = serde_json::from_str(ATTACH_JSON).unwrap();
let _attach : AttachmentRefOrValue = serde_json::from_str(ATTACH_JSON).expect("Could not parse attach JSON");

}

Expand All @@ -162,6 +162,5 @@ mod test {

assert_eq!(attach.valid_for.is_some(),true);
assert_eq!(attach.valid_for.unwrap().started(),true);
// assert_eq!(attach.valid_for.unwrap().finished(),false);
}
}
15 changes: 8 additions & 7 deletions src/common/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ mod test {
assert_eq!(email.medium_type.unwrap(),EMAIL_TYPE.to_string());
assert_eq!(email.characteristic.is_some(),true);
assert_eq!(email.characteristic.unwrap().contact_type.unwrap(),EMAIL_TYPE.to_string());
//assert_eq!(email.characteristic.unwrap().email_address.unwrap(),"[email protected]".to_string());
}

#[test]
Expand All @@ -192,18 +191,20 @@ mod test {

#[test]
fn test_mediumcharacteristic_deserialize() {
let mediumchar: MediumCharacteristic = serde_json::from_str(MEDIUM_CHAR_JSON).unwrap();
let mediumchar: MediumCharacteristic = serde_json::from_str(MEDIUM_CHAR_JSON)
.expect("MEDIUM_CHAR_JSON");

assert_eq!(mediumchar.contact_type.is_some(),true);
assert_eq!(mediumchar.contact_type.unwrap().as_str(),"email");
assert_eq!(mediumchar.contact_type.expect("Could not parse mediumchar JSON").as_str(),"email");

assert_eq!(mediumchar.email_address.is_some(),true);
assert_eq!(mediumchar.email_address.unwrap().as_str(),"[email protected]");
assert_eq!(mediumchar.email_address.expect("Could not parse email_address JSON").as_str(),"[email protected]");
}

#[test]
fn test_contact_deserialize() {
let contact : Contact = serde_json::from_str(CONTACT_JSON).unwrap();
let contact : Contact = serde_json::from_str(CONTACT_JSON)
.expect("CONTACT_JSON");

assert_eq!(contact.contact_name.as_str(),"John Quinton Citizen");
assert_eq!(contact.contact_type.as_str(),"primary");
Expand All @@ -213,7 +214,7 @@ mod test {

#[test]
fn test_contactmedium_deserialize() {
let contact_medium : ContactMedium = serde_json::from_str(CONTACT_MEDIUM).unwrap();
let contact_medium : ContactMedium = serde_json::from_str(CONTACT_MEDIUM).expect("Could not parse CONTACT_MEDIUM");

assert_eq!(contact_medium.preferred,true);
}
Expand All @@ -223,6 +224,6 @@ mod test {
let contact_char : ContactCharacteristic = serde_json::from_str(CONTACT_CHAR).unwrap();

assert_eq!(contact_char.email_address.is_some(),true);
assert_eq!(contact_char.email_address.unwrap().as_str(),"[email protected]");
assert_eq!(contact_char.email_address.expect("Could not parse email_address JSON").as_str(),"[email protected]");
}
}
3 changes: 2 additions & 1 deletion src/common/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ mod test {

#[test]
fn test_event_deserialize() {
let event : Event<Customer,CustomerEventType> = serde_json::from_str(EVENT_JSON).unwrap();
let event : Event<Customer,CustomerEventType> = serde_json::from_str(EVENT_JSON)
.expect("Could not parse EVENT_JSON");

assert_eq!(event.event_id.as_str(),"E123");
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/external_identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ mod test {

#[test]
fn test_external_deserialize() {
let external : ExternalIdentifier = serde_json::from_str(EXTERNAL_JSON).unwrap();
let external : ExternalIdentifier = serde_json::from_str(EXTERNAL_JSON)
.expect("Could not parse EXTERNAL_JSON");

assert_eq!(external.external_identifier_type.as_str(),"email");
assert_eq!(external.owner.as_str(),"customer");
Expand Down
3 changes: 2 additions & 1 deletion src/common/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ mod test {

#[test]
fn test_money_deserialize() {
let money : Money = serde_json::from_str(MONEY_JSON).unwrap();
let money : Money = serde_json::from_str(MONEY_JSON)
.expect("MONEY_JSON");

assert_eq!(money.unit.as_str(),"AUD");
assert_eq!(money.value,12.34);
Expand Down
3 changes: 2 additions & 1 deletion src/common/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ mod test {

#[test]
fn test_price_deserialization() {
let price : Price = serde_json::from_str(PRICE_JSON).unwrap();
let price : Price = serde_json::from_str(PRICE_JSON)
.expect("PRICE_JSON");

assert_eq!(price.percentage, 30.0);
assert_eq!(price.tax_rate,10.0);
Expand Down
14 changes: 7 additions & 7 deletions src/common/related_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ pub struct RelatedParty {
impl From<&Customer> for RelatedParty {
fn from(cust: &Customer) -> Self {
RelatedParty {
id: cust.id.as_ref().unwrap().clone(),
href: cust.href.as_ref().unwrap().clone(),
id: cust.get_id(),
href: cust.get_href(),
name: cust.name.clone(),
role: Some(Customer::get_class()),
base_type: Some(Customer::get_class()),
Expand Down Expand Up @@ -118,9 +118,9 @@ impl From<OrganizationRef> for RelatedParty {
impl From<&Individual> for RelatedParty {
fn from(value: &Individual) -> Self {
RelatedParty {
id: value.id.as_ref().unwrap().clone(),
href: value.href.as_ref().unwrap().clone(),
name: value.full_name.clone(),
id: value.get_id(),
href: value.get_href(),
name: Some(value.get_name()),
role: Some(Individual::get_class()),
referred_type: Some(Individual::get_class()),
base_type: Some(Individual::get_class()),
Expand All @@ -135,8 +135,8 @@ impl From<&Individual> for RelatedParty {
impl From<&PartyRole> for RelatedParty {
fn from(value: &PartyRole) -> Self {
RelatedParty {
id: value.id.as_ref().unwrap().clone(),
href: value.href.as_ref().unwrap().clone(),
id: value.get_id(),
href: value.get_href(),
name: None,
role: value.name.clone(),
referred_type: Some(PartyRole::get_class()),
Expand Down
3 changes: 2 additions & 1 deletion src/common/tax_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ mod test {

#[test]
fn test_taxitem_deserialise() {
let taxitem : TaxItem = serde_json::from_str(TAX_JSON).unwrap();
let taxitem : TaxItem = serde_json::from_str(TAX_JSON)
.expect("Could not parase TAX_JSON");

assert_eq!(taxitem.tax_category.as_str(),"TaxCategory");
assert_eq!(taxitem.tax_rate,0.10);
Expand Down
Loading

0 comments on commit 8f93502

Please sign in to comment.