nfe-core 0.1.2

Core types and error handling for NF-e
Documentation
//! Configuração do cliente

use crate::{
    UfBrasil,
    error::{Error, Result},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ambiente {
    Homologacao,
    Producao,
}

impl Ambiente {
    pub fn codigo(&self) -> u8 {
        match self {
            Ambiente::Homologacao => 2,
            Ambiente::Producao => 1,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Config {
    pub cnpj: String,
    pub uf: UfBrasil,
    pub ambiente: Ambiente,
    pub timeout_secs: u64,
    pub tentar_novamente: bool,
    pub max_tentativas: u32,
}

impl Config {
    pub fn validar(&self) -> Result<()> {
        if self.cnpj.len() != 14 {
            return Err(Error::Config("CNPJ deve ter 14 dígitos".to_string()));
        }
        if !self.cnpj.chars().all(|c| c.is_ascii_digit()) {
            return Err(Error::Config("CNPJ deve conter apenas números".to_string()));
        }
        Ok(())
    }

    pub fn with_timeout(mut self, timeout_secs: u64) -> Self {
        self.timeout_secs = timeout_secs;
        self
    }

    pub fn with_retry(mut self, tentar: bool, max_tentativas: u32) -> Self {
        self.tentar_novamente = tentar;
        self.max_tentativas = max_tentativas;
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_new() {
        let config = {
            let cnpj = "99999999999999".to_string();
            let uf = UfBrasil::SP;
            let ambiente = Ambiente::Homologacao;
            Config {
                cnpj,
                uf,
                ambiente,
                timeout_secs: 30,
                tentar_novamente: true,
                max_tentativas: 3,
            }
        };
        assert_eq!(config.cnpj, "99999999999999");
        assert_eq!(config.timeout_secs, 30);
    }

    #[test]
    fn test_config_validar_sucesso() {
        let config = {
            let cnpj = "12345678000199".to_string();
            let uf = UfBrasil::SP;
            let ambiente = Ambiente::Producao;
            Config {
                cnpj,
                uf,
                ambiente,
                timeout_secs: 30,
                tentar_novamente: true,
                max_tentativas: 3,
            }
        };
        assert!(config.validar().is_ok());
    }

    #[test]
    fn test_config_validar_cnpj_invalido() {
        let config = {
            let cnpj = "123".to_string();
            let uf = UfBrasil::SP;
            let ambiente = Ambiente::Producao;
            Config {
                cnpj,
                uf,
                ambiente,
                timeout_secs: 30,
                tentar_novamente: true,
                max_tentativas: 3,
            }
        };
        assert!(config.validar().is_err());
    }

    #[test]
    fn test_config_validar_cnpj_invalido_com_letras() {
        let config = {
            let cnpj = "1234teste".to_string();
            let uf = UfBrasil::SP;
            let ambiente = Ambiente::Producao;
            Config {
                cnpj,
                uf,
                ambiente,
                timeout_secs: 30,
                tentar_novamente: true,
                max_tentativas: 3,
            }
        };
        assert!(config.validar().is_err());
    }

    #[test]
    fn test_ambiente_codigo() {
        assert_eq!(Ambiente::Homologacao.codigo(), 2);
        assert_eq!(Ambiente::Producao.codigo(), 1);
    }
}