by couchand · 6y ago · 0 comments
This might sound like it could get cumbersome fast, but it's actually really simple in practice, due to clever uses of traits. UnverifiedInvoiceId implements rocket::FromParam, so I can deserialize it from a URL parameter. InvoiceId is only created in the serde::Deserialize implementation of an invoice-returning API response.
So a (significantly simplified) endpoint in the app might look like:
#[get("/invoices/<unverified_invoice_id>")]
pub fn get_invoice_by_id(
unverified_invoice_id: UnverifiedInvoiceId,
api_conn: ApiConnection,
) -> HtmlResponse {
// API "search" methods take unverified user input as parameters
let invoice = api_conn.fetch_invoice_by_id(unverified_invoice_id);
// API "lookup" methods take verified ids
let line_items = api_conn.fetch_invoice_line_items(invoice.id);
render_invoice(invoice, line_items);
}
I can't accidentally switch up things that don't belong together, even though under the hood they're all just unsigned ints.It is possible to do something like this in nearly any language with a type system.
Serde -> Zod (https://github.com/vriad/zod)
Diesel -> Prisma
Yet you expose yourself to concepts that are unique to Rust that do not enhance the developer experience (such as lifetimes). I don't need to worry about lifetimes in typescript and can just focus on shipping code.
Lifetimes is just one example, but there are others that create more friction than they provide benefit IMO.