Lago Types
A comprehensive type library for the Lago billing system, providing Rust data structures for API requests, responses, and domain models.
Overview
This crate contains all the type definitions needed to interact with the Lago billing API, including:
- Models: Core domain objects like
Customer,Invoice,ActivityLogObject,ApiLogObject,AppliedCoupon,UsageThreshold - Requests: Structured request types for API operations
- Responses: Typed responses from API endpoints
- Filters: Query parameter builders for list operations
- Error handling: Common error types and API error responses
Features
- Type Safety: Strongly typed structs and enums for all API interactions
- Serialization: Full serde support for JSON serialization/deserialization
- String Parsing: Built-in
FromStrimplementations for enums to parse from string representations - Filtering: Composable filter builders for list queries
- Pagination: Built-in pagination support with metadata
- Documentation: Comprehensive documentation for all public APIs
String to Enum Conversion
The library provides convenient FromStr implementations for all enums, allowing easy conversion from string representations to typed enum values. This is particularly useful when working with external APIs or user input.
Usage Examples
use FromStr;
use ;
// Using FromStr::from_str()
let invoice_type = from_str.unwrap;
assert_eq!;
// Error handling for invalid values
let result: = "invalid_type".parse;
assert!;
Usage
Add this crate to your Cargo.toml:
[]
= { = "../lago-types" }
Basic Example
use FromStr;
use ;
// Parse enum values from strings
let status: InvoiceStatus = "finalized".parse.unwrap;
let payment_status: InvoicePaymentStatus = "pending".parse.unwrap;
let invoice_type = from_str.unwrap;
// Create a request to list invoices with filters
let request = new
.with_pagination
.with_filters;
// Convert to query parameters
let params = request.to_query_params;
Invoice Preview
Preview an invoice before creating it:
use ;
// Preview for an existing customer with a new subscription
let preview = for_customer
.with_plan_code
.with_billing_time;
let request = new;
// Preview with inline customer details
let customer = new
.with_name
.with_currency;
let preview = new
.with_plan_code
.with_subscription_at;
// Preview with coupons
let coupon = new
.with_percentage;
let preview = for_customer
.with_plan_code
.with_coupons;
// Preview for existing subscriptions with plan upgrade
let subscriptions = new
.with_plan_code;
let preview = for_customer
.with_subscriptions;
### Activity Logs
Fetch activity logs:
```rust
use FromStr;
use ;
// Parse activity source from string
let source = from_str.unwrap;
// Create a request to list activity logs with filters
let request = new
.with_pagination
.with_filters;
// Get a specific activity log by activity ID
let get_request = new;
API Logs
Fetch API logs:
use FromStr;
use ;
// Parse HTTP method from string
let method = from_str.unwrap;
// Create a request to list API logs with filters
let request = new
.with_pagination
.with_filters;
// Get a specific API log by request ID
let get_request = new;
Applied Coupons
Work with applied coupons:
use ;
// Create input to apply a coupon to a customer
let apply_input = new;
// Apply with a fixed amount discount
let apply_input = new
.with_fixed_amount
.with_frequency;
// Apply with a percentage discount
let apply_input = new
.with_percentage_rate
.with_frequency
.with_frequency_duration; // Apply for 3 billing periods
let request = new;
// Create a request to list applied coupons with filters
let list_request = new
.with_pagination
.with_filters;
// Convert to query parameters
let params = list_request.to_query_params;
Module Structure
models/- Core domain models and data structuresrequests/- Request types for API operationsresponses/- Response types from API endpointsfilters/- Filter builders for list operationserror.rs- Error types and handling
Release
Before publishing a release
cargo check
cargo test
cargo doc --no-deps --open
cargo package
Run the release
cargo login API_KEY
cargo publish
License
This project is licensed under the MIT License - see the LICENSE file for details.