Errors
Rust represents fallible operations with Result<T, E>. Target languages have different conventions: Swift uses throws and Kotlin uses exceptions. BoltFFI bridges these by converting Result return types into the native error handling mechanism of each platform. When a function returns Err, it becomes a thrown error in Swift or a thrown exception in Kotlin.
Supported Error Types
The error type in Result<T, E> can be:
Stringor&'static str- becomes a generic error with a message- A struct marked with
#[error]- becomes a structured error type - An enum marked with
#[error]- becomes an error enum
The #[error] attribute marks types as error types. In Swift, these types conform to Error. In Kotlin, they extend Exception.
String Errors
The simplest approach is returning Result<T, String> or Result<T, &'static str>. The error message is captured in a generic error type.
Struct Errors
For structured error information, define a struct with #[error]. The struct becomes a throwable error type in both languages.
Enum Errors
Error enums let you represent distinct failure cases. Simple enums (no associated data) become native enums. Enums with payloads become sealed types.
Simple Enums
Enums with Payloads
When enum variants carry associated data, the error becomes a sealed type hierarchy.
Async Errors
Async functions that return Result work the same way. The function becomes async throws in Swift and suspend with @Throws in Kotlin.
How It Works
When a function returns Result<T, E>, BoltFFI encodes both the success and error cases in the wire format. A tag byte indicates which case occurred: 0 for Ok, 1 for Err. The payload follows the tag.
On the target side, the generated code reads the tag, decodes the appropriate type, and either returns the value or throws the error. String errors are wrapped in FfiError/FfiException. Custom error types are thrown directly since they conform to Error (Swift) or extend Exception (Kotlin).