SFST Rust Binding
This is a Rust binding for the SFST (Stuttgart Finite State Transducer Tools) library. It provides a safe, idiomatic Rust interface to the C++ SFST library for finite state transducer operations.
Features
- Safe API: Memory-safe Rust wrapper around the C++ SFST library
- Two API styles: Functional API and RAII-style API for automatic resource management
- Error handling: Comprehensive error types with descriptive messages
- Python-compatible: API mirrors the existing Python binding
Prerequisites
- Rust toolchain (1.70+)
- C++ compiler (g++ or clang++)
- SFST source files (should be in
../src/relative to this directory)
Building
-
Clone or place this
rust/directory alongside the SFST source code:project/ ├── src/ # SFST C++ source files ├── python/ # Python bindings (optional) └── rust/ # This Rust binding -
Build the library:
-
Run tests:
-
Run the example/test binary:
Usage
Functional API
use sfst;
// Initialize transducer from file
init?;
// Analyze a surface form
let analysis = analyse?;
println!; // ["easy<ADJ><comp>"]
// Generate a surface form
let generation = generate?;
println!; // ["easier"]
// Manual cleanup
cleanup;
RAII API (Recommended)
use Sfst;
// Initialize with automatic cleanup
let sfst = new?;
// Analyze and generate
let analysis = sfst.analyse?;
let generation = sfst.generate?;
// Automatic cleanup when `sfst` goes out of scope
Error Handling
use ;
match new
API Reference
Functions
init(filename: &str) -> Result<(), SfstError>- Initialize transducer from fileanalyse(input: &str) -> Result<Vec<String>, SfstError>- Analyze a stringgenerate(input: &str) -> Result<Vec<String>, SfstError>- Generate a stringcleanup()- Manual cleanup (not needed with RAII API)
RAII Wrapper
Sfst::new(filename: &str) -> Result<Sfst, SfstError>- Create with automatic cleanupsfst.analyse(input: &str) -> Result<Vec<String>, SfstError>- Analyze a stringsfst.generate(input: &str) -> Result<Vec<String>, SfstError>- Generate a string
Error Types
File Structure
rust/
├── Cargo.toml # Package configuration
├── README.md # This file
├── build.rs # Build script for C++ compilation
└── src/
├── lib.rs # Main library code
├── main.rs # Example/test binary
├── sfst_wrapper.h # C header for FFI
└── sfst_wrapper.cpp # C++ wrapper implementation
Testing
The crate includes comprehensive tests that mirror the Python test suite:
# Run library tests
# Run the example binary (requires test files)
The tests expect the test file python/tests/easy.a to exist. Make sure you have the complete SFST project structure.
License
This binding follows the same license as the original SFST library.