Use structured types instead of generic strings in API definitions to improve type safety and clarity. Prefer enums over string literals, specific ID types over generic strings, and maintain consistent data type usage across request/response models.
Use structured types instead of generic strings in API definitions to improve type safety and clarity. Prefer enums over string literals, specific ID types over generic strings, and maintain consistent data type usage across request/response models.
Key practices:
pub status: SubscriptionStatus not pub status: Stringmerchant_id: impl Into<id_type::MerchantId> not merchant_id: Stringconnector: Connector not connector: Stringbase_url: Url not base_url: StringExample:
// Good - structured types
#[derive(Debug, Clone, serde::Serialize)]
pub struct CreateSubscriptionResponse {
pub status: SubscriptionStatus, // enum instead of String
pub merchant_id: id_type::MerchantId, // specific ID type
pub connector: Connector, // enum instead of String
}
// Avoid - generic strings
pub struct CreateSubscriptionResponse {
pub status: String, // unclear what values are valid
pub merchant_id: String, // loses type safety
pub connector: String, // no validation
}
This approach prevents runtime errors, improves API documentation, enables better IDE support, and makes the codebase more maintainable.
Enter the URL of a public GitHub repository