facture_rs 0.1.2

A wrapper for the FactureApp API service.
Documentation
use crate::belling::utils::{core::{comprobante::Comprobante, enums::BillingType, error::BillingError}, json::{entity::EntityWrapper, json_raw::JsonRaw}};


#[derive(serde::Deserialize, serde::Serialize)]
pub struct JsonInput {
  pub bill_type: BillingType,
  json: JsonRaw,
  bearer: String
}

impl JsonInput {
  pub fn new() -> Self{
    JsonInput{
      bill_type: BillingType::JSON,
      bearer: String::new(),
      json: JsonRaw::new()
    }
  }

  pub fn set_comprobantes(mut self, comprobantes: impl Into<Vec<Comprobante>>) -> Self{
    self.json.entity.data.comprobantes = comprobantes.into();
    self
  }

  pub fn set_comprobante(mut self, comprobante: impl Into<Comprobante>) -> Self{
    self.json.entity.data.comprobantes.push(comprobante.into());
    self
  }

  pub fn set_sucursal(mut self, id: impl Into<usize>) -> Self{
    self.json.entity.data.sucursal.id = id.into();
    self
  }

  pub fn set_emisor(mut self, id: impl Into<usize>) -> Self{
    self.json.entity.data.emisor.id = id.into();
    self
  }

  pub fn set_template(mut self, template: impl Into<String>) -> Self{
    self.json.entity.data.template = Some(template.into());
    self
  }

  pub fn set_bearer(mut self, bearer: impl Into<String>) -> Self{
    self.bearer = bearer.into();
    self
  }

  pub fn get_bearer(&self) -> String{
    self.bearer.clone()
  }

  pub fn get_json(&self) -> Result<String, BillingError> {
    let entity = EntityWrapper{ entity: &self.json.entity };
    serde_json::to_string_pretty(&entity).map_err(BillingError::from)
  }

}