use serde_with::skip_serializing_none;
use serde::{Deserialize, Serialize};
use crate::{AttributeSortOrder, AttributeType};
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttributeCreate {
name: String,
slug: Option<String>,
#[serde(rename = "type")]
attribute_type: Option<AttributeType>,
order_by: Option<AttributeSortOrder>,
has_archives: Option<bool>,
}
#[derive(Default)]
pub struct AttributeCreateBuilder<N> {
name: N,
slug: Option<String>,
attribute_type: Option<AttributeType>,
order_by: Option<AttributeSortOrder>,
has_archives: Option<bool>,
}
#[derive(Default)]
pub struct WithName(String);
#[derive(Default)]
pub struct NoName;
impl<N> AttributeCreateBuilder<N> {
pub fn name(self, name: impl Into<String>) -> AttributeCreateBuilder<WithName> {
AttributeCreateBuilder {
name: WithName(name.into()),
slug: self.slug,
attribute_type: self.attribute_type,
order_by: self.order_by,
has_archives: self.has_archives,
}
}
pub fn slug(mut self, slug: impl Into<String>) -> Self {
let _ = self.slug.insert(slug.into());
self
}
pub fn attribute_type(mut self, attribute_type: AttributeType) -> Self {
let _ = self.attribute_type.insert(attribute_type);
self
}
pub fn order_by(mut self, order_by: AttributeSortOrder) -> Self {
let _ = self.order_by.insert(order_by);
self
}
pub fn enable_archives(mut self) -> Self {
let _ = self.has_archives.insert(true);
self
}
}
impl AttributeCreateBuilder<WithName> {
pub fn build(self) -> AttributeCreate {
AttributeCreate {
name: self.name.0,
slug: self.slug,
attribute_type: self.attribute_type,
order_by: self.order_by,
has_archives: self.has_archives,
}
}
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttributeUpdate {
id: Option<i32>,
name: Option<String>,
slug: Option<String>,
#[serde(rename = "type")]
attribute_type: Option<AttributeType>,
order_by: Option<AttributeSortOrder>,
has_archives: Option<bool>,
}
#[derive(Default)]
pub struct AttributeUpdateBuilder {
id: Option<i32>,
name: Option<String>,
slug: Option<String>,
attribute_type: Option<AttributeType>,
order_by: Option<AttributeSortOrder>,
has_archives: Option<bool>,
}
impl AttributeUpdateBuilder {
pub fn id(&mut self, id: i32) -> &mut Self {
let _ = self.id.insert(id);
self
}
pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
let _ = self.name.insert(name.into());
self
}
pub fn slug(&mut self, slug: impl Into<String>) -> &mut Self {
let _ = self.slug.insert(slug.into());
self
}
pub fn attribute_type(&mut self, attribute_type: AttributeType) -> &mut Self {
let _ = self.attribute_type.insert(attribute_type);
self
}
pub fn order_by(&mut self, order_by: AttributeSortOrder) -> &mut Self {
let _ = self.order_by.insert(order_by);
self
}
pub fn enable_archives(&mut self) -> &mut Self {
let _ = self.has_archives.insert(true);
self
}
pub fn disable_archives(&mut self) -> &mut Self {
let _ = self.has_archives.insert(false);
self
}
pub fn build(&self) -> AttributeUpdate {
AttributeUpdate {
id: self.id,
name: self.name.to_owned(),
slug: self.slug.to_owned(),
attribute_type: self.attribute_type.to_owned(),
order_by: self.order_by.to_owned(),
has_archives: self.has_archives,
}
}
}