rust_woocommerce/controllers/
product_attributes.rs1use serde_with::skip_serializing_none;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{AttributeSortOrder, AttributeType};
6
7#[skip_serializing_none]
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct AttributeCreate {
10 name: String,
11 slug: Option<String>,
12 #[serde(rename = "type")]
13 attribute_type: Option<AttributeType>,
14 order_by: Option<AttributeSortOrder>,
15 has_archives: Option<bool>,
16}
17#[derive(Default)]
18pub struct AttributeCreateBuilder<N> {
19 name: N,
20 slug: Option<String>,
21 attribute_type: Option<AttributeType>,
22 order_by: Option<AttributeSortOrder>,
23 has_archives: Option<bool>,
24}
25#[derive(Default)]
26pub struct WithName(String);
27#[derive(Default)]
28pub struct NoName;
29impl<N> AttributeCreateBuilder<N> {
30 pub fn name(self, name: impl Into<String>) -> AttributeCreateBuilder<WithName> {
32 AttributeCreateBuilder {
33 name: WithName(name.into()),
34 slug: self.slug,
35 attribute_type: self.attribute_type,
36 order_by: self.order_by,
37 has_archives: self.has_archives,
38 }
39 }
40 pub fn slug(mut self, slug: impl Into<String>) -> Self {
42 let _ = self.slug.insert(slug.into());
43 self
44 }
45 pub fn attribute_type(mut self, attribute_type: AttributeType) -> Self {
47 let _ = self.attribute_type.insert(attribute_type);
48 self
49 }
50 pub fn order_by(mut self, order_by: AttributeSortOrder) -> Self {
52 let _ = self.order_by.insert(order_by);
53 self
54 }
55 pub fn enable_archives(mut self) -> Self {
57 let _ = self.has_archives.insert(true);
58 self
59 }
60}
61impl AttributeCreateBuilder<WithName> {
62 pub fn build(self) -> AttributeCreate {
63 AttributeCreate {
64 name: self.name.0,
65 slug: self.slug,
66 attribute_type: self.attribute_type,
67 order_by: self.order_by,
68 has_archives: self.has_archives,
69 }
70 }
71}
72#[skip_serializing_none]
73#[derive(Debug, Clone, Serialize, Deserialize)]
74pub struct AttributeUpdate {
75 id: Option<i32>,
76 name: Option<String>,
77 slug: Option<String>,
78 #[serde(rename = "type")]
79 attribute_type: Option<AttributeType>,
80 order_by: Option<AttributeSortOrder>,
81 has_archives: Option<bool>,
82}
83#[derive(Default)]
84pub struct AttributeUpdateBuilder {
85 id: Option<i32>,
86 name: Option<String>,
87 slug: Option<String>,
88 attribute_type: Option<AttributeType>,
89 order_by: Option<AttributeSortOrder>,
90 has_archives: Option<bool>,
91}
92impl AttributeUpdateBuilder {
93 pub fn id(&mut self, id: i32) -> &mut Self {
95 let _ = self.id.insert(id);
96 self
97 }
98 pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
100 let _ = self.name.insert(name.into());
101 self
102 }
103 pub fn slug(&mut self, slug: impl Into<String>) -> &mut Self {
105 let _ = self.slug.insert(slug.into());
106 self
107 }
108 pub fn attribute_type(&mut self, attribute_type: AttributeType) -> &mut Self {
110 let _ = self.attribute_type.insert(attribute_type);
111 self
112 }
113 pub fn order_by(&mut self, order_by: AttributeSortOrder) -> &mut Self {
115 let _ = self.order_by.insert(order_by);
116 self
117 }
118 pub fn enable_archives(&mut self) -> &mut Self {
120 let _ = self.has_archives.insert(true);
121 self
122 }
123 pub fn disable_archives(&mut self) -> &mut Self {
125 let _ = self.has_archives.insert(false);
126 self
127 }
128 pub fn build(&self) -> AttributeUpdate {
129 AttributeUpdate {
130 id: self.id,
131 name: self.name.to_owned(),
132 slug: self.slug.to_owned(),
133 attribute_type: self.attribute_type.to_owned(),
134 order_by: self.order_by.to_owned(),
135 has_archives: self.has_archives,
136 }
137 }
138}