From c953e72c133af411d7b9a4c231c236bd6684f811 Mon Sep 17 00:00:00 2001 From: Stuart Harris Date: Mon, 23 Sep 2024 13:59:52 +0100 Subject: [PATCH] types improvements --- wasm-components/rust/Cargo.lock | 1 + wasm-components/rust/data-init/Cargo.toml | 2 +- wasm-components/rust/data-init/src/lib.rs | 72 ++++++++++--------- .../rust/http-controller/Cargo.toml | 2 +- .../rust/http-controller/src/lib.rs | 54 +++++++------- .../rust/notification-service/Cargo.toml | 1 + .../rust/notification-service/src/lib.rs | 8 ++- .../rust/orders-service/Cargo.toml | 2 +- .../rust/orders-service/src/lib.rs | 9 ++- .../rust/products-service/Cargo.toml | 2 +- .../rust/products-service/src/lib.rs | 19 ++--- 11 files changed, 93 insertions(+), 79 deletions(-) diff --git a/wasm-components/rust/Cargo.lock b/wasm-components/rust/Cargo.lock index 86169a0..4bf9921 100644 --- a/wasm-components/rust/Cargo.lock +++ b/wasm-components/rust/Cargo.lock @@ -201,6 +201,7 @@ version = "0.1.0" dependencies = [ "serde", "serde_json", + "uuid", "wit-bindgen 0.32.0", ] diff --git a/wasm-components/rust/data-init/Cargo.toml b/wasm-components/rust/data-init/Cargo.toml index 9f659fa..45720a4 100644 --- a/wasm-components/rust/data-init/Cargo.toml +++ b/wasm-components/rust/data-init/Cargo.toml @@ -9,5 +9,5 @@ crate-type = ["cdylib"] [dependencies] serde.workspace = true serde_json.workspace = true -uuid = { workspace = true, features = ["v4"] } +uuid = { workspace = true, features = ["serde", "v4"] } wit-bindgen.workspace = true diff --git a/wasm-components/rust/data-init/src/lib.rs b/wasm-components/rust/data-init/src/lib.rs index e158e86..48a5bee 100644 --- a/wasm-components/rust/data-init/src/lib.rs +++ b/wasm-components/rust/data-init/src/lib.rs @@ -143,112 +143,114 @@ impl Guest for Component { fn sample_products() -> Vec { vec![ SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Car Engine"), description: String::from("V8 engine with 500 horsepower"), - price: 8500, + price: Pence(8500), sku: String::from("ENG-V8-500"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Brake Pads"), description: String::from("High performance brake pads"), - price: 150, + price: Pence(150), sku: String::from("BRK-PD-HP"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Air Filter"), description: String::from("Premium air filter for increased airflow"), - price: 30, + price: Pence(30), sku: String::from("AIR-FLT-PREM"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Spark Plugs"), description: String::from("High-efficiency spark plugs"), - price: 60, + price: Pence(60), sku: String::from("SPK-PLG-HI-EFF"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Tire Set"), description: String::from("Set of 4 all-season tires"), - price: 600, + price: Pence(600), sku: String::from("TIR-SET-AS"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Battery"), description: String::from("High-capacity car battery"), - price: 120, + price: Pence(120), sku: String::from("BAT-HC-12V"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Windshield Wipers"), description: String::from("All-weather windshield wipers"), - price: 45, + price: Pence(45), sku: String::from("WND-WPR-AW"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Fuel Pump"), description: String::from("Electric fuel pump for efficient fuel delivery"), - price: 220, + price: Pence(220), sku: String::from("FL-PMP-ELEC"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Radiator"), description: String::from("High-efficiency car radiator"), - price: 320, + price: Pence(320), sku: String::from("RAD-HI-EFF"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Headlights"), description: String::from("LED headlights with long lifespan"), - price: 250, + price: Pence(250), sku: String::from("HDL-LED-LONG"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Alternator"), description: String::from("High output alternator for enhanced performance"), - price: 300, + price: Pence(300), sku: String::from("ALT-HO-ENH"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Exhaust System"), description: String::from("Performance exhaust system"), - price: 750, + price: Pence(750), sku: String::from("EXH-SYS-PERF"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Suspension Kit"), description: String::from("Complete suspension kit for improved handling"), - price: 900, + price: Pence(900), sku: String::from("SUS-KIT-IMP"), }, SerializableProduct { - id: Uuid::new_v4().to_string(), + id: Uuid::new_v4(), name: String::from("Turbocharger"), description: String::from("High-performance turbocharger"), - price: 1400, + price: Pence(1400), sku: String::from("TRB-CHR-HP"), }, ] } #[derive(Serialize, Deserialize)] -pub struct SerializableProduct { - /// UUID - pub id: String, - pub name: String, - pub description: String, - pub price: i32, - pub sku: String, +struct Pence(i32); + +#[derive(Serialize, Deserialize)] +struct SerializableProduct { + id: Uuid, + name: String, + description: String, + price: Pence, + sku: String, } diff --git a/wasm-components/rust/http-controller/Cargo.toml b/wasm-components/rust/http-controller/Cargo.toml index ce63025..cc2ad94 100644 --- a/wasm-components/rust/http-controller/Cargo.toml +++ b/wasm-components/rust/http-controller/Cargo.toml @@ -11,6 +11,6 @@ anyhow.workspace = true form_urlencoded = "1.2.1" routefinder = "0.5.4" serde.workspace = true -uuid = { workspace = true, features = ["v4", "serde"] } +uuid = { workspace = true, features = ["serde"] } waki = { version = "0.3.1", features = ["json"] } wit-bindgen.workspace = true diff --git a/wasm-components/rust/http-controller/src/lib.rs b/wasm-components/rust/http-controller/src/lib.rs index 51d0401..336617f 100644 --- a/wasm-components/rust/http-controller/src/lib.rs +++ b/wasm-components/rust/http-controller/src/lib.rs @@ -150,7 +150,7 @@ impl Handlers { match request.method() { Method::Get => { let skus = &query[KEY]; - let skus: Vec = skus.split(',').map(String::from).collect(); + let skus: Vec = skus.split(',').map(Into::into).collect(); match get_inventory(&skus) { Ok(inventory) => { @@ -288,22 +288,10 @@ mod response { } } -impl TryFrom<&Order> for SerializableOrder { - type Error = anyhow::Error; - - fn try_from(order: &Order) -> Result { - Ok(SerializableOrder { - order_number: order.order_number.parse()?, - total: Pence(order.total), - line_items: order.line_items.iter().map(Into::into).collect(), - }) - } -} - #[derive(Serialize, Deserialize, Default)] -pub struct SerializableAvailability { - pub sku: String, - pub is_in_stock: bool, +struct SerializableAvailability { + sku: String, + is_in_stock: bool, } impl From<&Availability> for SerializableAvailability { @@ -320,9 +308,9 @@ struct Pence(i32); #[derive(Serialize, Deserialize)] struct SerializableLineItem { - pub sku: String, - pub price: Pence, - pub quantity: i32, + sku: String, + price: Pence, + quantity: i32, } impl From<&SerializableLineItem> for LineItem { @@ -352,13 +340,25 @@ struct SerializableOrder { total: Pence, } +impl TryFrom<&Order> for SerializableOrder { + type Error = anyhow::Error; + + fn try_from(order: &Order) -> Result { + Ok(SerializableOrder { + order_number: order.order_number.parse()?, + total: Pence(order.total), + line_items: order.line_items.iter().map(Into::into).collect(), + }) + } +} + #[derive(Serialize, Deserialize)] -pub struct SerializableProduct { - pub id: Uuid, - pub name: String, - pub description: String, - pub price: i32, - pub sku: String, +struct SerializableProduct { + id: Uuid, + name: String, + description: String, + price: Pence, + sku: String, } impl TryFrom<&Product> for SerializableProduct { @@ -369,7 +369,7 @@ impl TryFrom<&Product> for SerializableProduct { id: value.id.parse()?, name: value.name.clone(), description: value.description.clone(), - price: value.price, + price: Pence(value.price), sku: value.sku.clone(), }) } @@ -381,7 +381,7 @@ impl From<&SerializableProduct> for Product { id: val.id.to_string(), name: val.name.clone(), description: val.description.clone(), - price: val.price, + price: val.price.0, sku: val.sku.clone(), } } diff --git a/wasm-components/rust/notification-service/Cargo.toml b/wasm-components/rust/notification-service/Cargo.toml index b0d1400..fec385e 100644 --- a/wasm-components/rust/notification-service/Cargo.toml +++ b/wasm-components/rust/notification-service/Cargo.toml @@ -9,4 +9,5 @@ crate-type = ["cdylib"] [dependencies] serde.workspace = true serde_json.workspace = true +uuid = { workspace = true, features = ["serde"] } wit-bindgen.workspace = true diff --git a/wasm-components/rust/notification-service/src/lib.rs b/wasm-components/rust/notification-service/src/lib.rs index e33754c..1dab58c 100644 --- a/wasm-components/rust/notification-service/src/lib.rs +++ b/wasm-components/rust/notification-service/src/lib.rs @@ -9,6 +9,7 @@ wit_bindgen::generate!({ }); use serde::{Deserialize, Serialize}; +use uuid::Uuid; use exports::wasmcloud::messaging::handler::{BrokerMessage, Guest}; use wasi::logging::logging::{log, Level}; @@ -22,7 +23,10 @@ impl Guest for Component { "NOTIFICATION-SERVICE-HANDLE-MESSAGE: failed to deserialize order notification", ); - loud_print("recieved order number", ¬ification.order_number); + loud_print( + "recieved order number", + ¬ification.order_number.to_string(), + ); Ok(()) } @@ -45,5 +49,5 @@ fn loud_print(msg: &str, data: &str) { #[derive(Serialize, Deserialize, Default)] struct OrderNotification { - order_number: String, + order_number: Uuid, } diff --git a/wasm-components/rust/orders-service/Cargo.toml b/wasm-components/rust/orders-service/Cargo.toml index 60988b6..4d4d0c8 100644 --- a/wasm-components/rust/orders-service/Cargo.toml +++ b/wasm-components/rust/orders-service/Cargo.toml @@ -10,5 +10,5 @@ crate-type = ["cdylib"] indoc = "2.0.5" serde.workspace = true serde_json.workspace = true -uuid = { workspace = true, features = ["v4"] } +uuid = { workspace = true, features = ["serde", "v4"] } wit-bindgen.workspace = true diff --git a/wasm-components/rust/orders-service/src/lib.rs b/wasm-components/rust/orders-service/src/lib.rs index d76d896..6f362a4 100644 --- a/wasm-components/rust/orders-service/src/lib.rs +++ b/wasm-components/rust/orders-service/src/lib.rs @@ -81,11 +81,14 @@ impl Guest for Component { .iter() .fold(0, |acc, item| acc + item.price * item.quantity); - let order_number = Uuid::new_v4().to_string(); + let order_number = Uuid::new_v4(); query( &sql, - &[PgValue::Text(order_number.clone()), PgValue::Integer(total)], + &[ + PgValue::Text(order_number.to_string()), + PgValue::Integer(total), + ], ) .map_err(|e| { let msg = format!("Failed to insert order: {:?}", e); @@ -214,5 +217,5 @@ impl Guest for Component { #[derive(Serialize, Deserialize, Default)] struct OrderNotification { - pub order_number: String, + order_number: Uuid, } diff --git a/wasm-components/rust/products-service/Cargo.toml b/wasm-components/rust/products-service/Cargo.toml index dfe7716..fe70480 100644 --- a/wasm-components/rust/products-service/Cargo.toml +++ b/wasm-components/rust/products-service/Cargo.toml @@ -11,5 +11,5 @@ anyhow.workspace = true log = "0.4.21" serde.workspace = true serde_json.workspace = true -uuid = { workspace = true, features = ["serde", "v4"] } +uuid = { workspace = true, features = ["serde"] } wit-bindgen.workspace = true diff --git a/wasm-components/rust/products-service/src/lib.rs b/wasm-components/rust/products-service/src/lib.rs index e8c87aa..0128cbd 100644 --- a/wasm-components/rust/products-service/src/lib.rs +++ b/wasm-components/rust/products-service/src/lib.rs @@ -81,12 +81,15 @@ impl Guest for ProductComponent { } #[derive(Serialize, Deserialize)] -pub struct SerializableProduct { - pub id: Uuid, - pub name: String, - pub description: String, - pub price: i32, - pub sku: String, +struct Pence(i32); + +#[derive(Serialize, Deserialize)] +struct SerializableProduct { + id: Uuid, + name: String, + description: String, + price: Pence, + sku: String, } impl TryFrom for SerializableProduct { @@ -97,7 +100,7 @@ impl TryFrom for SerializableProduct { id: value.id.parse()?, name: value.name, description: value.description, - price: value.price, + price: Pence(value.price), sku: value.sku, }) } @@ -109,7 +112,7 @@ impl From for Product { id: val.id.to_string(), name: val.name, description: val.description, - price: val.price, + price: val.price.0, sku: val.sku, } }