use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MetafieldOwner {
Product,
Variant,
Customer,
Order,
Shop,
Collection,
Page,
Blog,
Article,
}
impl MetafieldOwner {
#[must_use]
pub const fn to_path_segment(&self) -> &'static str {
match self {
Self::Product => "products",
Self::Variant => "variants",
Self::Customer => "customers",
Self::Order => "orders",
Self::Shop => "", Self::Collection => "collections",
Self::Page => "pages",
Self::Blog => "blogs",
Self::Article => "articles",
}
}
}
impl fmt::Display for MetafieldOwner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::Product => "product",
Self::Variant => "variant",
Self::Customer => "customer",
Self::Order => "order",
Self::Shop => "shop",
Self::Collection => "collection",
Self::Page => "page",
Self::Blog => "blog",
Self::Article => "article",
};
write!(f, "{s}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_metafield_owner_serialization() {
let owner = MetafieldOwner::Product;
let json = serde_json::to_string(&owner).unwrap();
assert_eq!(json, "\"product\"");
let owner = MetafieldOwner::Customer;
let json = serde_json::to_string(&owner).unwrap();
assert_eq!(json, "\"customer\"");
let owner = MetafieldOwner::Shop;
let json = serde_json::to_string(&owner).unwrap();
assert_eq!(json, "\"shop\"");
}
#[test]
fn test_metafield_owner_deserialization() {
let owner: MetafieldOwner = serde_json::from_str("\"product\"").unwrap();
assert_eq!(owner, MetafieldOwner::Product);
let owner: MetafieldOwner = serde_json::from_str("\"variant\"").unwrap();
assert_eq!(owner, MetafieldOwner::Variant);
let owner: MetafieldOwner = serde_json::from_str("\"order\"").unwrap();
assert_eq!(owner, MetafieldOwner::Order);
}
#[test]
fn test_metafield_owner_to_path_segment() {
assert_eq!(MetafieldOwner::Product.to_path_segment(), "products");
assert_eq!(MetafieldOwner::Variant.to_path_segment(), "variants");
assert_eq!(MetafieldOwner::Customer.to_path_segment(), "customers");
assert_eq!(MetafieldOwner::Order.to_path_segment(), "orders");
assert_eq!(MetafieldOwner::Shop.to_path_segment(), "");
assert_eq!(MetafieldOwner::Collection.to_path_segment(), "collections");
assert_eq!(MetafieldOwner::Page.to_path_segment(), "pages");
assert_eq!(MetafieldOwner::Blog.to_path_segment(), "blogs");
assert_eq!(MetafieldOwner::Article.to_path_segment(), "articles");
}
#[test]
fn test_metafield_owner_display() {
assert_eq!(format!("{}", MetafieldOwner::Product), "product");
assert_eq!(format!("{}", MetafieldOwner::Customer), "customer");
assert_eq!(format!("{}", MetafieldOwner::Shop), "shop");
assert_eq!(format!("{}", MetafieldOwner::Collection), "collection");
}
}