1 个不稳定版本

使用旧 Rust 2015

0.1.0 2016年7月29日

#299国际化(i18n)

MIT 许可证

16KB
286

增值税

Build Status

简介

这是一个模块,用于验证增值税号的格式和存在性。它查询了VIES API,该API极其不稳定,经常有一个国家的服务不可用。

它还包含欧盟国家的当前增值税率。它不包含任何历史数据或降低的税率。

待办事项:查看当国家数据库关闭时API返回的内容

安装

将以下内容添加到 Cargo.toml 中

vat = "0.1"

用法

增值税号验证

use vat::{validate_format, validate_vat_number, VatError};

// vat_number should contian the country code
// it can contain spaces or dashes, those will be stripped by `vat`
fn is_vat_number_valid(vat_number: &str) -> bool {
    // checks if the format is ok first
    if !validate_format(vat_number) {
        return false;
    }

    // format is valid, check that this is an existing VAT number
    match validate_vat_number(vat_number) {
        Ok(_) => true,
        Err(e) => match e {
            // could be the api is down, allow it since the format is ok
            // in practice you would probably refuse it
            VatError::HttpError(_) => true,
            _ => false,
        }
    }
}

查看 src/errors.rs 了解可能发生的错误类型。

在上述情况下,我们只进行验证,但 validate_vat_number 函数将返回以下结构体的公司信息

pub struct Company {
    pub country_code: String,
    pub vat_number: String,
    pub name: String,
    pub address: String,
}

获取增值税率

use vat::{get_rate, get_all_rates};

// casing is not important
let fr_rate = get_rate("fr").unwrap();
let all_rates = get_all_rates();

依赖关系

~9MB
~201K SLoC