Skip to content

Commit 6a86cbc

Browse files
committed
Update rust functions
1 parent 056e0e7 commit 6a86cbc

7 files changed

+99
-65
lines changed

discounts/rust/discount/default/shopify.extension.toml.liquid

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
api_version = "2025-04"
1+
api_version = "unstable"
22

33
[[extensions]]
44
name = "t:name"
@@ -19,6 +19,8 @@ description = "t:description"
1919

2020
[extensions.build]
2121
command = "cargo build --target=wasm32-wasip1 --release"
22-
path = "target/wasm32-wasip1/release/{{handle | replace: " ", "-" | downcase}}.wasm"
22+
path = "target/wasm32-wasip1/release/discount-function-rust.wasm/{{handle | replace: " ", "-" | downcase}}.wasm"
2323
watch = [ "src/**/*.rs" ]
2424

25+
[extensions.ui]
26+
handle = "{{handle}}"

discounts/rust/network/default/shopify.extension.toml.liquid

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
api_version = "2025-04"
1+
api_version = "unstable"
22

33
[[extensions]]
44
name = "t:name"
@@ -29,6 +29,8 @@ description = "t:description"
2929

3030
[extensions.build]
3131
command = "cargo build --target=wasm32-wasip1 --release"
32-
path = "target/wasm32-wasip1/release/{{handle | replace: " ", "-" | downcase}}.wasm"
32+
path = "target/wasm32-wasip1/release/function-network-rust.wasm/{{handle | replace: " ", "-" | downcase}}.wasm"
3333
watch = [ "src/**/*.rs" ]
3434

35+
[extensions.ui]
36+
handle = "{{handle}}"

