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