Skip to content

Commit

Permalink
enhance: follow clippy suggestions on xsender (project-openubl#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 authored Aug 11, 2024
1 parent 8d418ea commit 19bc2a4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
46 changes: 24 additions & 22 deletions xsender/src/analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use log::warn;
use anyhow::anyhow;

use crate::constants::{
BOLETA_SERIE_REGEX, FACTURA_SERIE_REGEX, GUIA_REMISION_REMITENTE_SERIE_REGEX,
Expand Down Expand Up @@ -71,44 +71,46 @@ pub fn filename_formatted_without_extension(
document_type: &str,
document_id: &str,
ruc: &str,
) -> Option<String> {
) -> anyhow::Result<String> {
match document_type {
DocumentType::INVOICE => {
if FACTURA_SERIE_REGEX.is_match(document_id) {
Some(format!("{ruc}-{}-{document_id}", Catalog1::FACTURA))
} else if BOLETA_SERIE_REGEX.is_match(document_id) {
Some(format!("{ruc}-{}-{document_id}", Catalog1::BOLETA))
if FACTURA_SERIE_REGEX.as_ref()?.is_match(document_id) {
Ok(format!("{ruc}-{}-{document_id}", Catalog1::FACTURA))
} else if BOLETA_SERIE_REGEX.as_ref()?.is_match(document_id) {
Ok(format!("{ruc}-{}-{document_id}", Catalog1::BOLETA))
} else {
warn!("Could not build filename from Invoice");
None
Err(anyhow!("Could not build filename from Invoice"))
}
}
DocumentType::CREDIT_NOTE => {
Some(format!("{ruc}-{}-{document_id}", Catalog1::NOTA_CREDITO))
}
DocumentType::DEBIT_NOTE => Some(format!("{ruc}-{}-{document_id}", Catalog1::NOTA_DEBITO)),
DocumentType::CREDIT_NOTE => Ok(format!("{ruc}-{}-{document_id}", Catalog1::NOTA_CREDITO)),
DocumentType::DEBIT_NOTE => Ok(format!("{ruc}-{}-{document_id}", Catalog1::NOTA_DEBITO)),
DocumentType::VOIDED_DOCUMENTS | DocumentType::SUMMARY_DOCUMENTS => {
Some(format!("{ruc}-{document_id}"))
Ok(format!("{ruc}-{document_id}"))
}
DocumentType::PERCEPTION => Some(format!("{ruc}-{}-{document_id}", Catalog1::PERCEPCION)),
DocumentType::RETENTION => Some(format!("{ruc}-{}-{document_id}", Catalog1::RETENCION)),
DocumentType::PERCEPTION => Ok(format!("{ruc}-{}-{document_id}", Catalog1::PERCEPCION)),
DocumentType::RETENTION => Ok(format!("{ruc}-{}-{document_id}", Catalog1::RETENCION)),
DocumentType::DESPATCH_ADVICE => {
if GUIA_REMISION_REMITENTE_SERIE_REGEX.is_match(document_id) {
Some(format!(
if GUIA_REMISION_REMITENTE_SERIE_REGEX
.as_ref()?
.is_match(document_id)
{
Ok(format!(
"{ruc}-{}-{document_id}",
Catalog1::GUIA_REMISION_REMITENTE
))
} else if GUIA_REMISION_TRANSPORTISTA_SERIE_REGEX.is_match(document_id) {
Some(format!(
} else if GUIA_REMISION_TRANSPORTISTA_SERIE_REGEX
.as_ref()?
.is_match(document_id)
{
Ok(format!(
"{ruc}-{}-{document_id}",
Catalog1::GUIA_REMISION_TRANSPORTISTA
))
} else {
warn!("Could not build filename from DespatchAdvice");
None
Err(anyhow!("Could not build filename from DespatchAdvice"))
}
}
_ => None,
_ => Err(anyhow!("Not supported document")),
}
}

Expand Down
14 changes: 10 additions & 4 deletions xsender/src/client_sunat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ impl ClientSUNAT {
file: &File,
credentials: &Credentials,
) -> Result<SendFileResponse, ClientSunatErr> {
let client = HTTP_CLIENT
.as_ref()
.map_err(|_error| ClientSunatErr::Any(anyhow!("Could not initialize client")))?;

match &target {
SendFileTarget::Soap(url, action) => {
let envelope_body = match action {
Expand Down Expand Up @@ -98,8 +102,7 @@ impl ClientSUNAT {

let body = envelope.to_string_xml()?;

let response = HTTP_CLIENT
.clone()
let response = client
.post(url)
.body(body)
.header("Content-Type", "text/xml; charset=utf-8")
Expand Down Expand Up @@ -127,6 +130,10 @@ impl ClientSUNAT {
ticket: &str,
credentials: &Credentials,
) -> Result<VerifyTicketResponse, ClientSunatErr> {
let client = HTTP_CLIENT
.as_ref()
.map_err(|_error| ClientSunatErr::Any(anyhow!("Could not initialize client")))?;

match &target {
VerifyTicketTarget::Soap(url) => {
let envelope = EnvelopeData {
Expand All @@ -137,8 +144,7 @@ impl ClientSUNAT {

let body = envelope.to_string_xml()?;

let response = HTTP_CLIENT
.clone()
let response = client
.post(url)
.body(body)
.header("Content-Type", "text/xml; charset=utf-8")
Expand Down
10 changes: 5 additions & 5 deletions xsender/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ lazy_static::lazy_static! {
tera
};

pub static ref FACTURA_SERIE_REGEX: Regex = Regex::new("^[F|f].*$").expect("Invalid FACTURA_SERIE_REGEX");
pub static ref BOLETA_SERIE_REGEX: Regex = Regex::new("^[B|f].*$").expect("Invalid BOLETA_SERIE_REGEX");
pub static ref GUIA_REMISION_REMITENTE_SERIE_REGEX: Regex = Regex::new("^[T|t].*$").expect("Invalid GUIA_REMISION_REMITENTE_SERIE_REGEX");
pub static ref GUIA_REMISION_TRANSPORTISTA_SERIE_REGEX: Regex = Regex::new("^[V|v].*$").expect("Invalid GUIA_REMISION_TRANSPORTISTA_SERIE_REGEX");
pub static ref HTTP_CLIENT: Client = Client::builder().connection_verbose(true).build().expect("Could not create HTTP client");
pub static ref FACTURA_SERIE_REGEX: Result<Regex, regex::Error> = Regex::new("^[F|f].*$");
pub static ref BOLETA_SERIE_REGEX: Result<Regex, regex::Error> = Regex::new("^[B|f].*$");
pub static ref GUIA_REMISION_REMITENTE_SERIE_REGEX: Result<Regex, regex::Error> = Regex::new("^[T|t].*$");
pub static ref GUIA_REMISION_TRANSPORTISTA_SERIE_REGEX: Result<Regex, regex::Error> = Regex::new("^[V|v].*$");
pub static ref HTTP_CLIENT: Result<Client, reqwest::Error> = Client::builder().connection_verbose(true).build();
}
2 changes: 1 addition & 1 deletion xsender/src/file_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl FileSender {
&metadata.document_id,
&metadata.ruc,
)
.ok_or(FileSenderErr::TargetDiscovery)?;
.map_err(|_| FileSenderErr::TargetDiscovery)?;

let send_target = send_file_target(
&metadata.document_type,
Expand Down
2 changes: 1 addition & 1 deletion xsigner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl XSigner {
let mut sigctx = XmlSecSignatureContext::new();
sigctx.insert_key(private_key);

sigctx.sign_document(xml).expect("Failed to sign document");
sigctx.sign_document(xml)?;

Ok(())
}
Expand Down

0 comments on commit 19bc2a4

Please sign in to comment.