discounts/rust/network/default/src/generate_cart_fetch.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ fn generate_cart_fetch(
2222
let json_body = json!({ "enteredDiscountCodes": entered_discount_codes });
2323

2424
let request = CartFetchHttpRequest {
25-
headers: vec![CartFetchHttpRequestHeader {
26-
name: "accept".to_string(),
27-
value: "application/json".to_string(),
28-
}],
29-
method: CartFetchHttpRequestMethod::GET,
25+
headers: vec![
26+
CartFetchHttpRequestHeader {
27+
name: "accept".to_string(),
28+
value: "application/json".to_string(),
29+
},
30+
CartFetchHttpRequestHeader {
31+
name: "Content-Type".to_string(),
32+
value: "application/json".to_string(),
33+
}
34+
],
35+
method: CartFetchHttpRequestMethod::POST,
3036
policy: CartFetchHttpRequestPolicy {
3137
read_timeout_ms: 2000,
3238
},
33-
url: "<external server url>".to_string(),
39+
url: "<external-server-url>".to_string(),
3440
body: Some(json_body.to_string()),
3541
json_body: Some(json_body.clone()),
3642
};
@@ -59,15 +65,21 @@ mod tests {
5965
let json_body = json!({ "enteredDiscountCodes": [] });
6066
let expected = FunctionCartFetchResult {
6167
request: Some(CartFetchHttpRequest {
62-
headers: vec![CartFetchHttpRequestHeader {
63-
name: "accept".to_string(),
64-
value: "application/json".to_string(),
65-
}],
66-
method: CartFetchHttpRequestMethod::GET,
68+
headers: vec![
69+
CartFetchHttpRequestHeader {
70+
name: "accept".to_string(),
71+
value: "application/json".to_string(),
72+
},
73+
CartFetchHttpRequestHeader {
74+
name: "Content-Type".to_string(),
75+
value: "application/json".to_string(),
76+
}
77+
],
78+
method: CartFetchHttpRequestMethod::POST,
6779
policy: CartFetchHttpRequestPolicy {
6880
read_timeout_ms: 2000,
6981
},
70-
url: "<external server url>".to_string(),
82+
url: "<external-server-url>".to_string(),
7183
json_body: Some(json_body.clone()),
7284
body: Some(json_body.to_string()),
7385
}),

discounts/rust/network/default/src/generate_cart_run.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ use cart_lines_discounts_generate_run::input::ResponseData;
1010

1111
#[derive(Deserialize)]
1212
#[serde(rename_all = "camelCase")]
13-
struct DiscountResponse {
14-
operations: Operations,
15-
}
16-
17-
#[derive(Deserialize)]
18-
#[serde(rename_all = "camelCase")]
19-
struct Operations {
13+
struct OperationItem {
2014
#[serde(default)]
2115
add_product_discounts: Option<ProductDiscounts>,
2216
#[serde(default)]
2317
add_order_discounts: Option<OrderDiscounts>,
2418
#[serde(default)]
2519
add_discount_code_validations: Option<ValidDiscountCodes>,
20+
// Ignore other operation types that might be in the response but we don't use in cart context
21+
#[serde(flatten)]
22+
_other: std::collections::HashMap<String, serde_json::Value>,
2623
}
2724

2825
#[shopify_function_target(
@@ -32,25 +29,31 @@ struct Operations {
3229
)]
3330
fn generate_cart_run(input: ResponseData) -> Result<FunctionCartRunResult> {
3431
let fetch_result = input.fetch_result.ok_or("Missing fetch result")?;
35-
let body = fetch_result.body.ok_or("Missing response body")?;
3632

37-
// Parse the response body directly into our types
38-
let response: DiscountResponse =
39-
serde_json::from_str(&body).map_err(|e| format!("Failed to parse JSON: {}", e))?;
33+
// Use jsonBody which is the only available property
34+
let json_body = fetch_result.json_body.ok_or("Missing json_body in response")?;
35+
36+
// Parse using the JSON value
37+
let operation_items = serde_json::from_value::<Vec<OperationItem>>(json_body)
38+
.map_err(|e| format!("Failed to convert jsonBody: {}", e))?;
4039

4140
// Convert the response into operations
4241
let mut operations = Vec::new();
4342

44-
if let Some(validations) = response.operations.add_discount_code_validations {
45-
operations.push(CartOperation::AddDiscountCodeValidations(validations));
46-
}
43+
// Process each operation item
44+
for item in operation_items {
45+
if let Some(validations) = item.add_discount_code_validations {
46+
operations.push(CartOperation::AddDiscountCodeValidations(validations));
47+
}
4748

48-
if let Some(product_discounts) = response.operations.add_product_discounts {
49-
operations.push(CartOperation::AddProductDiscounts(product_discounts));
50-
}
49+
if let Some(product_discounts) = item.add_product_discounts {
50+
operations.push(CartOperation::AddProductDiscounts(product_discounts));
51+
}
5152

52-
if let Some(order_discounts) = response.operations.add_order_discounts {
53-
operations.push(CartOperation::AddOrderDiscounts(order_discounts));
53+
if let Some(order_discounts) = item.add_order_discounts {
54+
operations.push(CartOperation::AddOrderDiscounts(order_discounts));
55+
}
56+
// Ignore delivery discounts for cart operations
5457
}
5558

5659
Ok(FunctionCartRunResult { operations })

discounts/rust/network/default/src/generate_delivery_fetch.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,21 @@ fn generate_delivery_fetch(
2222
let json_body = json!({ "enteredDiscountCodes": entered_discount_codes });
2323

2424
let request = DeliveryFetchHttpRequest {
25-
headers: vec![DeliveryFetchHttpRequestHeader {
26-
name: "accept".to_string(),
27-
value: "application/json".to_string(),
28-
}],
29-
method: DeliveryFetchHttpRequestMethod::GET,
25+
headers: vec![
26+
DeliveryFetchHttpRequestHeader {
27+
name: "accept".to_string(),
28+
value: "application/json".to_string(),
29+
},
30+
DeliveryFetchHttpRequestHeader {
31+
name: "Content-Type".to_string(),
32+
value: "application/json".to_string(),
33+
}
34+
],
35+
method: DeliveryFetchHttpRequestMethod::POST,
3036
policy: DeliveryFetchHttpRequestPolicy {
3137
read_timeout_ms: 2000,
3238
},
33-
url: "<external server url>".to_string(),
39+
url: "<external-server-url>".to_string(),
3440
body: Some(json_body.to_string()),
3541
json_body: Some(json_body.clone()),
3642
};
@@ -59,15 +65,21 @@ mod tests {
5965
let json_body = json!({ "enteredDiscountCodes": ["ABC"] });
6066
let expected = FunctionDeliveryFetchResult {
6167
request: Some(DeliveryFetchHttpRequest {
62-
headers: vec![DeliveryFetchHttpRequestHeader {
63-
name: "accept".to_string(),
64-
value: "application/json".to_string(),
65-
}],
66-
method: DeliveryFetchHttpRequestMethod::GET,
68+
headers: vec![
69+
DeliveryFetchHttpRequestHeader {
70+
name: "accept".to_string(),
71+
value: "application/json".to_string(),
72+
},
73+
DeliveryFetchHttpRequestHeader {
74+
name: "Content-Type".to_string(),
75+
value: "application/json".to_string(),
76+
}
77+
],
78+
method: DeliveryFetchHttpRequestMethod::POST,
6779
policy: DeliveryFetchHttpRequestPolicy {
6880
read_timeout_ms: 2000,
6981
},
70-
url: "<external server url>".to_string(),
82+
url: "<external-server-url>".to_string(),
7183
json_body: Some(json_body.clone()),
7284
body: Some(json_body.to_string()),
7385
}),

discounts/rust/network/default/src/generate_delivery_run.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ use cart_delivery_options_discounts_generate_run::input::ResponseData;
1010

1111
#[derive(Deserialize)]
1212
#[serde(rename_all = "camelCase")]
13-
struct DiscountResponse {
14-
operations: Operations,
15-
}
16-
17-
#[derive(Deserialize)]
18-
#[serde(rename_all = "camelCase")]
19-
struct Operations {
13+
struct OperationItem {
2014
#[serde(default)]
2115
add_delivery_discounts: Option<DeliveryDiscounts>,
2216
#[serde(default)]
2317
add_discount_code_validations: Option<ValidDiscountCodes>,
18+
// Ignore any other fields we don't need
19+
#[serde(flatten)]
20+
_other: std::collections::HashMap<String, serde_json::Value>,
2421
}
2522

2623
#[shopify_function_target(
@@ -30,21 +27,27 @@ struct Operations {
3027
)]
3128
fn generate_delivery_run(input: ResponseData) -> Result<FunctionDeliveryRunResult> {
3229
let fetch_result = input.fetch_result.ok_or("Missing fetch result")?;
33-
let body = fetch_result.body.ok_or("Missing response body")?;
3430

35-
// Parse the response body directly into our types
36-
let response: DiscountResponse =
37-
serde_json::from_str(&body).map_err(|e| format!("Failed to parse JSON: {}", e))?;
31+
// Use jsonBody which is the only available property
32+
let json_body = fetch_result.json_body.ok_or("Missing json_body in response")?;
33+
34+
// Parse using the JSON value
35+
let operation_items = serde_json::from_value::<Vec<OperationItem>>(json_body)
36+
.map_err(|e| format!("Failed to convert jsonBody: {}", e))?;
3837

3938
// Convert the response into operations
4039
let mut operations = Vec::new();
4140

42-
if let Some(validations) = response.operations.add_discount_code_validations {
43-
operations.push(DeliveryOperation::AddDiscountCodeValidations(validations));
44-
}
41+
// Process each operation item
42+
for item in operation_items {
43+
if let Some(validations) = item.add_discount_code_validations {
44+
operations.push(DeliveryOperation::AddDiscountCodeValidations(validations));
45+
}
4546

46-
if let Some(delivery_discounts) = response.operations.add_delivery_discounts {
47-
operations.push(DeliveryOperation::AddDeliveryDiscounts(delivery_discounts));
47+
if let Some(delivery_discounts) = item.add_delivery_discounts {
48+
operations.push(DeliveryOperation::AddDeliveryDiscounts(delivery_discounts));
49+
}
50+
// Ignore cart/order discounts for delivery operations
4851
}
4952

5053
Ok(FunctionDeliveryRunResult { operations })

discounts/rust/network/default/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::process;
2-
pub mod generate_cart_fetch;
32
pub mod generate_cart_run;
4-
pub mod generate_delivery_fetch;
53
pub mod generate_delivery_run;
4+
pub mod generate_cart_fetch;
5+
pub mod generate_delivery_fetch;
66

77
fn main() {
88
eprintln!("Please invoke a named export.");

0 commit comments

Comments
 (0)