Skip to main content

coil_commerce/
identifiers.rs

1use crate::CommerceModelError;
2use crate::validation::validate_token;
3use std::fmt;
4
5macro_rules! token_type {
6    ($name:ident, $field:literal) => {
7        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8        pub struct $name(String);
9
10        impl $name {
11            pub fn new(value: impl Into<String>) -> Result<Self, CommerceModelError> {
12                Ok(Self(validate_token($field, value.into())?))
13            }
14
15            pub fn as_str(&self) -> &str {
16                &self.0
17            }
18        }
19
20        impl fmt::Display for $name {
21            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22                f.write_str(&self.0)
23            }
24        }
25    };
26}
27
28token_type!(ProductId, "product_id");
29token_type!(ProductHandle, "product_handle");
30token_type!(CollectionId, "collection_id");
31token_type!(CollectionHandle, "collection_handle");
32token_type!(OrderId, "order_id");
33token_type!(CheckoutId, "checkout_id");
34token_type!(RefundId, "refund_id");
35token_type!(Sku, "sku");
36token_type!(CurrencyCode, "currency");
37token_type!(EntitlementKey, "entitlement_key");