jacquard_api/network_slices/
tools.rs1pub mod bug;
10pub mod document;
11pub mod richtext;
12
13#[allow(unused_imports)]
14use alloc::collections::BTreeMap;
15
16#[allow(unused_imports)]
17use core::marker::PhantomData;
18use jacquard_common::{BosStr, CowStr, DefaultStr, FromStaticStr};
19
20#[allow(unused_imports)]
21use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
22use jacquard_common::deps::smol_str::SmolStr;
23use jacquard_common::types::blob::BlobRef;
24use jacquard_common::types::value::Data;
25use jacquard_derive::IntoStatic;
26use jacquard_lexicon::lexicon::LexiconDoc;
27use jacquard_lexicon::schema::LexiconSchema;
28
29use crate::network_slices::tools;
30#[allow(unused_imports)]
31use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
32use serde::{Deserialize, Serialize};
33
34#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
35#[serde(
36 rename_all = "camelCase",
37 bound(deserialize = "S: Deserialize<'de> + BosStr")
38)]
39pub struct Image<S: BosStr = DefaultStr> {
40 pub alt: S,
42 pub image: BlobRef<S>,
43 #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
44 pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
45}
46
47#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
48#[serde(
49 rename_all = "camelCase",
50 bound(deserialize = "S: Deserialize<'de> + BosStr")
51)]
52pub struct Images<S: BosStr = DefaultStr> {
53 pub images: Vec<tools::Image<S>>,
54 #[serde(flatten, default, skip_serializing_if = "Option::is_none")]
55 pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
56}
57
58impl<S: BosStr> LexiconSchema for Image<S> {
59 fn nsid() -> &'static str {
60 "network.slices.tools.defs"
61 }
62 fn def_name() -> &'static str {
63 "image"
64 }
65 fn lexicon_doc() -> LexiconDoc<'static> {
66 lexicon_doc_network_slices_tools_defs()
67 }
68 fn validate(&self) -> Result<(), ConstraintError> {
69 {
70 let value = &self.image;
71 {
72 let size = value.blob().size;
73 if size > 1000000usize {
74 return Err(ConstraintError::BlobTooLarge {
75 path: ValidationPath::from_field("image"),
76 max: 1000000usize,
77 actual: size,
78 });
79 }
80 }
81 }
82 {
83 let value = &self.image;
84 {
85 let mime = value.blob().mime_type.as_str();
86 let accepted: &[&str] = &["image/*"];
87 let matched = accepted.iter().any(|pattern| {
88 if *pattern == "*/*" {
89 true
90 } else if pattern.ends_with("/*") {
91 let prefix = &pattern[..pattern.len() - 2];
92 mime.starts_with(prefix) && mime.as_bytes().get(prefix.len()) == Some(&b'/')
93 } else {
94 mime == *pattern
95 }
96 });
97 if !matched {
98 return Err(ConstraintError::BlobMimeTypeNotAccepted {
99 path: ValidationPath::from_field("image"),
100 accepted: vec!["image/*".to_string()],
101 actual: mime.to_string(),
102 });
103 }
104 }
105 }
106 Ok(())
107 }
108}
109
110impl<S: BosStr> LexiconSchema for Images<S> {
111 fn nsid() -> &'static str {
112 "network.slices.tools.defs"
113 }
114 fn def_name() -> &'static str {
115 "images"
116 }
117 fn lexicon_doc() -> LexiconDoc<'static> {
118 lexicon_doc_network_slices_tools_defs()
119 }
120 fn validate(&self) -> Result<(), ConstraintError> {
121 {
122 let value = &self.images;
123 #[allow(unused_comparisons)]
124 if value.len() > 4usize {
125 return Err(ConstraintError::MaxLength {
126 path: ValidationPath::from_field("images"),
127 max: 4usize,
128 actual: value.len(),
129 });
130 }
131 }
132 Ok(())
133 }
134}
135
136pub mod image_state {
137
138 pub use crate::builder_types::{IsSet, IsUnset, Set, Unset};
139 #[allow(unused)]
140 use ::core::marker::PhantomData;
141 mod sealed {
142 pub trait Sealed {}
143 }
144 pub trait State: sealed::Sealed {
146 type Alt;
147 type Image;
148 }
149 pub struct Empty(());
151 impl sealed::Sealed for Empty {}
152 impl State for Empty {
153 type Alt = Unset;
154 type Image = Unset;
155 }
156 pub struct SetAlt<St: State = Empty>(PhantomData<fn() -> St>);
158 impl<St: State> sealed::Sealed for SetAlt<St> {}
159 impl<St: State> State for SetAlt<St> {
160 type Alt = Set<members::alt>;
161 type Image = St::Image;
162 }
163 pub struct SetImage<St: State = Empty>(PhantomData<fn() -> St>);
165 impl<St: State> sealed::Sealed for SetImage<St> {}
166 impl<St: State> State for SetImage<St> {
167 type Alt = St::Alt;
168 type Image = Set<members::image>;
169 }
170 #[allow(non_camel_case_types)]
172 pub mod members {
173 pub struct alt(());
175 pub struct image(());
177 }
178}
179
180pub struct ImageBuilder<St: image_state::State, S: BosStr = DefaultStr> {
182 _state: PhantomData<fn() -> St>,
183 _fields: (Option<S>, Option<BlobRef<S>>),
184 _type: PhantomData<fn() -> S>,
185}
186
187impl Image<DefaultStr> {
188 pub fn new() -> ImageBuilder<image_state::Empty, DefaultStr> {
190 ImageBuilder::new()
191 }
192}
193
194impl<S: BosStr> Image<S> {
195 pub fn builder() -> ImageBuilder<image_state::Empty, S> {
197 ImageBuilder::builder()
198 }
199}
200
201impl ImageBuilder<image_state::Empty, DefaultStr> {
202 pub fn new() -> Self {
204 ImageBuilder {
205 _state: PhantomData,
206 _fields: (None, None),
207 _type: PhantomData,
208 }
209 }
210}
211
212impl<S: BosStr> ImageBuilder<image_state::Empty, S> {
213 pub fn builder() -> Self {
215 ImageBuilder {
216 _state: PhantomData,
217 _fields: (None, None),
218 _type: PhantomData,
219 }
220 }
221}
222
223impl<St, S: BosStr> ImageBuilder<St, S>
224where
225 St: image_state::State,
226 St::Alt: image_state::IsUnset,
227{
228 pub fn alt(mut self, value: impl Into<S>) -> ImageBuilder<image_state::SetAlt<St>, S> {
230 self._fields.0 = Option::Some(value.into());
231 ImageBuilder {
232 _state: PhantomData,
233 _fields: self._fields,
234 _type: PhantomData,
235 }
236 }
237}
238
239impl<St, S: BosStr> ImageBuilder<St, S>
240where
241 St: image_state::State,
242 St::Image: image_state::IsUnset,
243{
244 pub fn image(
246 mut self,
247 value: impl Into<BlobRef<S>>,
248 ) -> ImageBuilder<image_state::SetImage<St>, S> {
249 self._fields.1 = Option::Some(value.into());
250 ImageBuilder {
251 _state: PhantomData,
252 _fields: self._fields,
253 _type: PhantomData,
254 }
255 }
256}
257
258impl<St, S: BosStr> ImageBuilder<St, S>
259where
260 St: image_state::State,
261 St::Alt: image_state::IsSet,
262 St::Image: image_state::IsSet,
263{
264 pub fn build(self) -> Image<S> {
266 Image {
267 alt: self._fields.0.unwrap(),
268 image: self._fields.1.unwrap(),
269 extra_data: Default::default(),
270 }
271 }
272 pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Image<S> {
274 Image {
275 alt: self._fields.0.unwrap(),
276 image: self._fields.1.unwrap(),
277 extra_data: Some(extra_data),
278 }
279 }
280}
281
282fn lexicon_doc_network_slices_tools_defs() -> LexiconDoc<'static> {
283 use alloc::collections::BTreeMap;
284 #[allow(unused_imports)]
285 use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
286 use jacquard_lexicon::lexicon::*;
287 LexiconDoc {
288 lexicon: Lexicon::Lexicon1,
289 id: CowStr::new_static("network.slices.tools.defs"),
290 defs: {
291 let mut map = BTreeMap::new();
292 map.insert(
293 SmolStr::new_static("image"),
294 LexUserType::Object(LexObject {
295 required: Some(vec![
296 SmolStr::new_static("image"),
297 SmolStr::new_static("alt"),
298 ]),
299 properties: {
300 #[allow(unused_mut)]
301 let mut map = BTreeMap::new();
302 map.insert(
303 SmolStr::new_static("alt"),
304 LexObjectProperty::String(LexString {
305 description: Some(CowStr::new_static(
306 "Alt text description of the image, for accessibility",
307 )),
308 ..Default::default()
309 }),
310 );
311 map.insert(
312 SmolStr::new_static("image"),
313 LexObjectProperty::Blob(LexBlob {
314 ..Default::default()
315 }),
316 );
317 map
318 },
319 ..Default::default()
320 }),
321 );
322 map.insert(
323 SmolStr::new_static("images"),
324 LexUserType::Object(LexObject {
325 required: Some(vec![SmolStr::new_static("images")]),
326 properties: {
327 #[allow(unused_mut)]
328 let mut map = BTreeMap::new();
329 map.insert(
330 SmolStr::new_static("images"),
331 LexObjectProperty::Array(LexArray {
332 items: LexArrayItem::Ref(LexRef {
333 r#ref: CowStr::new_static("#image"),
334 ..Default::default()
335 }),
336 max_length: Some(4usize),
337 ..Default::default()
338 }),
339 );
340 map
341 },
342 ..Default::default()
343 }),
344 );
345 map
346 },
347 ..Default::default()
348 }
349}
350
351pub mod images_state {
352
353 pub use crate::builder_types::{IsSet, IsUnset, Set, Unset};
354 #[allow(unused)]
355 use ::core::marker::PhantomData;
356 mod sealed {
357 pub trait Sealed {}
358 }
359 pub trait State: sealed::Sealed {
361 type Images;
362 }
363 pub struct Empty(());
365 impl sealed::Sealed for Empty {}
366 impl State for Empty {
367 type Images = Unset;
368 }
369 pub struct SetImages<St: State = Empty>(PhantomData<fn() -> St>);
371 impl<St: State> sealed::Sealed for SetImages<St> {}
372 impl<St: State> State for SetImages<St> {
373 type Images = Set<members::images>;
374 }
375 #[allow(non_camel_case_types)]
377 pub mod members {
378 pub struct images(());
380 }
381}
382
383pub struct ImagesBuilder<St: images_state::State, S: BosStr = DefaultStr> {
385 _state: PhantomData<fn() -> St>,
386 _fields: (Option<Vec<tools::Image<S>>>,),
387 _type: PhantomData<fn() -> S>,
388}
389
390impl Images<DefaultStr> {
391 pub fn new() -> ImagesBuilder<images_state::Empty, DefaultStr> {
393 ImagesBuilder::new()
394 }
395}
396
397impl<S: BosStr> Images<S> {
398 pub fn builder() -> ImagesBuilder<images_state::Empty, S> {
400 ImagesBuilder::builder()
401 }
402}
403
404impl ImagesBuilder<images_state::Empty, DefaultStr> {
405 pub fn new() -> Self {
407 ImagesBuilder {
408 _state: PhantomData,
409 _fields: (None,),
410 _type: PhantomData,
411 }
412 }
413}
414
415impl<S: BosStr> ImagesBuilder<images_state::Empty, S> {
416 pub fn builder() -> Self {
418 ImagesBuilder {
419 _state: PhantomData,
420 _fields: (None,),
421 _type: PhantomData,
422 }
423 }
424}
425
426impl<St, S: BosStr> ImagesBuilder<St, S>
427where
428 St: images_state::State,
429 St::Images: images_state::IsUnset,
430{
431 pub fn images(
433 mut self,
434 value: impl Into<Vec<tools::Image<S>>>,
435 ) -> ImagesBuilder<images_state::SetImages<St>, S> {
436 self._fields.0 = Option::Some(value.into());
437 ImagesBuilder {
438 _state: PhantomData,
439 _fields: self._fields,
440 _type: PhantomData,
441 }
442 }
443}
444
445impl<St, S: BosStr> ImagesBuilder<St, S>
446where
447 St: images_state::State,
448 St::Images: images_state::IsSet,
449{
450 pub fn build(self) -> Images<S> {
452 Images {
453 images: self._fields.0.unwrap(),
454 extra_data: Default::default(),
455 }
456 }
457 pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Images<S> {
459 Images {
460 images: self._fields.0.unwrap(),
461 extra_data: Some(extra_data),
462 }
463 }
464}