https://github.com/jacobsvante/google-taxonomy
Rust enum with all Google Product Categories / Taxonomy
https://github.com/jacobsvante/google-taxonomy
google google-merchant taxonomy
Last synced: 8 months ago
JSON representation
Rust enum with all Google Product Categories / Taxonomy
- Host: GitHub
- URL: https://github.com/jacobsvante/google-taxonomy
- Owner: jacobsvante
- Created: 2021-08-13T12:16:22.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-27T11:42:12.000Z (almost 5 years ago)
- Last Synced: 2025-09-15T11:33:08.271Z (9 months ago)
- Topics: google, google-merchant, taxonomy
- Language: Rust
- Homepage: https://crates.io/crates/google_taxonomy
- Size: 131 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Google Taxonomy / Product Categories
The purpose of this crate is to more easily work with [Google Product Categories / Taxonomy](https://support.google.com/merchants/answer/6324436).
This is provided via the `google_taxonomy::ProductCategory` struct which contains all categories that exist as of 2021-08-13.
## Associated constants naming
The ProductCategory contains each product category as an associated constant. They are translated from the [taxonomy-with-ids.en-US.txt](https://www.google.com/basepages/producttype/taxonomy-with-ids.en-US.txt) file as follows:
1. The leading ID is removed (and can be obtained using the `id` method).
2. The immediately following characters ` - ` are removed
3. All occurrences of `&` are replaced with `And`
4. Finally, all non-alphanumeric ascii characters are removed
For example `1604 - Apparel & Accessories > Clothing` becomes `ProductCategory::ApparelAndAccessoriesClothing`.
Each constant takes up 2 bytes of memory.
## Examples
### Try to parse an integer as a product category (i.e. from its ID)
```rust
use std::convert::TryInto;
use google_taxonomy::ProductCategory;
let cat: ProductCategory = 3237.try_into().unwrap();
assert_eq!(cat, ProductCategory::AnimalsAndPetSuppliesLiveAnimals);
```
### Get the number representation of the product category
```rust
use google_taxonomy::ProductCategory;
assert_eq!(ProductCategory::AnimalsAndPetSuppliesLiveAnimals.id(), 3237);
```
### Get the name of a product category
```rust
use google_taxonomy::ProductCategory;
assert_eq!(ProductCategory::AnimalsAndPetSuppliesLiveAnimals.to_string(), "Animals & Pet Supplies > Live Animals");
```
### Serialize / deserialize with Serde
```rust
#[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);
}
```