| Crates.io | rgnucash |
| lib.rs | rgnucash |
| version | 0.1.2 |
| created_at | 2025-12-03 15:25:20.201821+00 |
| updated_at | 2025-12-03 15:25:20.201821+00 |
| description | FFI bindings to libgnucash engine |
| homepage | https://github.com/user/rgnucash |
| repository | https://github.com/greenpdx/rgnucash |
| max_upload_size | |
| id | 1964262 |
| size | 643,838 |
Rust FFI bindings and safe wrappers for the GnuCash accounting engine.
This workspace provides Rust access to GnuCash's core accounting functionality:
Install GnuCash development libraries:
Debian/Ubuntu:
sudo apt install gnucash libgnucash-dev libglib2.0-dev
Fedora:
sudo dnf install gnucash gnucash-devel glib2-devel
Arch Linux:
sudo pacman -S gnucash glib2
macOS (Homebrew):
brew install gnucash glib pkg-config
Rust 2024 edition (1.85+) is required.
The build script automatically detects GnuCash using pkg-config:
# Clone the repository
git clone https://github.com/user/gnucash-sys.git
cd gnucash-sys
# Build
cargo build
# Run tests
cargo test
# Build examples
cargo build --examples
If GnuCash is installed in a non-standard location, set environment variables:
# Using .env file
cat > .env << EOF
GNUCASH_SRC=/path/to/gnucash/source
GNUCASH_BUILD=/path/to/gnucash/build
EOF
# Or export directly
export GNUCASH_LIB=/usr/lib/gnucash
export GNUCASH_INCLUDE=/usr/include/gnucash
The build script searches for libraries in this order:
GNUCASH_LIB, GNUCASH_INCLUDE)gnucash, gnucash-engine)/usr/lib/gnucash, /usr/include/gnucash)use gnucash_sys::{init_engine, Book, Account, GNCAccountType, Numeric};
fn main() {
// Initialize the engine (required before any operations)
init_engine();
// Create a new book
let book = Book::new();
let root = book.root_account().expect("Book should have root");
// Create an account
let checking = Account::new(&book);
checking.begin_edit();
checking.set_name("Checking");
checking.set_type(GNCAccountType::ACCT_TYPE_BANK);
checking.commit_edit();
root.append_child(&checking);
// Check balance
println!("Balance: ${:.2}", checking.balance().to_f64());
}
use gnucash_sys::{init_engine, Session, SessionOpenMode};
fn main() {
init_engine();
match Session::open("/path/to/file.gnucash", SessionOpenMode::SESSION_READ_ONLY) {
Ok(session) => {
if let Some(book) = session.book() {
println!("Transactions: {}", book.transaction_count());
if let Some(root) = book.root_account() {
for account in root.children() {
println!("Account: {:?}", account.name());
}
}
}
session.end();
}
Err(e) => eprintln!("Failed to open: {:?}", e),
}
}
gnucash-sys/
├── src/ # Core FFI bindings and wrappers
│ ├── lib.rs # Main library entry
│ ├── book.rs # Book wrapper
│ ├── account.rs # Account wrapper
│ ├── transaction.rs # Transaction wrapper
│ ├── split.rs # Split wrapper
│ ├── session.rs # Session management
│ ├── price.rs # Price database
│ ├── types.rs # Guid, Numeric types
│ └── ...
├── examples/ # Example programs
├── docs/
│ └── API.md # Core API documentation
├── app/ # gnucash-ext crate
│ ├── src/
│ │ ├── lib.rs # Extended library
│ │ ├── business/ # Business entities
│ │ ├── query.rs # Query framework
│ │ ├── builder.rs # Builder patterns
│ │ └── ...
│ ├── examples/ # Business examples
│ ├── docs/
│ │ └── API.md # Extended API documentation
│ └── README.md
├── Cargo.toml # Workspace configuration
├── build.rs # Build script for bindgen
└── README.md # This file
# List available examples
cargo run --example
# Core examples
cargo run --example simple_book
cargo run --example simple_session
cargo run --example list_accounts /path/to/file.gnucash
cargo run --example account_tree /path/to/file.gnucash
cargo run --example create_transaction
cargo run --example export_csv /path/to/file.gnucash "Assets:Checking"
cargo run --example balance_sheet /path/to/file.gnucash
# Business examples (gnucash-ext)
cargo run -p gnucash-ext --example simple_business
| Type | Description |
|---|---|
Session |
Connection to GnuCash file/database |
Book |
Top-level container for financial data |
Account |
Ledger in hierarchical tree |
Transaction |
Double-entry accounting record |
Split |
Single entry linking amount to account |
Guid |
128-bit unique identifier |
Numeric |
Rational number for precise arithmetic |
Price |
Price quote for commodity |
PriceDB |
Database of prices |
| Type | Description |
|---|---|
Customer |
Customer for invoicing |
Vendor |
Vendor/supplier |
Employee |
Employee for expense vouchers |
Job |
Project linked to customer |
Invoice |
Invoice, bill, or voucher |
Entry |
Line item in invoice |
Owner |
Polymorphic owner type |
Query |
QofQuery for searching |
TransactionBuilder |
Fluent transaction creation |
InvoiceBuilder |
Fluent invoice creation |
The safe wrappers handle memory management via RAII (Drop trait). Key patterns:
begin_edit() before modifications, commit_edit() aftermark_unowned() after adding entities to hierarchiessession.end() when doneNote: The underlying GnuCash library is not thread-safe. While wrapper types implement Send, concurrent access requires external synchronization.
MIT
Contributions welcome! Please ensure: