Skip to content

Commit

Permalink
refactor: update exposed functions and move results to struct builders
Browse files Browse the repository at this point in the history
  • Loading branch information
d-kuen committed Nov 22, 2024
1 parent bc77f11 commit b27dd01
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 435 deletions.
16 changes: 8 additions & 8 deletions src/address.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::xml::XmlElement;
use crate::xml::{ToXml, XmlElement};

#[derive(Default)]
pub struct Address<'a> {
Expand Down Expand Up @@ -34,18 +34,18 @@ impl<'a> Address<'a> {
}

pub fn with_phone(mut self, phone_number: &'a str) -> Self {
let phone_numbers = self.phone.get_or_insert_with(Vec::new);
phone_numbers.push(phone_number);
self.phone.get_or_insert_with(Vec::new).push(phone_number);
self
}

pub fn with_email(mut self, email_address: &'a str) -> Self {
let email_addresses = self.email.get_or_insert_with(Vec::new);
email_addresses.push(email_address);
self.email.get_or_insert_with(Vec::new).push(email_address);
self
}
}

pub fn as_xml(&self) -> XmlElement {
impl ToXml for Address<'_> {
fn to_xml(&self) -> String {
let mut e = XmlElement::new("Address").with_text_element("Name", self.name);

if let Some(s) = self.street {
Expand All @@ -60,7 +60,7 @@ impl<'a> Address<'a> {
if let Some(cc) = self.country_code {
ce = ce.with_attr("CountryCode", cc);
}
e = e.with_element(ce);
e = e.with_element(&ce);

if let Some(phone_numbers) = &self.phone {
for phone_number in phone_numbers {
Expand All @@ -74,6 +74,6 @@ impl<'a> Address<'a> {
}
}

e
e.to_xml()
}
}
26 changes: 16 additions & 10 deletions src/biller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
address::Address, contact::Contact, identification::FurtherIdentification,
order_reference::OrderReference, xml::XmlElement,
address::Address,
contact::Contact,
identification::FurtherIdentification,
order_reference::OrderReference,
xml::{ToXml, XmlElement},
};

#[derive(Default)]
Expand All @@ -24,8 +27,9 @@ impl<'a> Biller<'a> {
mut self,
further_identification: FurtherIdentification<'a>,
) -> Self {
let fi = self.further_identification.get_or_insert_with(Vec::new);
fi.push(further_identification);
self.further_identification
.get_or_insert_with(Vec::new)
.push(further_identification);
self
}

Expand All @@ -38,29 +42,31 @@ impl<'a> Biller<'a> {
self.contact = Some(contact);
self
}
}

pub fn as_xml(&self) -> XmlElement {
impl ToXml for Biller<'_> {
fn to_xml(&self) -> String {
let mut e = XmlElement::new("Biller")
.with_text_element("VATIdentificationNumber", self.vat_identification_number);

if let Some(fis) = &self.further_identification {
for fi in fis {
e = e.with_element(fi.as_xml())
e = e.with_element(fi)
}
}

if let Some(or) = &self.order_reference {
e = e.with_element(or.as_xml());
e = e.with_element(or);
}

if let Some(a) = &self.address {
e = e.with_element(a.as_xml());
e = e.with_element(a);
}

if let Some(c) = &self.contact {
e = e.with_element(c.as_xml());
e = e.with_element(c);
}

e
e.to_xml()
}
}
14 changes: 7 additions & 7 deletions src/contact.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::xml::XmlElement;
use crate::xml::{ToXml, XmlElement};

#[derive(Default)]
pub struct Contact<'a> {
Expand All @@ -22,18 +22,18 @@ impl<'a> Contact<'a> {
}

pub fn with_phone(mut self, phone_number: &'a str) -> Self {
let phone_numbers = self.phone.get_or_insert_with(Vec::new);
phone_numbers.push(phone_number);
self.phone.get_or_insert_with(Vec::new).push(phone_number);
self
}

pub fn with_email(mut self, email_address: &'a str) -> Self {
let email_addresses = self.email.get_or_insert_with(Vec::new);
email_addresses.push(email_address);
self.email.get_or_insert_with(Vec::new).push(email_address);
self
}
}

