Skip to content

Commit

Permalink
types improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Sep 23, 2024
1 parent 8887b62 commit c953e72
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 79 deletions.
1 change: 1 addition & 0 deletions wasm-components/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wasm-components/rust/data-init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
72 changes: 37 additions & 35 deletions wasm-components/rust/data-init/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,112 +143,114 @@ impl Guest for Component {
fn sample_products() -> Vec<SerializableProduct> {
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,
}
2 changes: 1 addition & 1 deletion wasm-components/rust/http-controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 27 additions & 27 deletions wasm-components/rust/http-controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Handlers {
match request.method() {
Method::Get => {
let skus = &query[KEY];
let skus: Vec<String> = skus.split(',').map(String::from).collect();
let skus: Vec<String> = skus.split(',').map(Into::into).collect();

match get_inventory(&skus) {
Ok(inventory) => {
Expand Down Expand Up @@ -288,22 +288,10 @@ mod response {
}
}

impl TryFrom<&Order> for SerializableOrder {
type Error = anyhow::Error;

fn try_from(order: &Order) -> Result<Self, Self::Error> {
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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -352,13 +340,25 @@ struct SerializableOrder {
total: Pence,
}

impl TryFrom<&Order> for SerializableOrder {
type Error = anyhow::Error;

fn try_from(order: &Order) -> Result<Self, Self::Error> {
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 {
Expand All @@ -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(),
})
}
Expand All @@ -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(),
}
}
Expand Down
1 change: 1 addition & 0 deletions wasm-components/rust/notification-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ crate-type = ["cdylib"]
[dependencies]
serde.workspace = true
serde_json.workspace = true
uuid = { workspace = true, features = ["serde"] }
wit-bindgen.workspace = true
8 changes: 6 additions & 2 deletions wasm-components/rust/notification-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -22,7 +23,10 @@ impl Guest for Component {
"NOTIFICATION-SERVICE-HANDLE-MESSAGE: failed to deserialize order notification",
);

loud_print("recieved order number", &notification.order_number);
loud_print(
"recieved order number",
&notification.order_number.to_string(),
);

Ok(())
}
Expand All @@ -45,5 +49,5 @@ fn loud_print(msg: &str, data: &str) {

#[derive(Serialize, Deserialize, Default)]
struct OrderNotification {
order_number: String,
order_number: Uuid,
}
2 changes: 1 addition & 1 deletion wasm-components/rust/orders-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions wasm-components/rust/orders-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -214,5 +217,5 @@ impl Guest for Component {

#[derive(Serialize, Deserialize, Default)]
struct OrderNotification {
pub order_number: String,
order_number: Uuid,
}
2 changes: 1 addition & 1 deletion wasm-components/rust/products-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 11 additions & 8 deletions wasm-components/rust/products-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Product> for SerializableProduct {
Expand All @@ -97,7 +100,7 @@ impl TryFrom<Product> for SerializableProduct {
id: value.id.parse()?,
name: value.name,
description: value.description,
price: value.price,
price: Pence(value.price),
sku: value.sku,
})
}
Expand All @@ -109,7 +112,7 @@ impl From<SerializableProduct> for Product {
id: val.id.to_string(),
name: val.name,
description: val.description,
price: val.price,
price: val.price.0,
sku: val.sku,
}
}
Expand Down

0 comments on commit c953e72

Please sign in to comment.