use crate::xml_utils::TagContent;
use crate::xml_utils::tag;
#[derive(Debug, Clone)]
pub struct TaxId<'a> {
value: &'a str,
}
impl<'a> TaxId<'a> {
pub fn new(value: &'a str) -> Self {
Self { value }
}
pub fn is_cpf(&self) -> bool {
self.value.len() <= 11
}
pub fn tag_name(&self) -> &'static str {
if self.is_cpf() { "CPF" } else { "CNPJ" }
}
pub fn padded(&self) -> String {
let width = if self.is_cpf() { 11 } else { 14 };
format!("{:0>width$}", self.value, width = width)
}
pub fn to_xml_tag(&self) -> String {
tag(
self.tag_name(),
&[],
TagContent::Text(&self.padded_static()),
)
}
fn padded_static(&self) -> String {
self.padded()
}
}