saleor-apps-rs/sitemap-generator/src/sitemap/mod.rs

168 lines
4.1 KiB
Rust
Raw Normal View History

2024-07-03 14:07:04 +00:00
mod category;
mod collection;
mod event_handler;
mod page;
mod product;
2024-07-10 21:51:59 +00:00
use std::ops::{Deref, DerefMut};
2024-07-03 14:07:04 +00:00
use serde::{Deserialize, Serialize};
2024-07-10 21:51:59 +00:00
use tinytemplate::TinyTemplate;
use crate::{
app::SitemapConfig,
queries::{
event_subjects_updated::{Category, Collection, Page, Product, ProductUpdated},
get_all_categories_n_products::Product,
},
};
2024-07-03 14:07:04 +00:00
const SITEMAP_XMLNS: &str = "http://sitemaps.org/schemas/sitemap/0.9";
const SALEOR_REF_XMLNS: &str = "http://app-sitemap-generator.kremik.sk/xml-schemas/saleor-ref.xsd";
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
2024-07-03 14:07:04 +00:00
#[serde(rename = "urlset")]
pub struct UrlSet {
2024-07-10 21:51:59 +00:00
pub urls: Vec<Url>,
2024-07-03 14:07:04 +00:00
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
2024-07-03 14:07:04 +00:00
pub struct Url {
2024-07-10 21:51:59 +00:00
pub url: String,
pub data: ItemData,
pub related: Option<ItemData>,
2024-07-03 14:07:04 +00:00
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
2024-07-10 21:51:59 +00:00
pub struct ItemData {
pub id: String,
pub slug: String,
pub typ: ItemType,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub enum ItemType {
2024-07-03 14:07:04 +00:00
Product,
Category,
Collection,
Page,
2024-07-03 14:07:04 +00:00
}
impl UrlSet {
pub fn new() -> Self {
2024-07-10 21:51:59 +00:00
Self { urls: vec![] }
}
}
impl Deref for UrlSet {
type Target = Vec<Url>;
fn deref(&self) -> &Self::Target {
&self.urls
2024-07-03 14:07:04 +00:00
}
2024-07-10 21:51:59 +00:00
}
2024-07-10 21:51:59 +00:00
impl DerefMut for UrlSet {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.urls
}
2024-07-03 14:07:04 +00:00
}
impl Url {
2024-07-10 21:51:59 +00:00
pub fn new_product(
sitemap_config: &SitemapConfig,
product: Product,
) -> Result<Self, NewUrlError> {
let category = product
.category
.as_ref()
.ok_or(NewUrlError::MissingData)?
.clone();
let data = ItemData {
id: product.id.inner().to_owned(),
slug: product.slug.clone(),
typ: ItemType::Product,
};
let related = Some(ItemData {
id: category.id.inner().to_owned(),
slug: category.slug,
typ: ItemType::Category,
});
let mut tt = TinyTemplate::new();
tt.add_template("t", &sitemap_config.product_template);
let url = tt.render("t", &product)?;
Ok(Self { url, data, related })
2024-07-03 14:07:04 +00:00
}
2024-07-10 21:51:59 +00:00
pub fn new_category(
sitemap_config: &SitemapConfig,
category: Category,
) -> Result<Self, NewUrlError> {
let data = ItemData {
id: category.id.inner().to_owned(),
slug: category.slug.clone(),
typ: ItemType::Category,
};
2024-07-10 21:51:59 +00:00
let mut tt = TinyTemplate::new();
tt.add_template("t", &sitemap_config.category_template);
let url = tt.render("t", &category)?;
Ok(Self {
url,
data,
related: None,
})
2024-07-03 14:07:04 +00:00
}
2024-07-10 21:51:59 +00:00
pub fn new_collection(
sitemap_config: &SitemapConfig,
collection: Collection,
) -> Result<Self, NewUrlError> {
let data = ItemData {
id: collection.id.inner().to_owned(),
slug: collection.slug.clone(),
typ: ItemType::Collection,
};
let mut tt = TinyTemplate::new();
tt.add_template("t", &sitemap_config.collection_template);
let url = tt.render("t", &collection)?;
Ok(Self {
url,
data,
related: None,
})
}
pub fn new_page(sitemap_config: &SitemapConfig, page: Page) -> Result<Self, NewUrlError> {
let data = ItemData {
id: page.id.inner().to_owned(),
slug: page.slug.clone(),
typ: ItemType::Page,
};
let mut tt = TinyTemplate::new();
tt.add_template("t", &sitemap_config.pages_template);
let url = tt.render("t", &page)?;
Ok(Self {
url,
data,
related: None,
})
}
}
#[derive(thiserror::Error, Debug)]
pub enum NewUrlError {
#[error("Some property inside passed data for new url was None, but should've been Some")]
MissingData,
#[error("Bad templates or wrong context data to fill out the template")]
BadTemplating(#[from] tinytemplate::error::Error),
2024-07-03 14:07:04 +00:00
}