Crates.io | google_taxonomy |
lib.rs | google_taxonomy |
version | 0.3.2 |
source | src |
created_at | 2021-08-13 12:38:41.297737 |
updated_at | 2021-08-27 11:42:57.762735 |
description | Struct with all Google Product Categories / Taxonomy |
homepage | https://github.com/jmagnusson/google-taxonomy |
repository | https://github.com/jmagnusson/google-taxonomy |
max_upload_size | |
id | 435684 |
size | 511,883 |
The purpose of this crate is to more easily work with Google Product Categories / Taxonomy.
This is provided via the google_taxonomy::ProductCategory
struct which contains all categories that exist as of 2021-08-13.
The ProductCategory contains each product category as an associated constant. They are translated from the taxonomy-with-ids.en-US.txt file as follows:
id
method).-
are removed&
are replaced with And
For example 1604 - Apparel & Accessories > Clothing
becomes ProductCategory::ApparelAndAccessoriesClothing
.
Each constant takes up 2 bytes of memory.
use std::convert::TryInto;
use google_taxonomy::ProductCategory;
let cat: ProductCategory = 3237.try_into().unwrap();
assert_eq!(cat, ProductCategory::AnimalsAndPetSuppliesLiveAnimals);
use google_taxonomy::ProductCategory;
assert_eq!(ProductCategory::AnimalsAndPetSuppliesLiveAnimals.id(), 3237);
use google_taxonomy::ProductCategory;
assert_eq!(ProductCategory::AnimalsAndPetSuppliesLiveAnimals.to_string(), "Animals & Pet Supplies > Live Animals");
#[cfg(feature = "serde")]
{
use serde::{Deserialize, Serialize};
use google_taxonomy::ProductCategory;
#[derive(Deserialize, Serialize, Debug, PartialEq)]
struct Product {
category: ProductCategory,
}
let serialized = r#"{"category":"Animals & Pet Supplies"}"#;
// Deserialize, e.g. with serde_json
let deserialized: Product = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, Product { category: ProductCategory::AnimalsAndPetSupplies });
// And back to its original serialized form again...
assert_eq!(serde_json::to_string(&deserialized).unwrap(), serialized);
}