fatoora-rs-cli 0.1.2

Command-line interface for the fatoora ZATCA SDK
fatoora-rs-cli-0.1.2 is not a library.

fatoora-rs

MIT License codecov Crates.io Version Crates.io Version Crates.io Version PyPi Version

An unofficial open-source toolkit for everything you'd need for ZATCA (Zakat, Tax and Customs Authority of Saudi Arabia) Phase 1 and 2 compliant e-invoicing

...with bindings and support for many programming languages (so far only C/C++ and Python).

...and also built in Rust btw

fatoora-rs is in active early development. While the core functionality is usable, the public API is still evolving and may change as the project matures. We strive to maintain good test coverage and stability, but users should be aware that some rough edges may remain. Feedback and contributions are especially welcome at this stage.

Disclaimer: fatoora-rs is not affiliated, associated, authorized, endorsed by, or in any way officially connected with ZATCA (Zakat, Tax and Customs Authority), or any of its subsidiaries or its affiliates. The official ZATCA website can be found at https://zatca.gov.sa.

Documentation

  • Rust API: docs.rs
  • Project docs: see docs/ (MkDocs site)

Features

Everything done by the official ZATCA SDK

  • CSR Generation
  • Invoice Signing (All invoice types)
  • Validation (UBL only for now)
  • QR Generation
  • API Requests

But we do it faster and better e.g. ~190x faster invoice hashing than ZATCA's SDK (see bench/)

Dependencies

XML parsing/manipulation is done internally with libxml2, so you might need to install it if you haven't already. See here for relevant instructions.

C headers are generated in fatoora-ffi/include/, with grouped headers under fatoora/ (e.g., fatoora/config.h). Do not edit these generated headers manually; update the Rust sources and re-run the build to regenerate them.

Installation

The Rust library can be added with cargo add fatoora-core.

The cli tool can also be installed with cargo:

cargo install fatoora-rs-cli

Python bindings:

pip install fatoora-rs

Python modules mirror the Rust core layout (e.g., fatoora.config, fatoora.csr, fatoora.invoice, fatoora.sign).

Usage/Examples

Rust

use fatoora_core::config::EnvironmentType;
use fatoora_core::csr::{CsrProperties, SigningKey};

let props_text = std::fs::read_to_string("csr.properties")?;
let props = CsrProperties::from_properties_str(&props_text)?;
let key = SigningKey::generate();
let csr = props.build(&key, EnvironmentType::NonProduction)?;
let csr_pem = csr.to_pem()?;
let key_pem = key.to_pem()?;

Python

from fatoora.config import Environment
from fatoora.csr import CsrProperties, SigningKey

props = CsrProperties.parse_file("csr.properties")
key = SigningKey.generate()
csr = props.build(key, Environment.NON_PRODUCTION)
csr_pem = csr.to_pem_base64()
key_pem = key.to_pem()

CLI

fatoora-rs-cli csr --csr-config csr.properties --generated-csr csr.pem --private-key key.pem --pem

Rust

use fatoora_core::invoice::sign::InvoiceSigner;

let cert_pem = std::fs::read_to_string("cert.pem")?;
let key_pem = std::fs::read_to_string("key.pem")?;
let signer = InvoiceSigner::from_pem(cert_pem.trim(), key_pem.trim())?;
let xml = std::fs::read_to_string("invoice.xml")?;
let signed_xml = signer.sign_xml(&xml)?;

CLI

fatoora-rs-cli sign --invoice invoice.xml --cert cert.pem --key key.pem --signed-invoice signed.xml

Rust

use fatoora_core::config::Config;
use fatoora_core::invoice::validation::validate_xml_invoice_from_str;

let config = Config::new(fatoora_core::config::EnvironmentType::NonProduction);
let xml = std::fs::read_to_string("invoice.xml")?;
validate_xml_invoice_from_str(&xml, &config)?;

CLI

fatoora-rs-cli validate --invoice invoice.xml --xsd-path assets/schemas/UBL2.1/xsd/maindoc/UBL-Invoice-2.1.xsd

Rust

use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;

let xml = std::fs::read_to_string("signed.xml")?;
let signed = parse_signed_invoice_xml(&xml)?;
let qr = signed.qr_code();

CLI

fatoora-rs-cli qr --invoice signed.xml

Rust

use fatoora_core::invoice::xml::parse::parse_finalized_invoice_xml;

let xml = std::fs::read_to_string("invoice.xml")?;
let invoice = parse_finalized_invoice_xml(&xml)?;
let hash = invoice.hash_base64()?;

CLI

fatoora-rs-cli generate-hash --invoice invoice.xml

Rust

use fatoora_core::invoice::xml::parse::parse_signed_invoice_xml;

let xml = std::fs::read_to_string("signed.xml")?;
let signed = parse_signed_invoice_xml(&xml)?;
let payload = serde_json::json!({
    "invoiceHash": signed.invoice_hash(),
    "uuid": signed.uuid(),
    "invoice": signed.to_xml_base64(),
});

CLI

fatoora-rs-cli invoice-request --invoice signed.xml --api-request request.json

Contributing

Contributions are always welcome! See CONTRIBUTING.md for more details.

Roadmap

  • Increase test coverage to 100% (Inshallah)
  • Add the full validation suite (not only UBL schema)
  • Expand bindings to other languages (subject to demand)
  • PDF invoice generation

Relevant Links