1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! Shipping zone and method operations for rate calculation
//!
//! Manages shipping zones (geographic regions) and their associated shipping methods
//! (e.g., standard, express) with rate calculation based on destination.
//!
//! # Example
//!
//! ```rust,ignore
//! use stateset_embedded::{Commerce, CreateShippingZone};
//!
//! let commerce = Commerce::new("./store.db")?;
//!
//! let zone = commerce.shipping_zones().create(CreateShippingZone {
//! name: "US Domestic".into(),
//! countries: vec!["US".into()],
//! ..Default::default()
//! })?;
//!
//! println!("Zone created: {}", zone.name);
//! # Ok::<(), stateset_embedded::CommerceError>(())
//! ```
use stateset_core::{
CreateShippingZone, CreateZoneShippingMethod, Result, ShippingMethodId, ShippingZone,
ShippingZoneFilter, ShippingZoneId, UpdateShippingZone, ZoneShippingMethod,
ZoneShippingMethodFilter, ZoneShippingRate, ZoneShippingRateRequest,
};
use stateset_db::Database;
use std::sync::Arc;
/// Shipping zone and method operations.
pub struct ShippingZones {
db: Arc<dyn Database>,
}
impl std::fmt::Debug for ShippingZones {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ShippingZones").finish_non_exhaustive()
}
}
impl ShippingZones {
pub(crate) fn new(db: Arc<dyn Database>) -> Self {
Self { db }
}
// ========================================================================
// Zone Operations
// ========================================================================
/// Create a new shipping zone.
///
/// # Example
///
/// ```rust,ignore
/// use stateset_embedded::{Commerce, CreateShippingZone};
///
/// let commerce = Commerce::new("./store.db")?;
///
/// let zone = commerce.shipping_zones().create(CreateShippingZone {
/// name: "Europe".into(),
/// countries: vec!["DE".into(), "FR".into(), "IT".into()],
/// ..Default::default()
/// })?;
/// # Ok::<(), stateset_embedded::CommerceError>(())
/// ```
pub fn create(&self, input: CreateShippingZone) -> Result<ShippingZone> {
self.db.shipping_zones().create(input)
}
/// Get a shipping zone by ID.
pub fn get(&self, id: ShippingZoneId) -> Result<Option<ShippingZone>> {
self.db.shipping_zones().get(id)
}
/// Update a shipping zone.
pub fn update(&self, id: ShippingZoneId, input: UpdateShippingZone) -> Result<ShippingZone> {
self.db.shipping_zones().update(id, input)
}
/// List shipping zones with optional filtering.
pub fn list(&self, filter: ShippingZoneFilter) -> Result<Vec<ShippingZone>> {
self.db.shipping_zones().list(filter)
}
/// Delete a shipping zone.
pub fn delete(&self, id: ShippingZoneId) -> Result<()> {
self.db.shipping_zones().delete(id)
}
/// Find shipping zones matching a destination address.
///
/// Returns all zones whose geographic criteria match the given country,
/// region, and postal code.
pub fn find_matching_zones(
&self,
country: &str,
region: Option<&str>,
postal_code: Option<&str>,
) -> Result<Vec<ShippingZone>> {
self.db.shipping_zones().find_matching_zones(country, region, postal_code)
}
// ========================================================================
// Shipping Method Operations
// ========================================================================
/// Create a shipping method within a zone.
pub fn create_method(&self, input: CreateZoneShippingMethod) -> Result<ZoneShippingMethod> {
self.db.zone_shipping_methods().create(input)
}
/// Get a shipping method by ID.
pub fn get_method(&self, id: ShippingMethodId) -> Result<Option<ZoneShippingMethod>> {
self.db.zone_shipping_methods().get(id)
}
/// List shipping methods with optional filtering.
pub fn list_methods(
&self,
filter: ZoneShippingMethodFilter,
) -> Result<Vec<ZoneShippingMethod>> {
self.db.zone_shipping_methods().list(filter)
}
/// Delete a shipping method.
pub fn delete_method(&self, id: ShippingMethodId) -> Result<()> {
self.db.zone_shipping_methods().delete(id)
}
/// Calculate shipping rates for a destination.
///
/// Returns available rates across all matching zones and methods.
pub fn calculate_rates(
&self,
request: ZoneShippingRateRequest,
) -> Result<Vec<ZoneShippingRate>> {
self.db.zone_shipping_methods().calculate_rates(request)
}
}