pub fn as_xml(&self) -> XmlElement {
impl ToXml for Contact<'_> {
fn to_xml(&self) -> String {
let mut e = XmlElement::new("Contact");

if let Some(s) = self.salutation {
Expand All @@ -54,6 +54,6 @@ impl<'a> Contact<'a> {
}
}

e
e.to_xml()
}
}
61 changes: 27 additions & 34 deletions src/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
reduction_and_surcharge::{
ReductionAndSurchargeListLineItemDetails, ReductionListLineItem, SurchargeListLineItem,
},
tax::{TaxCategory, TaxItem},
xml::XmlElement,
tax::TaxItem,
xml::{ToXml, XmlElement},
};

#[derive(Default)]
Expand All @@ -18,7 +18,7 @@ pub struct DetailsItem<'a> {
unit_price: Decimal,
base_quantity: Option<Decimal>,
reduction_and_surcharge: Option<ReductionAndSurchargeListLineItemDetails<'a>>,
tax_item: TaxItem,
pub(crate) tax_item: TaxItem,
}

impl<'a> DetailsItem<'a> {
Expand Down Expand Up @@ -72,11 +72,7 @@ impl<'a> DetailsItem<'a> {
self
}

pub fn tax_item_tuple(&self) -> (Decimal, TaxCategory) {
self.tax_item.tax_item_tuple()
}

pub fn line_item_amount(&self) -> Decimal {
pub(crate) fn line_item_amount(&self) -> Decimal {
let base_quantity = self.base_quantity.unwrap_or(Decimal::ONE);

let reduction_and_surcharge_sum = match &self.reduction_and_surcharge {
Expand All @@ -89,12 +85,14 @@ impl<'a> DetailsItem<'a> {
/* + sum of other_vat_able_tax_list_line_item.tax_amount */
}

pub fn line_item_total_gross_amount(&self) -> Decimal {
pub(crate) fn line_item_total_gross_amount(&self) -> Decimal {
self.line_item_amount()
* ((self.tax_item.percent() + Decimal::ONE_HUNDRED) / Decimal::ONE_HUNDRED)
* ((self.tax_item.tax_percent + Decimal::ONE_HUNDRED) / Decimal::ONE_HUNDRED)
}
}

pub fn as_xml(&self) -> XmlElement {
impl ToXml for DetailsItem<'_> {
fn to_xml(&self) -> String {
let mut e = XmlElement::new("ListLineItem");

// PositionNumber.
Expand All @@ -109,7 +107,7 @@ impl<'a> DetailsItem<'a> {

// Quantity.
e = e.with_element(
XmlElement::new("Quantity")
&XmlElement::new("Quantity")
.with_attr("Unit", self.unit)
.with_text(self.quantity.clone_with_scale(4).to_string()),
);
Expand All @@ -120,42 +118,42 @@ impl<'a> DetailsItem<'a> {
if let Some(bq) = &self.base_quantity {
up = up.with_attr("BaseQuantity", bq.to_string())
}
e = e.with_element(up);
e = e.with_element(&up);

// ReductionListLineItem(s) and SurchargeListLineItem(s).
let mut reduction_and_surcharge_sum = Decimal::ZERO;
if let Some(reduction_and_surcharge) = &self.reduction_and_surcharge {
reduction_and_surcharge_sum = reduction_and_surcharge.sum();
e = e.with_element(reduction_and_surcharge.as_xml());
e = e.with_element(reduction_and_surcharge);
}

// TaxItem.
let taxable_amount = self.quantity * self.unit_price + reduction_and_surcharge_sum;
e = e.with_element(self.tax_item.as_xml(&taxable_amount));
e = e.with_element(&self.tax_item.taxable_amount(taxable_amount));

// LineItemAmount.
e = e.with_text_element("LineItemAmount", self.line_item_amount().to_string());

e
e.to_xml()
}
}

#[derive(Default)]
pub struct Details<'a> {
pub items: Vec<DetailsItem<'a>>,
pub(crate) struct Details<'a> {
pub(crate) items: Vec<DetailsItem<'a>>,
}

impl Details<'_> {
pub fn as_xml(&self) -> XmlElement {
impl ToXml for Details<'_> {
fn to_xml(&self) -> String {
let mut e = XmlElement::new("Details");

let mut ie = XmlElement::new("ItemList");
for item in &self.items {
ie = ie.with_element(item.as_xml());
ie = ie.with_element(item);
}
e = e.with_element(ie);
e = e.with_element(&ie);

e
e.to_xml()
}
}

Expand All @@ -167,7 +165,7 @@ mod tests {
use crate::{
reduction_and_surcharge::{ReductionAndSurchargeValue, SurchargeListLineItem},
tax::TaxCategory,
xml::XmlToString,
xml::ToXml,
};

#[test]
Expand All @@ -182,8 +180,7 @@ mod tests {
TaxItem::new(dec!(20), TaxCategory::S),
)
.with_description("Sand")
.as_xml()
.to_string();
.to_xml();

assert_eq!(
result,
Expand All @@ -203,8 +200,7 @@ mod tests {
TaxItem::new(dec!(20), TaxCategory::S),
)
.with_description("Sand")
.as_xml()
.to_string();
.to_xml();

assert_eq!(
result,
Expand All @@ -225,8 +221,7 @@ mod tests {
dec!(5),
ReductionAndSurchargeValue::Amount(dec!(2.3399)),
))
.as_xml()
.to_string();
.to_xml();

assert_eq!(
result,
Expand All @@ -247,8 +242,7 @@ mod tests {
dec!(5),
ReductionAndSurchargeValue::Amount(dec!(2)),
))
.as_xml()
.to_string();
.to_xml();

assert_eq!(
result,
Expand All @@ -268,8 +262,7 @@ mod tests {
TaxItem::new(dec!(20), TaxCategory::S),
)
.with_description("Sand")
.as_xml()
.to_string();
.to_xml();

assert_eq!(
result.as_str(),
Expand Down
5 changes: 3 additions & 2 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub enum DocumentType {
SubsequentDebit,
}

impl DocumentType {
pub fn as_str(&self) -> &str {
impl ToString for DocumentType {
fn to_string(&self) -> String {
match self {
DocumentType::CreditMemo => "CreditMemo",
DocumentType::FinalSettlement => "FinalSettlement",
Expand All @@ -21,5 +21,6 @@ impl DocumentType {
DocumentType::SubsequentCredit => "SubsequentCredit",
DocumentType::SubsequentDebit => "SubsequentDebit",
}
.to_owned()
}
}
14 changes: 9 additions & 5 deletions src/identification.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::xml::XmlElement;
use crate::xml::{ToXml, XmlElement};

pub enum FurtherIdentificationType {
ARA,
Expand All @@ -18,8 +18,8 @@ pub enum FurtherIdentificationType {
VN,
}

impl FurtherIdentificationType {
pub fn as_str(&self) -> &str {
impl ToString for FurtherIdentificationType {
fn to_string(&self) -> String {
match self {
FurtherIdentificationType::ARA => "ARA",
FurtherIdentificationType::BBG_GZ => "BBG_GZ",
Expand All @@ -36,6 +36,7 @@ impl FurtherIdentificationType {
FurtherIdentificationType::VID => "VID",
FurtherIdentificationType::VN => "VN",
}
.to_owned()
}
}

Expand All @@ -48,10 +49,13 @@ impl FurtherIdentification<'_> {
pub fn new(id: &str, id_type: FurtherIdentificationType) -> FurtherIdentification {
FurtherIdentification { id, id_type }
}
}

pub fn as_xml(&self) -> XmlElement {
impl ToXml for FurtherIdentification<'_> {
fn to_xml(&self) -> String {
XmlElement::new("FurtherIdentification")
.with_attr("IdentificationType", self.id_type.as_str())
.with_attr("IdentificationType", self.id_type.to_string())
.with_text(self.id)
.to_xml()
}
}
Loading

0 comments on commit b27dd01

Please sign in to comment.