Skip to main content

rs_matter/dm/clusters/
unit_testing.rs

1/*
2 *
3 *    Copyright (c) 2025-2026 Project CHIP Authors
4 *
5 *    Licensed under the Apache License, Version 2.0 (the "License");
6 *    you may not use this file except in compliance with the License.
7 *    You may obtain a copy of the License at
8 *
9 *        http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *    Unless required by applicable law or agreed to in writing, software
12 *    distributed under the License is distributed on an "AS IS" BASIS,
13 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *    See the License for the specific language governing permissions and
15 *    limitations under the License.
16 */
17
18//! This module contains the implementation of the Unit Testing cluster and its handler.
19
20use core::num::NonZeroU8;
21
22use crate::dm::{
23    ArrayAttributeRead, ArrayAttributeWrite, Cluster, Dataver, InvokeContext, ReadContext,
24    WriteContext,
25};
26use crate::error::{Error, ErrorCode};
27use crate::tlv::{
28    Nullable, NullableBuilder, OctetStr, Octets, OctetsArrayBuilder, OctetsBuilder, TLVArray,
29    TLVBuilder, TLVBuilderParent, TLVTag, TLVWrite, ToTLVArrayBuilder, ToTLVBuilder, Utf8Str,
30    Utf8StrBuilder,
31};
32use crate::utils::cell::RefCell;
33use crate::utils::init::{init, Init, IntoFallibleInit};
34use crate::utils::storage::Vec;
35
36pub use crate::dm::clusters::decl::globals::*;
37pub use crate::dm::clusters::decl::unit_testing::*;
38use crate::{except, with};
39
40#[derive(Debug)]
41#[cfg_attr(feature = "defmt", derive(defmt::Format))]
42struct TestListStructOctetOwned {
43    member_1: u64,
44    member_2: Vec<u8, 32>,
45}
46
47#[derive(Debug)]
48#[cfg_attr(feature = "defmt", derive(defmt::Format))]
49struct SimpleStructOwned {
50    a: u8,
51    b: bool,
52    c: SimpleEnum,
53    d: Vec<u8, 10>,
54    e: heapless::String<10>,
55    f: SimpleBitmap,
56    g: f32,
57    h: f64,
58    i: Option<TestGlobalEnum>,
59}
60
61impl SimpleStructOwned {
62    pub fn init() -> impl Init<Self> {
63        init!(Self {
64            a: 0,
65            b: false,
66            c: SimpleEnum::ValueA,
67            d <- Vec::init(),
68            e: heapless::String::new(),
69            f: SimpleBitmap::empty(),
70            g: 0.0,
71            h: 0.0,
72            i: Some(TestGlobalEnum::SomeValue),
73        })
74    }
75}
76
77#[derive(Debug)]
78#[cfg_attr(feature = "defmt", derive(defmt::Format))]
79struct TestGlobalStructOwned {
80    name: heapless::String<10>,
81    my_bitmap: Nullable<TestGlobalBitmap>,
82    my_enum: Option<Nullable<TestGlobalEnum>>,
83}
84
85impl TestGlobalStructOwned {
86    pub fn init() -> impl Init<Self> {
87        init!(Self {
88            name: heapless::String::new(),
89            my_bitmap <- Nullable::init_none(),
90            my_enum: None,
91        })
92    }
93}
94
95#[derive(Debug)]
96#[cfg_attr(feature = "defmt", derive(defmt::Format))]
97struct NullablesAndOptionalsStructOwned {
98    nullable_int: Nullable<u16>,
99    optional_int: Option<u16>,
100    nullable_optional_int: Option<Nullable<u16>>,
101    nullable_string: Nullable<heapless::String<10>>,
102    optional_string: Option<heapless::String<10>>,
103    nullable_optional_string: Option<Nullable<heapless::String<10>>>,
104    nullable_struct: Nullable<SimpleStructOwned>,
105    optional_struct: Option<SimpleStructOwned>,
106    nullable_optional_struct: Option<Nullable<SimpleStructOwned>>,
107    nullable_list: Nullable<Vec<SimpleEnum, 16>>,
108    optional_list: Option<Vec<SimpleEnum, 16>>,
109    nullable_optional_list: Option<Nullable<Vec<SimpleEnum, 16>>>,
110}
111
112impl NullablesAndOptionalsStructOwned {
113    pub fn init() -> impl Init<Self> {
114        init!(Self {
115            nullable_int <- Nullable::init_none(),
116            optional_int: None,
117            nullable_optional_int: None,
118            nullable_string <- Nullable::init_none(),
119            optional_string: None,
120            nullable_optional_string: None,
121            nullable_struct <- Nullable::init_none(),
122            optional_struct: None,
123            nullable_optional_struct: None,
124            nullable_list <- Nullable::init_none(),
125            optional_list: None,
126            nullable_optional_list: None,
127        })
128    }
129
130    pub fn update(&mut self, s: &NullablesAndOptionalsStruct) -> Result<(), Error> {
131        self.nullable_int = s.nullable_int()?.clone();
132        self.optional_int = s.optional_int()?;
133        self.nullable_optional_int = s.nullable_optional_int()?.clone();
134        self.nullable_string = Nullable::new(
135            s.nullable_string()?
136                .into_option()
137                .map(|s| s.try_into().map_err(|_| ErrorCode::InvalidAction))
138                .transpose()?,
139        );
140        self.optional_string = s
141            .optional_string()?
142            .map(|s| s.try_into().map_err(|_| ErrorCode::InvalidAction))
143            .transpose()?;
144        self.nullable_optional_string = if let Some(ss) = s.nullable_optional_string()? {
145            Some(Nullable::new(
146                ss.into_option()
147                    .map(|s| s.try_into().map_err(|_| ErrorCode::InvalidAction))
148                    .transpose()?,
149            ))
150        } else {
151            None
152        };
153        self.nullable_struct = if let Some(s) = s.nullable_struct()?.as_opt_ref() {
154            Nullable::some(SimpleStructOwned {
155                a: s.a()?,
156                b: s.b()?,
157                c: s.c()?,
158                d: s.d()?
159                    .0
160                    .try_into()
161                    .map_err(|_| ErrorCode::ConstraintError)?,
162                e: s.e()?.try_into().map_err(|_| ErrorCode::ConstraintError)?,
163                f: s.f()?,
164                g: s.g()?,
165                h: s.h()?,
166                i: s.i()?,
167            })
168        } else {
169            Nullable::none()
170        };
171        self.optional_struct = if let Some(s) = s.optional_struct()?.as_ref() {
172            Some(SimpleStructOwned {
173                a: s.a()?,
174                b: s.b()?,
175                c: s.c()?,
176                d: s.d()?
177                    .0
178                    .try_into()
179                    .map_err(|_| ErrorCode::ConstraintError)?,
180                e: s.e()?.try_into().map_err(|_| ErrorCode::ConstraintError)?,
181                f: s.f()?,
182                g: s.g()?,
183                h: s.h()?,
184                i: s.i()?,
185            })
186        } else {
187            None
188        };
189        self.nullable_optional_struct = if let Some(s) = s.nullable_optional_struct()?.as_ref() {
190            Some(if let Some(s) = s.as_opt_ref() {
191                Nullable::some(SimpleStructOwned {
192                    a: s.a()?,
193                    b: s.b()?,
194                    c: s.c()?,
195                    d: s.d()?
196                        .0
197                        .try_into()
198                        .map_err(|_| ErrorCode::ConstraintError)?,
199                    e: s.e()?.try_into().map_err(|_| ErrorCode::ConstraintError)?,
200                    f: s.f()?,
201                    g: s.g()?,
202                    h: s.h()?,
203                    i: s.i()?,
204                })
205            } else {
206                Nullable::none()
207            })
208        } else {
209            None
210        };
211        self.optional_list = if let Some(l) = s.optional_list()?.as_ref() {
212            Some(l.iter().collect::<Result<Vec<_, 16>, _>>()?)
213        } else {
214            None
215        };
216        self.nullable_list = if let Some(l) = s.nullable_list()?.as_opt_ref() {
217            Nullable::some(l.iter().collect::<Result<Vec<_, 16>, _>>()?)
218        } else {
219            Nullable::none()
220        };
221        self.nullable_optional_list = if let Some(l) = s.nullable_optional_list()?.as_ref() {
222            Some(if let Some(l) = l.as_opt_ref() {
223                Nullable::some(l.iter().collect::<Result<Vec<_, 16>, _>>()?)
224            } else {
225                Nullable::none()
226            })
227        } else {
228            None
229        };
230
231        Ok(())
232    }
233}
234
235#[derive(Debug)]
236#[cfg_attr(feature = "defmt", derive(defmt::Format))]
237struct TestFabricScopedOwned {
238    fabric_sensitive_int8u: u8,
239    optional_fabric_sensitive_int8u: Option<u8>,
240    nullable_fabric_sensitive_int8u: Nullable<u8>,
241    optional_nullable_fabric_sensitive_int8u: Option<Nullable<u8>>,
242    fabric_sensitive_char_string: heapless::String<32>,
243    fabric_sensitive_struct: SimpleStructOwned,
244    fabric_sensitive_int8u_list: heapless::Vec<u8, 32>,
245}
246
247impl TestFabricScopedOwned {
248    pub fn init() -> impl Init<Self> {
249        init!(Self {
250            fabric_sensitive_int8u: 0,
251            optional_fabric_sensitive_int8u: None,
252            nullable_fabric_sensitive_int8u <- Nullable::init_none(),
253            optional_nullable_fabric_sensitive_int8u: None,
254            fabric_sensitive_char_string: heapless::String::new(),
255            fabric_sensitive_struct <- SimpleStructOwned::init(),
256            fabric_sensitive_int8u_list: heapless::Vec::new(),
257        })
258    }
259
260    pub fn update(&mut self, s: &TestFabricScoped) -> Result<(), crate::error::Error> {
261        self.fabric_sensitive_int8u = s
262            .fabric_sensitive_int_8_u()?
263            .ok_or(ErrorCode::ConstraintError)?;
264        self.optional_fabric_sensitive_int8u = s.optional_fabric_sensitive_int_8_u()?;
265        self.nullable_fabric_sensitive_int8u = s
266            .nullable_fabric_sensitive_int_8_u()?
267            .ok_or(ErrorCode::ConstraintError)?
268            .clone();
269        self.optional_nullable_fabric_sensitive_int8u =
270            s.nullable_optional_fabric_sensitive_int_8_u()?.clone();
271        self.fabric_sensitive_char_string = s
272            .fabric_sensitive_char_string()?
273            .ok_or(ErrorCode::ConstraintError)?
274            .try_into()
275            .map_err(|_| ErrorCode::ConstraintError)?;
276        let ss = s
277            .fabric_sensitive_struct()?
278            .ok_or(ErrorCode::ConstraintError)?;
279        self.fabric_sensitive_struct.a = ss.a()?;
280        self.fabric_sensitive_struct.b = ss.b()?;
281        self.fabric_sensitive_struct.c = ss.c()?;
282        self.fabric_sensitive_struct.d = ss
283            .d()?
284            .0
285            .try_into()
286            .map_err(|_| ErrorCode::ConstraintError)?;
287        self.fabric_sensitive_struct.e =
288            ss.e()?.try_into().map_err(|_| ErrorCode::ConstraintError)?;
289        self.fabric_sensitive_struct.f = ss.f()?;
290        self.fabric_sensitive_struct.g = ss.g()?;
291        self.fabric_sensitive_struct.h = ss.h()?;
292        self.fabric_sensitive_int8u_list.clear();
293        for i in s
294            .fabric_sensitive_int_8_u_list()?
295            .ok_or(ErrorCode::ConstraintError)?
296            .iter()
297        {
298            self.fabric_sensitive_int8u_list
299                .push(i?)
300                .map_err(|_| ErrorCode::ConstraintError)?;
301        }
302
303        Ok(())
304    }
305}
306
307#[derive(Debug)]
308#[cfg_attr(feature = "defmt", derive(defmt::Format))]
309pub struct UnitTestingHandlerData {
310    boolean: bool,
311    bitmap_8: Bitmap8MaskMap,
312    bitmap_16: Bitmap16MaskMap,
313    bitmap_32: Bitmap32MaskMap,
314    bitmap_64: Bitmap64MaskMap,
315    int_8_u: u8,
316    int_16_u: u16,
317    int_24_u: u32,
318    int_32_u: u32,
319    int_40_u: u64,
320    int_48_u: u64,
321    int_56_u: u64,
322    int_64_u: u64,
323    int_8_s: i8,
324    int_16_s: i16,
325    int_24_s: i32,
326    int_32_s: i32,
327    int_40_s: i64,
328    int_48_s: i64,
329    int_56_s: i64,
330    int_64_s: i64,
331    enum_8: u8,
332    enum_16: u16,
333    float_single: f32,
334    float_double: f64,
335    octet_string: Vec<u8, 10>,
336    list_int_8_u: Vec<u8, 16>,
337    list_octet_string: Vec<Vec<u8, 10>, 16>,
338    list_struct_octet_string: Vec<TestListStructOctetOwned, 16>,
339    long_octet_string: Vec<u8, 1000>,
340    char_string: heapless::String<10>,
341    long_char_string: heapless::String<1000>,
342    epoch_us: u64,
343    epoch_s: u32,
344    vendor_id: u16,
345    list_nullables_and_optionals_struct: Vec<NullablesAndOptionalsStructOwned, 16>,
346    enum_attr: SimpleEnum,
347    struct_attr: SimpleStructOwned,
348    range_restricted_int_8_u: u8,
349    range_restricted_int_8_s: i8,
350    range_restricted_int_16_u: u16,
351    range_restricted_int_16_s: i16,
352    list_long_octet_string: Vec<Vec<u8, 1000>, 16>,
353    list_fabric_scoped: Vec<(NonZeroU8, Vec<TestFabricScopedOwned, 16>), 16>,
354    timed_write_boolean: bool,
355    nullable_boolean: Nullable<bool>,
356    nullable_bitmap_8: Nullable<Bitmap8MaskMap>,
357    nullable_bitmap_16: Nullable<Bitmap16MaskMap>,
358    nullable_bitmap_32: Nullable<Bitmap32MaskMap>,
359    nullable_bitmap_64: Nullable<Bitmap64MaskMap>,
360    nullable_int_8_u: Nullable<u8>,
361    nullable_int_16_u: Nullable<u16>,
362    nullable_int_24_u: Nullable<u32>,
363    nullable_int_32_u: Nullable<u32>,
364    nullable_int_40_u: Nullable<u64>,
365    nullable_int_48_u: Nullable<u64>,
366    nullable_int_56_u: Nullable<u64>,
367    nullable_int_64_u: Nullable<u64>,
368    nullable_int_8_s: Nullable<i8>,
369    nullable_int_16_s: Nullable<i16>,
370    nullable_int_24_s: Nullable<i32>,
371    nullable_int_32_s: Nullable<i32>,
372    nullable_int_40_s: Nullable<i64>,
373    nullable_int_48_s: Nullable<i64>,
374    nullable_int_56_s: Nullable<i64>,
375    nullable_int_64_s: Nullable<i64>,
376    nullable_enum_8: Nullable<u8>,
377    nullable_enum_16: Nullable<u16>,
378    nullable_float_single: Nullable<f32>,
379    nullable_float_double: Nullable<f64>,
380    nullable_octet_string: Nullable<Vec<u8, 10>>,
381    nullable_char_string: Nullable<heapless::String<10>>,
382    nullable_enum_attr: Nullable<SimpleEnum>,
383    nullable_struct: Nullable<SimpleStructOwned>,
384    nullable_range_restricted_int_8_u: Nullable<u8>,
385    nullable_range_restricted_int_8_s: Nullable<i8>,
386    nullable_range_restricted_int_16_u: Nullable<u16>,
387    nullable_range_restricted_int_16_s: Nullable<i16>,
388    mei_int_8_u: u8,
389    global_enum: TestGlobalEnum,
390    global_struct: TestGlobalStructOwned,
391    nullable_global_enum: Nullable<TestGlobalEnum>,
392    nullable_global_struct: Nullable<TestGlobalStructOwned>,
393}
394
395impl UnitTestingHandlerData {
396    pub fn init() -> impl Init<Self> {
397        init!(Self {
398            boolean: false,
399            bitmap_8: Bitmap8MaskMap::empty(),
400            bitmap_16: Bitmap16MaskMap::empty(),
401            bitmap_32: Bitmap32MaskMap::empty(),
402            bitmap_64: Bitmap64MaskMap::empty(),
403            int_8_u: 0,
404            int_16_u: 0,
405            int_24_u: 0,
406            int_32_u: 0,
407            int_40_u: 0,
408            int_48_u: 0,
409            int_56_u: 0,
410            int_64_u: 0,
411            int_8_s: 0,
412            int_16_s: 0,
413            int_24_s: 0,
414            int_32_s: 0,
415            int_40_s: 0,
416            int_48_s: 0,
417            int_56_s: 0,
418            int_64_s: 0,
419            enum_8: 0,
420            enum_16: 0,
421            float_single: 0.0,
422            float_double: 0.0,
423            octet_string <- Vec::init(),
424            list_int_8_u <- Vec::init(),
425            list_octet_string <- Vec::init(),
426            list_struct_octet_string <- Vec::init(),
427            long_octet_string <- Vec::init(),
428            char_string: heapless::String::new(),
429            long_char_string: heapless::String::new(),
430            epoch_us: 0,
431            epoch_s: 0,
432            vendor_id: 0,
433            list_nullables_and_optionals_struct <- Vec::init().chain(|vec| {
434                unwrap!(vec.push_init_unchecked(NullablesAndOptionalsStructOwned::init()));
435                Ok(())
436            }),
437            enum_attr: SimpleEnum::ValueA,
438            struct_attr <- SimpleStructOwned::init(),
439            range_restricted_int_8_u: 70,
440            range_restricted_int_8_s: -20,
441            range_restricted_int_16_u: 200,
442            range_restricted_int_16_s: -100,
443            list_long_octet_string <- Vec::init().chain(|vec| {
444                for _ in 0..4 {
445                    let item = Vec::init()
446                        .chain(|item| {
447                            unwrap!(item.extend_from_slice(b"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"));
448
449                            Ok(())
450                        });
451
452                    unwrap!(vec.push_init_unchecked(item));
453                }
454
455                Ok(())
456            }),
457            list_fabric_scoped <- Vec::init(),
458            timed_write_boolean: false,
459            nullable_boolean <- Nullable::init_none(),
460            nullable_bitmap_8 <- Nullable::init_none(),
461            nullable_bitmap_16 <- Nullable::init_none(),
462            nullable_bitmap_32 <- Nullable::init_none(),
463            nullable_bitmap_64 <- Nullable::init_none(),
464            nullable_int_8_u : Nullable::some(0),
465            nullable_int_16_u : Nullable::some(0),
466            nullable_int_24_u : Nullable::some(0),
467            nullable_int_32_u : Nullable::some(0),
468            nullable_int_40_u : Nullable::some(0),
469            nullable_int_48_u : Nullable::some(0),
470            nullable_int_56_u : Nullable::some(0),
471            nullable_int_64_u : Nullable::some(0),
472            nullable_int_8_s : Nullable::some(0),
473            nullable_int_16_s : Nullable::some(0),
474            nullable_int_24_s : Nullable::some(0),
475            nullable_int_32_s : Nullable::some(0),
476            nullable_int_40_s : Nullable::some(0),
477            nullable_int_48_s : Nullable::some(0),
478            nullable_int_56_s : Nullable::some(0),
479            nullable_int_64_s : Nullable::some(0),
480            nullable_enum_8 <- Nullable::init_none(),
481            nullable_enum_16 <- Nullable::init_none(),
482            nullable_float_single <- Nullable::init_none(),
483            nullable_float_double <- Nullable::init_none(),
484            nullable_octet_string <- Nullable::init_some(Vec::init()),
485            nullable_char_string: Nullable::some(heapless::String::new()),
486            nullable_enum_attr <- Nullable::init_none(),
487            nullable_struct <- Nullable::init_none(),
488            nullable_range_restricted_int_8_u: Nullable::some(70),
489            nullable_range_restricted_int_8_s: Nullable::some(-20),
490            nullable_range_restricted_int_16_u: Nullable::some(200),
491            nullable_range_restricted_int_16_s: Nullable::some(-100),
492            mei_int_8_u: 0,
493            global_enum: TestGlobalEnum::SomeValue,
494            global_struct <- TestGlobalStructOwned::init(),
495            nullable_global_enum <- Nullable::init_none(),
496            nullable_global_struct <- Nullable::init_none(),
497        })
498    }
499}
500
501pub struct UnitTestingHandler<'a> {
502    dataver: Dataver,
503    data: &'a RefCell<UnitTestingHandlerData>,
504}
505
506impl<'a> UnitTestingHandler<'a> {
507    pub const fn new(dataver: Dataver, data: &'a RefCell<UnitTestingHandlerData>) -> Self {
508        Self { dataver, data }
509    }
510
511    pub const fn adapt(self) -> HandlerAdaptor<Self> {
512        HandlerAdaptor(self)
513    }
514}
515
516impl ClusterHandler for UnitTestingHandler<'_> {
517    const CLUSTER: Cluster<'static> = FULL_CLUSTER.with_attrs(with!(required)).with_cmds(except!(
518        CommandId::TestUnknownCommand
519            | CommandId::TestSimpleArgumentRequest
520            | CommandId::TestStructArrayArgumentRequest
521            | CommandId::TestComplexNullableOptionalRequest
522    ));
523
524    fn dataver(&self) -> u32 {
525        self.dataver.get()
526    }
527
528    fn dataver_changed(&self) {
529        self.dataver.changed();
530    }
531
532    fn boolean(&self, _ctx: impl ReadContext) -> Result<bool, Error> {
533        Ok(self.data.borrow().boolean)
534    }
535
536    fn bitmap_8(&self, _ctx: impl ReadContext) -> Result<Bitmap8MaskMap, Error> {
537        Ok(self.data.borrow().bitmap_8)
538    }
539
540    fn bitmap_16(&self, _ctx: impl ReadContext) -> Result<Bitmap16MaskMap, Error> {
541        Ok(self.data.borrow().bitmap_16)
542    }
543
544    fn bitmap_32(&self, _ctx: impl ReadContext) -> Result<Bitmap32MaskMap, Error> {
545        Ok(self.data.borrow().bitmap_32)
546    }
547
548    fn bitmap_64(&self, _ctx: impl ReadContext) -> Result<Bitmap64MaskMap, Error> {
549        Ok(self.data.borrow().bitmap_64)
550    }
551
552    fn int_8_u(&self, _ctx: impl ReadContext) -> Result<u8, Error> {
553        Ok(self.data.borrow().int_8_u)
554    }
555
556    fn int_16_u(&self, _ctx: impl ReadContext) -> Result<u16, Error> {
557        Ok(self.data.borrow().int_16_u)
558    }
559
560    fn int_24_u(&self, _ctx: impl ReadContext) -> Result<u32, Error> {
561        Ok(self.data.borrow().int_24_u)
562    }
563
564    fn int_32_u(&self, _ctx: impl ReadContext) -> Result<u32, Error> {
565        Ok(self.data.borrow().int_32_u)
566    }
567
568    fn int_40_u(&self, _ctx: impl ReadContext) -> Result<u64, Error> {
569        Ok(self.data.borrow().int_40_u)
570    }
571
572    fn int_48_u(&self, _ctx: impl ReadContext) -> Result<u64, Error> {
573        Ok(self.data.borrow().int_48_u)
574    }
575
576    fn int_56_u(&self, _ctx: impl ReadContext) -> Result<u64, Error> {
577        Ok(self.data.borrow().int_56_u)
578    }
579
580    fn int_64_u(&self, _ctx: impl ReadContext) -> Result<u64, Error> {
581        Ok(self.data.borrow().int_64_u)
582    }
583
584    fn int_8_s(&self, _ctx: impl ReadContext) -> Result<i8, Error> {
585        Ok(self.data.borrow().int_8_s)
586    }
587
588    fn int_16_s(&self, _ctx: impl ReadContext) -> Result<i16, Error> {
589        Ok(self.data.borrow().int_16_s)
590    }
591
592    fn int_24_s(&self, _ctx: impl ReadContext) -> Result<i32, Error> {
593        Ok(self.data.borrow().int_24_s)
594    }
595
596    fn int_32_s(&self, _ctx: impl ReadContext) -> Result<i32, Error> {
597        Ok(self.data.borrow().int_32_s)
598    }
599
600    fn int_40_s(&self, _ctx: impl ReadContext) -> Result<i64, Error> {
601        Ok(self.data.borrow().int_40_s)
602    }
603
604    fn int_48_s(&self, _ctx: impl ReadContext) -> Result<i64, Error> {
605        Ok(self.data.borrow().int_48_s)
606    }
607
608    fn int_56_s(&self, _ctx: impl ReadContext) -> Result<i64, Error> {
609        Ok(self.data.borrow().int_56_s)
610    }
611
612    fn int_64_s(&self, _ctx: impl ReadContext) -> Result<i64, Error> {
613        Ok(self.data.borrow().int_64_s)
614    }
615
616    fn enum_8(&self, _ctx: impl ReadContext) -> Result<u8, Error> {
617        Ok(self.data.borrow().enum_8)
618    }
619
620    fn enum_16(&self, _ctx: impl ReadContext) -> Result<u16, Error> {
621        Ok(self.data.borrow().enum_16)
622    }
623
624    fn float_single(&self, _ctx: impl ReadContext) -> Result<f32, Error> {
625        Ok(self.data.borrow().float_single)
626    }
627
628    fn float_double(&self, _ctx: impl ReadContext) -> Result<f64, Error> {
629        Ok(self.data.borrow().float_double)
630    }
631
632    fn octet_string<P: TLVBuilderParent>(
633        &self,
634        _ctx: impl ReadContext,
635        builder: OctetsBuilder<P>,
636    ) -> Result<P, Error> {
637        builder.set(Octets(self.data.borrow().octet_string.as_slice()))
638    }
639
640    fn list_int_8_u<P: TLVBuilderParent>(
641        &self,
642        _ctx: impl ReadContext,
643        builder: ArrayAttributeRead<ToTLVArrayBuilder<P, u8>, ToTLVBuilder<P, u8>>,
644    ) -> Result<P, Error> {
645        match builder {
646            ArrayAttributeRead::ReadOne(index, builder) => {
647                let data = self.data.borrow();
648                if index < data.list_int_8_u.len() as u16 {
649                    builder.set(&data.list_int_8_u[index as usize])
650                } else {
651                    Err(ErrorCode::ConstraintError.into())
652                }
653            }
654            ArrayAttributeRead::ReadAll(mut builder) => {
655                let data = self.data.borrow();
656
657                for i in &data.list_int_8_u {
658                    builder = builder.push(i)?;
659                }
660
661                builder.end()
662            }
663            ArrayAttributeRead::ReadNone(builder) => builder.end(),
664        }
665    }
666
667    fn list_octet_string<P: TLVBuilderParent>(
668        &self,
669        _ctx: impl ReadContext,
670        builder: ArrayAttributeRead<OctetsArrayBuilder<P>, OctetsBuilder<P>>,
671    ) -> Result<P, Error> {
672        match builder {
673            ArrayAttributeRead::ReadOne(index, builder) => {
674                let data = self.data.borrow();
675                if index < data.list_octet_string.len() as u16 {
676                    builder.set(Octets(data.list_octet_string[index as usize].as_slice()))
677                } else {
678                    Err(ErrorCode::ConstraintError.into())
679                }
680            }
681            ArrayAttributeRead::ReadAll(mut builder) => {
682                let data = self.data.borrow();
683
684                for i in &data.list_octet_string {
685                    builder = builder.push(Octets(i.as_slice()))?;
686                }
687
688                builder.end()
689            }
690            ArrayAttributeRead::ReadNone(builder) => builder.end(),
691        }
692    }
693
694    fn list_struct_octet_string<P: TLVBuilderParent>(
695        &self,
696        _ctx: impl ReadContext,
697        builder: ArrayAttributeRead<
698            TestListStructOctetArrayBuilder<P>,
699            TestListStructOctetBuilder<P>,
700        >,
701    ) -> Result<P, Error> {
702        match builder {
703            ArrayAttributeRead::ReadOne(index, builder) => {
704                let data = self.data.borrow();
705                if index < data.list_struct_octet_string.len() as u16 {
706                    let s = &data.list_struct_octet_string[index as usize];
707
708                    builder
709                        .member_1(s.member_1)?
710                        .member_2(Octets(&s.member_2))?
711                        .end()
712                } else {
713                    Err(ErrorCode::ConstraintError.into())
714                }
715            }
716            ArrayAttributeRead::ReadAll(mut builder) => {
717                let data = self.data.borrow();
718
719                for s in &data.list_struct_octet_string {
720                    builder = builder
721                        .push()?
722                        .member_1(s.member_1)?
723                        .member_2(Octets(&s.member_2))?
724                        .end()?;
725                }
726
727                builder.end()
728            }
729            ArrayAttributeRead::ReadNone(builder) => builder.end(),
730        }
731    }
732
733    fn long_octet_string<P: TLVBuilderParent>(
734        &self,
735        _ctx: impl ReadContext,
736        builder: OctetsBuilder<P>,
737    ) -> Result<P, Error> {
738        builder.set(Octets(self.data.borrow().long_octet_string.as_slice()))
739    }
740
741    fn char_string<P: TLVBuilderParent>(
742        &self,
743        _ctx: impl ReadContext,
744        builder: Utf8StrBuilder<P>,
745    ) -> Result<P, Error> {
746        builder.set(self.data.borrow().char_string.as_str())
747    }
748
749    fn long_char_string<P: TLVBuilderParent>(
750        &self,
751        _ctx: impl ReadContext,
752        builder: Utf8StrBuilder<P>,
753    ) -> Result<P, Error> {
754        builder.set(self.data.borrow().long_char_string.as_str())
755    }
756
757    fn epoch_us(&self, _ctx: impl ReadContext) -> Result<u64, Error> {
758        Ok(self.data.borrow().epoch_us)
759    }
760
761    fn epoch_s(&self, _ctx: impl ReadContext) -> Result<u32, Error> {
762        Ok(self.data.borrow().epoch_s)
763    }
764
765    fn vendor_id(&self, _ctx: impl ReadContext) -> Result<u16, Error> {
766        Ok(self.data.borrow().vendor_id)
767    }
768
769    fn list_nullables_and_optionals_struct<P: TLVBuilderParent>(
770        &self,
771        _ctx: impl ReadContext,
772        builder: ArrayAttributeRead<
773            NullablesAndOptionalsStructArrayBuilder<P>,
774            NullablesAndOptionalsStructBuilder<P>,
775        >,
776    ) -> Result<P, Error> {
777        fn read_one<PP: TLVBuilderParent>(
778            builder: NullablesAndOptionalsStructBuilder<PP>,
779            s: &NullablesAndOptionalsStructOwned,
780        ) -> Result<PP, Error> {
781            builder
782                .nullable_int(s.nullable_int.clone())?
783                .optional_int(s.optional_int)?
784                .nullable_optional_int(s.nullable_optional_int.clone())?
785                .nullable_string(s.nullable_string.as_deref())?
786                .optional_string(s.optional_string.as_deref())?
787                .nullable_optional_string(
788                    s.nullable_optional_string.as_ref().map(|s| s.as_deref()),
789                )?
790                .nullable_struct()?
791                .with_non_null(s.nullable_struct.as_ref(), |ss, builder| {
792                    builder
793                        .a(ss.a)?
794                        .b(ss.b)?
795                        .c(ss.c)?
796                        .d(Octets(&ss.d))?
797                        .e(ss.e.as_str())?
798                        .f(ss.f)?
799                        .g(ss.g)?
800                        .h(ss.h)?
801                        .i(ss.i)?
802                        .end()
803                })?
804                .optional_struct()?
805                .with_some(s.optional_struct.as_ref(), |ss, builder| {
806                    builder
807                        .a(ss.a)?
808                        .b(ss.b)?
809                        .c(ss.c)?
810                        .d(Octets(&ss.d))?
811                        .e(ss.e.as_str())?
812                        .f(ss.f)?
813                        .g(ss.g)?
814                        .h(ss.h)?
815                        .i(ss.i)?
816                        .end()
817                })?
818                .nullable_optional_struct()?
819                .with_some(s.nullable_optional_struct.as_ref(), |ss, builder| {
820                    builder.with_non_null(ss.as_ref(), |ss, builder| {
821                        builder
822                            .a(ss.a)?
823                            .b(ss.b)?
824                            .c(ss.c)?
825                            .d(Octets(&ss.d))?
826                            .e(ss.e.as_str())?
827                            .f(ss.f)?
828                            .g(ss.g)?
829                            .h(ss.h)?
830                            .i(ss.i)?
831                            .end()
832                    })
833                })?
834                .nullable_list()?
835                .with_non_null(s.nullable_list.as_ref(), |l, mut builder| {
836                    for s in *l {
837                        builder = builder.push(s)?;
838                    }
839
840                    builder.end()
841                })?
842                .optional_list()?
843                .with_some(s.optional_list.as_ref(), |l, mut builder| {
844                    for s in *l {
845                        builder = builder.push(s)?;
846                    }
847
848                    builder.end()
849                })?
850                .nullable_optional_list()?
851                .with_some(s.nullable_optional_list.as_ref(), |l, builder| {
852                    builder.with_non_null(l.as_ref(), |l, mut builder| {
853                        for s in *l {
854                            builder = builder.push(s)?;
855                        }
856
857                        builder.end()
858                    })
859                })?
860                .end()
861        }
862
863        match builder {
864            ArrayAttributeRead::ReadOne(index, builder) => {
865                let data = self.data.borrow();
866                if index < data.list_nullables_and_optionals_struct.len() as u16 {
867                    let s = &data.list_nullables_and_optionals_struct[index as usize];
868
869                    read_one(builder, s)
870                } else {
871                    Err(ErrorCode::ConstraintError.into())
872                }
873            }
874            ArrayAttributeRead::ReadAll(mut builder) => {
875                let data = self.data.borrow();
876
877                for s in &data.list_nullables_and_optionals_struct {
878                    builder = read_one(builder.push()?, s)?;
879                }
880
881                builder.end()
882            }
883            ArrayAttributeRead::ReadNone(builder) => builder.end(),
884        }
885    }
886
887    fn enum_attr(&self, _ctx: impl ReadContext) -> Result<SimpleEnum, Error> {
888        Ok(self.data.borrow().enum_attr)
889    }
890
891    fn struct_attr<P: TLVBuilderParent>(
892        &self,
893        _ctx: impl ReadContext,
894        builder: SimpleStructBuilder<P>,
895    ) -> Result<P, Error> {
896        let data = self.data.borrow();
897        let s = &data.struct_attr;
898
899        builder
900            .a(s.a)?
901            .b(s.b)?
902            .c(s.c)?
903            .d(Octets(&s.d))?
904            .e(s.e.as_str())?
905            .f(s.f)?
906            .g(s.g)?
907            .h(s.h)?
908            .i(s.i)?
909            .end()
910    }
911
912    fn range_restricted_int_8_u(&self, _ctx: impl ReadContext) -> Result<u8, Error> {
913        Ok(self.data.borrow().range_restricted_int_8_u)
914    }
915
916    fn range_restricted_int_8_s(&self, _ctx: impl ReadContext) -> Result<i8, Error> {
917        Ok(self.data.borrow().range_restricted_int_8_s)
918    }
919
920    fn range_restricted_int_16_u(&self, _ctx: impl ReadContext) -> Result<u16, Error> {
921        Ok(self.data.borrow().range_restricted_int_16_u)
922    }
923
924    fn range_restricted_int_16_s(&self, _ctx: impl ReadContext) -> Result<i16, Error> {
925        Ok(self.data.borrow().range_restricted_int_16_s)
926    }
927
928    fn list_long_octet_string<P: TLVBuilderParent>(
929        &self,
930        _ctx: impl ReadContext,
931        builder: ArrayAttributeRead<OctetsArrayBuilder<P>, OctetsBuilder<P>>,
932    ) -> Result<P, Error> {
933        match builder {
934            ArrayAttributeRead::ReadOne(index, builder) => {
935                let data = self.data.borrow();
936                if index < data.list_long_octet_string.len() as u16 {
937                    builder.set(Octets(
938                        data.list_long_octet_string[index as usize].as_slice(),
939                    ))
940                } else {
941                    Err(ErrorCode::ConstraintError.into())
942                }
943            }
944            ArrayAttributeRead::ReadAll(mut builder) => {
945                let data = self.data.borrow();
946
947                for i in &data.list_long_octet_string {
948                    builder = builder.push(Octets(i.as_slice()))?;
949                }
950
951                builder.end()
952            }
953            ArrayAttributeRead::ReadNone(builder) => builder.end(),
954        }
955    }
956
957    fn list_fabric_scoped<P: TLVBuilderParent>(
958        &self,
959        ctx: impl ReadContext,
960        builder: ArrayAttributeRead<TestFabricScopedArrayBuilder<P>, TestFabricScopedBuilder<P>>,
961    ) -> Result<P, Error> {
962        fn read_into<P: TLVBuilderParent>(
963            accessing_fab_idx: u8,
964            fabric_idx: NonZeroU8,
965            s: &TestFabricScopedOwned,
966            builder: TestFabricScopedBuilder<P>,
967        ) -> Result<P, Error> {
968            let same_fab_idx = accessing_fab_idx == fabric_idx.get();
969
970            builder
971                .fabric_sensitive_int_8_u(same_fab_idx.then_some(s.fabric_sensitive_int8u))?
972                .optional_fabric_sensitive_int_8_u(
973                    same_fab_idx
974                        .then_some(s.optional_fabric_sensitive_int8u)
975                        .flatten(),
976                )?
977                .nullable_fabric_sensitive_int_8_u(
978                    same_fab_idx.then_some(s.nullable_fabric_sensitive_int8u.clone()),
979                )?
980                .nullable_optional_fabric_sensitive_int_8_u(
981                    same_fab_idx
982                        .then_some(s.optional_nullable_fabric_sensitive_int8u.clone())
983                        .flatten(),
984                )?
985                .fabric_sensitive_char_string(
986                    same_fab_idx.then_some(s.fabric_sensitive_char_string.as_str()),
987                )?
988                .fabric_sensitive_struct()?
989                .with_some_if(same_fab_idx, |builder| {
990                    builder
991                        .a(s.fabric_sensitive_struct.a)?
992                        .b(s.fabric_sensitive_struct.b)?
993                        .c(s.fabric_sensitive_struct.c)?
994                        .d(Octets(&s.fabric_sensitive_struct.d))?
995                        .e(s.fabric_sensitive_struct.e.as_str())?
996                        .f(s.fabric_sensitive_struct.f)?
997                        .g(s.fabric_sensitive_struct.g)?
998                        .h(s.fabric_sensitive_struct.h)?
999                        .i(s.fabric_sensitive_struct.i)?
1000                        .end()
1001                })?
1002                .fabric_sensitive_int_8_u_list()?
1003                .with_some_if(same_fab_idx, |mut builder| {
1004                    for i in &s.fabric_sensitive_int8u_list {
1005                        builder = builder.push(i)?;
1006                    }
1007                    builder.end()
1008                })?
1009                .fabric_index(Some(fabric_idx.get()))?
1010                .end()
1011        }
1012
1013        let attr = ctx.attr();
1014
1015        let data = self.data.borrow();
1016        let mut list = data
1017            .list_fabric_scoped
1018            .iter()
1019            .flat_map(|(fab_idx, fab_items)| fab_items.iter().map(move |item| (fab_idx, item)))
1020            .filter(|s| !attr.fab_filter || s.0.get() == attr.fab_idx);
1021
1022        match builder {
1023            ArrayAttributeRead::ReadOne(index, builder) => {
1024                let item = list.nth(index as _).ok_or(ErrorCode::ConstraintError)?;
1025                read_into(attr.fab_idx, *item.0, item.1, builder)
1026            }
1027            ArrayAttributeRead::ReadAll(mut builder) => {
1028                for s in list {
1029                    builder = read_into(attr.fab_idx, *s.0, s.1, builder.push()?)?;
1030                }
1031
1032                builder.end()
1033            }
1034            ArrayAttributeRead::ReadNone(builder) => builder.end(),
1035        }
1036    }
1037
1038    fn timed_write_boolean(&self, _ctx: impl ReadContext) -> Result<bool, Error> {
1039        Ok(self.data.borrow().timed_write_boolean)
1040    }
1041
1042    fn general_error_boolean(&self, _ctx: impl ReadContext) -> Result<bool, Error> {
1043        Err(ErrorCode::InvalidDataType.into())
1044    }
1045
1046    fn cluster_error_boolean(&self, _ctx: impl ReadContext) -> Result<bool, Error> {
1047        Err(ErrorCode::Invalid.into())
1048    }
1049
1050    fn nullable_boolean(&self, _ctx: impl ReadContext) -> Result<Nullable<bool>, Error> {
1051        Ok(self.data.borrow().nullable_boolean.clone())
1052    }
1053
1054    fn nullable_bitmap_8(&self, _ctx: impl ReadContext) -> Result<Nullable<Bitmap8MaskMap>, Error> {
1055        Ok(self.data.borrow().nullable_bitmap_8.clone())
1056    }
1057
1058    fn nullable_bitmap_16(
1059        &self,
1060        _ctx: impl ReadContext,
1061    ) -> Result<Nullable<Bitmap16MaskMap>, Error> {
1062        Ok(self.data.borrow().nullable_bitmap_16.clone())
1063    }
1064
1065    fn nullable_bitmap_32(
1066        &self,
1067        _ctx: impl ReadContext,
1068    ) -> Result<Nullable<Bitmap32MaskMap>, Error> {
1069        Ok(self.data.borrow().nullable_bitmap_32.clone())
1070    }
1071
1072    fn nullable_bitmap_64(
1073        &self,
1074        _ctx: impl ReadContext,
1075    ) -> Result<Nullable<Bitmap64MaskMap>, Error> {
1076        Ok(self.data.borrow().nullable_bitmap_64.clone())
1077    }
1078
1079    fn nullable_int_8_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u8>, Error> {
1080        Ok(self.data.borrow().nullable_int_8_u.clone())
1081    }
1082
1083    fn nullable_int_16_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u16>, Error> {
1084        Ok(self.data.borrow().nullable_int_16_u.clone())
1085    }
1086
1087    fn nullable_int_24_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u32>, Error> {
1088        Ok(self.data.borrow().nullable_int_24_u.clone())
1089    }
1090
1091    fn nullable_int_32_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u32>, Error> {
1092        Ok(self.data.borrow().nullable_int_32_u.clone())
1093    }
1094
1095    fn nullable_int_40_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u64>, Error> {
1096        Ok(self.data.borrow().nullable_int_40_u.clone())
1097    }
1098
1099    fn nullable_int_48_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u64>, Error> {
1100        Ok(self.data.borrow().nullable_int_48_u.clone())
1101    }
1102
1103    fn nullable_int_56_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u64>, Error> {
1104        Ok(self.data.borrow().nullable_int_56_u.clone())
1105    }
1106
1107    fn nullable_int_64_u(&self, _ctx: impl ReadContext) -> Result<Nullable<u64>, Error> {
1108        Ok(self.data.borrow().nullable_int_64_u.clone())
1109    }
1110
1111    fn nullable_int_8_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i8>, Error> {
1112        Ok(self.data.borrow().nullable_int_8_s.clone())
1113    }
1114
1115    fn nullable_int_16_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i16>, Error> {
1116        Ok(self.data.borrow().nullable_int_16_s.clone())
1117    }
1118
1119    fn nullable_int_24_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i32>, Error> {
1120        Ok(self.data.borrow().nullable_int_24_s.clone())
1121    }
1122
1123    fn nullable_int_32_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i32>, Error> {
1124        Ok(self.data.borrow().nullable_int_32_s.clone())
1125    }
1126
1127    fn nullable_int_40_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i64>, Error> {
1128        Ok(self.data.borrow().nullable_int_40_s.clone())
1129    }
1130
1131    fn nullable_int_48_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i64>, Error> {
1132        Ok(self.data.borrow().nullable_int_48_s.clone())
1133    }
1134
1135    fn nullable_int_56_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i64>, Error> {
1136        Ok(self.data.borrow().nullable_int_56_s.clone())
1137    }
1138
1139    fn nullable_int_64_s(&self, _ctx: impl ReadContext) -> Result<Nullable<i64>, Error> {
1140        Ok(self.data.borrow().nullable_int_64_s.clone())
1141    }
1142
1143    fn nullable_enum_8(&self, _ctx: impl ReadContext) -> Result<Nullable<u8>, Error> {
1144        Ok(self.data.borrow().nullable_enum_8.clone())
1145    }
1146
1147    fn nullable_enum_16(&self, _ctx: impl ReadContext) -> Result<Nullable<u16>, Error> {
1148        Ok(self.data.borrow().nullable_enum_16.clone())
1149    }
1150
1151    fn nullable_float_single(&self, _ctx: impl ReadContext) -> Result<Nullable<f32>, Error> {
1152        Ok(self.data.borrow().nullable_float_single.clone())
1153    }
1154
1155    fn nullable_float_double(&self, _ctx: impl ReadContext) -> Result<Nullable<f64>, Error> {
1156        Ok(self.data.borrow().nullable_float_double.clone())
1157    }
1158
1159    fn nullable_octet_string<P: TLVBuilderParent>(
1160        &self,
1161        _ctx: impl ReadContext,
1162        builder: NullableBuilder<P, OctetsBuilder<P>>,
1163    ) -> Result<P, Error> {
1164        if let Some(o) = self.data.borrow().nullable_octet_string.as_opt_deref() {
1165            builder.non_null()?.set(Octets(o))
1166        } else {
1167            builder.null()
1168        }
1169    }
1170
1171    fn nullable_char_string<P: TLVBuilderParent>(
1172        &self,
1173        _ctx: impl ReadContext,
1174        builder: NullableBuilder<P, Utf8StrBuilder<P>>,
1175    ) -> Result<P, Error> {
1176        if let Some(o) = self.data.borrow().nullable_char_string.as_opt_deref() {
1177            builder.non_null()?.set(o)
1178        } else {
1179            builder.null()
1180        }
1181    }
1182
1183    fn nullable_enum_attr(&self, _ctx: impl ReadContext) -> Result<Nullable<SimpleEnum>, Error> {
1184        Ok(self.data.borrow().nullable_enum_attr.clone())
1185    }
1186
1187    fn nullable_struct<P: TLVBuilderParent>(
1188        &self,
1189        _ctx: impl ReadContext,
1190        builder: NullableBuilder<P, SimpleStructBuilder<P>>,
1191    ) -> Result<P, Error> {
1192        if let Some(s) = self.data.borrow().nullable_struct.as_opt_ref() {
1193            let builder = builder.non_null()?;
1194
1195            builder
1196                .a(s.a)?
1197                .b(s.b)?
1198                .c(s.c)?
1199                .d(Octets(&s.d))?
1200                .e(s.e.as_str())?
1201                .f(s.f)?
1202                .g(s.g)?
1203                .h(s.h)?
1204                .i(s.i)?
1205                .end()
1206        } else {
1207            builder.null()
1208        }
1209    }
1210
1211    fn nullable_range_restricted_int_8_u(
1212        &self,
1213        _ctx: impl ReadContext,
1214    ) -> Result<Nullable<u8>, Error> {
1215        Ok(self.data.borrow().nullable_range_restricted_int_8_u.clone())
1216    }
1217
1218    fn nullable_range_restricted_int_8_s(
1219        &self,
1220        _ctx: impl ReadContext,
1221    ) -> Result<Nullable<i8>, Error> {
1222        Ok(self.data.borrow().nullable_range_restricted_int_8_s.clone())
1223    }
1224
1225    fn nullable_range_restricted_int_16_u(
1226        &self,
1227        _ctx: impl ReadContext,
1228    ) -> Result<Nullable<u16>, Error> {
1229        Ok(self
1230            .data
1231            .borrow()
1232            .nullable_range_restricted_int_16_u
1233            .clone())
1234    }
1235
1236    fn nullable_range_restricted_int_16_s(
1237        &self,
1238        _ctx: impl ReadContext,
1239    ) -> Result<Nullable<i16>, Error> {
1240        Ok(self
1241            .data
1242            .borrow()
1243            .nullable_range_restricted_int_16_s
1244            .clone())
1245    }
1246
1247    fn global_enum(&self, _ctx: impl ReadContext) -> Result<TestGlobalEnum, Error> {
1248        Ok(self.data.borrow().global_enum)
1249    }
1250
1251    fn global_struct<P: TLVBuilderParent>(
1252        &self,
1253        _ctx: impl ReadContext,
1254        builder: TestGlobalStructBuilder<P>,
1255    ) -> Result<P, Error> {
1256        let s = &self.data.borrow().global_struct;
1257        builder
1258            .name(s.name.as_str())?
1259            .my_bitmap(s.my_bitmap.clone())?
1260            .my_enum(s.my_enum.clone())?
1261            .end()
1262    }
1263
1264    fn nullable_global_enum(
1265        &self,
1266        _ctx: impl ReadContext,
1267    ) -> Result<Nullable<TestGlobalEnum>, Error> {
1268        Ok(Nullable::some(self.data.borrow().global_enum))
1269    }
1270
1271    fn nullable_global_struct<P: TLVBuilderParent>(
1272        &self,
1273        _ctx: impl ReadContext,
1274        builder: NullableBuilder<P, TestGlobalStructBuilder<P>>,
1275    ) -> Result<P, Error> {
1276        if let Some(s) = self.data.borrow().nullable_global_struct.as_opt_ref() {
1277            let builder = builder.non_null()?;
1278
1279            builder
1280                .name(s.name.as_str())?
1281                .my_bitmap(s.my_bitmap.clone())?
1282                .my_enum(s.my_enum.clone())?
1283                .end()
1284        } else {
1285            builder.null()
1286        }
1287    }
1288
1289    fn set_boolean(&self, _ctx: impl WriteContext, value: bool) -> Result<(), Error> {
1290        self.data.borrow_mut().boolean = value;
1291        Ok(())
1292    }
1293
1294    fn set_bitmap_8(&self, _ctx: impl WriteContext, value: Bitmap8MaskMap) -> Result<(), Error> {
1295        self.data.borrow_mut().bitmap_8 = value;
1296        Ok(())
1297    }
1298
1299    fn set_bitmap_16(&self, _ctx: impl WriteContext, value: Bitmap16MaskMap) -> Result<(), Error> {
1300        self.data.borrow_mut().bitmap_16 = value;
1301        Ok(())
1302    }
1303
1304    fn set_bitmap_32(&self, _ctx: impl WriteContext, value: Bitmap32MaskMap) -> Result<(), Error> {
1305        self.data.borrow_mut().bitmap_32 = value;
1306        Ok(())
1307    }
1308
1309    fn set_bitmap_64(&self, _ctx: impl WriteContext, value: Bitmap64MaskMap) -> Result<(), Error> {
1310        self.data.borrow_mut().bitmap_64 = value;
1311        Ok(())
1312    }
1313
1314    fn set_int_8_u(&self, _ctx: impl WriteContext, value: u8) -> Result<(), Error> {
1315        self.data.borrow_mut().int_8_u = value;
1316        Ok(())
1317    }
1318
1319    fn set_int_16_u(&self, _ctx: impl WriteContext, value: u16) -> Result<(), Error> {
1320        self.data.borrow_mut().int_16_u = value;
1321        Ok(())
1322    }
1323
1324    fn set_int_24_u(&self, _ctx: impl WriteContext, value: u32) -> Result<(), Error> {
1325        self.data.borrow_mut().int_24_u = value;
1326        Ok(())
1327    }
1328
1329    fn set_int_32_u(&self, _ctx: impl WriteContext, value: u32) -> Result<(), Error> {
1330        self.data.borrow_mut().int_32_u = value;
1331        Ok(())
1332    }
1333
1334    fn set_int_40_u(&self, _ctx: impl WriteContext, value: u64) -> Result<(), Error> {
1335        self.data.borrow_mut().int_40_u = value;
1336        Ok(())
1337    }
1338
1339    fn set_int_48_u(&self, _ctx: impl WriteContext, value: u64) -> Result<(), Error> {
1340        self.data.borrow_mut().int_48_u = value;
1341        Ok(())
1342    }
1343
1344    fn set_int_56_u(&self, _ctx: impl WriteContext, value: u64) -> Result<(), Error> {
1345        self.data.borrow_mut().int_56_u = value;
1346        Ok(())
1347    }
1348
1349    fn set_int_64_u(&self, _ctx: impl WriteContext, value: u64) -> Result<(), Error> {
1350        self.data.borrow_mut().int_64_u = value;
1351        Ok(())
1352    }
1353
1354    fn set_int_8_s(&self, _ctx: impl WriteContext, value: i8) -> Result<(), Error> {
1355        self.data.borrow_mut().int_8_s = value;
1356        Ok(())
1357    }
1358
1359    fn set_int_16_s(&self, _ctx: impl WriteContext, value: i16) -> Result<(), Error> {
1360        self.data.borrow_mut().int_16_s = value;
1361        Ok(())
1362    }
1363
1364    fn set_int_24_s(&self, _ctx: impl WriteContext, value: i32) -> Result<(), Error> {
1365        self.data.borrow_mut().int_24_s = value;
1366        Ok(())
1367    }
1368
1369    fn set_int_32_s(&self, _ctx: impl WriteContext, value: i32) -> Result<(), Error> {
1370        self.data.borrow_mut().int_32_s = value;
1371        Ok(())
1372    }
1373
1374    fn set_int_40_s(&self, _ctx: impl WriteContext, value: i64) -> Result<(), Error> {
1375        self.data.borrow_mut().int_40_s = value;
1376        Ok(())
1377    }
1378
1379    fn set_int_48_s(&self, _ctx: impl WriteContext, value: i64) -> Result<(), Error> {
1380        self.data.borrow_mut().int_48_s = value;
1381        Ok(())
1382    }
1383
1384    fn set_int_56_s(&self, _ctx: impl WriteContext, value: i64) -> Result<(), Error> {
1385        self.data.borrow_mut().int_56_s = value;
1386        Ok(())
1387    }
1388
1389    fn set_int_64_s(&self, _ctx: impl WriteContext, value: i64) -> Result<(), Error> {
1390        self.data.borrow_mut().int_64_s = value;
1391        Ok(())
1392    }
1393
1394    fn set_enum_8(&self, _ctx: impl WriteContext, value: u8) -> Result<(), Error> {
1395        self.data.borrow_mut().enum_8 = value;
1396        Ok(())
1397    }
1398
1399    fn set_enum_16(&self, _ctx: impl WriteContext, value: u16) -> Result<(), Error> {
1400        self.data.borrow_mut().enum_16 = value;
1401        Ok(())
1402    }
1403
1404    fn set_float_single(&self, _ctx: impl WriteContext, value: f32) -> Result<(), Error> {
1405        self.data.borrow_mut().float_single = value;
1406        Ok(())
1407    }
1408
1409    fn set_float_double(&self, _ctx: impl WriteContext, value: f64) -> Result<(), Error> {
1410        self.data.borrow_mut().float_double = value;
1411        Ok(())
1412    }
1413
1414    fn set_octet_string(&self, _ctx: impl WriteContext, value: OctetStr<'_>) -> Result<(), Error> {
1415        self.data.borrow_mut().octet_string =
1416            value.0.try_into().map_err(|_| ErrorCode::ConstraintError)?;
1417        Ok(())
1418    }
1419
1420    fn set_list_int_8_u(
1421        &self,
1422        _ctx: impl WriteContext,
1423        value: ArrayAttributeWrite<TLVArray<'_, u8>, u8>,
1424    ) -> Result<(), Error> {
1425        match value {
1426            ArrayAttributeWrite::Replace(arr) => {
1427                if arr.iter().count() > 16 {
1428                    return Err(ErrorCode::ConstraintError.into());
1429                }
1430
1431                let mut data = self.data.borrow_mut();
1432                data.list_int_8_u.clear();
1433                for i in arr {
1434                    unwrap!(data.list_int_8_u.push(i?));
1435                }
1436
1437                Ok(())
1438            }
1439            ArrayAttributeWrite::Add(item) => {
1440                let mut data = self.data.borrow_mut();
1441                if data.list_int_8_u.len() < 16 {
1442                    unwrap!(data.list_int_8_u.push(item));
1443                    Ok(())
1444                } else {
1445                    Err(ErrorCode::ConstraintError.into())
1446                }
1447            }
1448            ArrayAttributeWrite::Update(index, item) => {
1449                let mut data = self.data.borrow_mut();
1450                if index < data.list_int_8_u.len() as u16 {
1451                    data.list_int_8_u[index as usize] = item;
1452                    Ok(())
1453                } else {
1454                    Err(ErrorCode::ConstraintError.into())
1455                }
1456            }
1457            ArrayAttributeWrite::Remove(index) => {
1458                let mut data = self.data.borrow_mut();
1459                if index < data.list_int_8_u.len() as u16 {
1460                    let _ = data.list_int_8_u.remove(index as usize);
1461                    Ok(())
1462                } else {
1463                    Err(ErrorCode::ConstraintError.into())
1464                }
1465            }
1466        }
1467    }
1468
1469    fn set_list_octet_string(
1470        &self,
1471        _ctx: impl WriteContext,
1472        value: ArrayAttributeWrite<TLVArray<'_, OctetStr<'_>>, OctetStr<'_>>,
1473    ) -> Result<(), Error> {
1474        match value {
1475            ArrayAttributeWrite::Replace(arr) => {
1476                if arr.iter().count() > 16 {
1477                    return Err(ErrorCode::ConstraintError.into());
1478                }
1479
1480                let mut data = self.data.borrow_mut();
1481                data.list_octet_string.clear();
1482                for i in arr {
1483                    unwrap!(data
1484                        .list_octet_string
1485                        .push(i?.0.try_into().map_err(|_| ErrorCode::ConstraintError)?));
1486                }
1487
1488                Ok(())
1489            }
1490            ArrayAttributeWrite::Add(item) => {
1491                let mut data = self.data.borrow_mut();
1492                if data.list_octet_string.len() < 16 {
1493                    unwrap!(data
1494                        .list_octet_string
1495                        .push(item.0.try_into().map_err(|_| ErrorCode::ConstraintError)?));
1496                    Ok(())
1497                } else {
1498                    Err(ErrorCode::ConstraintError.into())
1499                }
1500            }
1501            ArrayAttributeWrite::Update(index, item) => {
1502                let mut data = self.data.borrow_mut();
1503                if index < data.list_octet_string.len() as u16 {
1504                    data.list_octet_string[index as usize] =
1505                        item.0.try_into().map_err(|_| ErrorCode::ConstraintError)?;
1506                    Ok(())
1507                } else {
1508                    Err(ErrorCode::ConstraintError.into())
1509                }
1510            }
1511            ArrayAttributeWrite::Remove(index) => {
1512                let mut data = self.data.borrow_mut();
1513                if index < data.list_octet_string.len() as u16 {
1514                    let _ = data.list_octet_string.remove(index as usize);
1515                    Ok(())
1516                } else {
1517                    Err(ErrorCode::ConstraintError.into())
1518                }
1519            }
1520        }
1521    }
1522
1523    fn set_list_struct_octet_string(
1524        &self,
1525        _ctx: impl WriteContext,
1526        value: ArrayAttributeWrite<TLVArray<'_, TestListStructOctet<'_>>, TestListStructOctet<'_>>,
1527    ) -> Result<(), Error> {
1528        match value {
1529            ArrayAttributeWrite::Replace(arr) => {
1530                if arr.iter().count() > 16 {
1531                    return Err(ErrorCode::ConstraintError.into());
1532                }
1533
1534                let mut data = self.data.borrow_mut();
1535                data.list_struct_octet_string.clear();
1536                for i in arr {
1537                    let s = i?;
1538
1539                    unwrap!(data
1540                        .list_struct_octet_string
1541                        .push(TestListStructOctetOwned {
1542                            member_1: s.member_1()?,
1543                            member_2: s
1544                                .member_2()?
1545                                .0
1546                                .try_into()
1547                                .map_err(|_| ErrorCode::ConstraintError)?,
1548                        }));
1549                }
1550
1551                Ok(())
1552            }
1553            ArrayAttributeWrite::Add(item) => {
1554                let mut data = self.data.borrow_mut();
1555                if data.list_struct_octet_string.len() < 16 {
1556                    unwrap!(data
1557                        .list_struct_octet_string
1558                        .push(TestListStructOctetOwned {
1559                            member_1: item.member_1()?,
1560                            member_2: item
1561                                .member_2()?
1562                                .0
1563                                .try_into()
1564                                .map_err(|_| ErrorCode::ConstraintError)?,
1565                        }));
1566                    Ok(())
1567                } else {
1568                    Err(ErrorCode::ConstraintError.into())
1569                }
1570            }
1571            ArrayAttributeWrite::Update(index, item) => {
1572                let mut data = self.data.borrow_mut();
1573                if index < data.list_struct_octet_string.len() as u16 {
1574                    data.list_struct_octet_string[index as usize] = TestListStructOctetOwned {
1575                        member_1: item.member_1()?,
1576                        member_2: item
1577                            .member_2()?
1578                            .0
1579                            .try_into()
1580                            .map_err(|_| ErrorCode::ConstraintError)?,
1581                    };
1582                    Ok(())
1583                } else {
1584                    Err(ErrorCode::ConstraintError.into())
1585                }
1586            }
1587            ArrayAttributeWrite::Remove(index) => {
1588                let mut data = self.data.borrow_mut();
1589                if index < data.list_struct_octet_string.len() as u16 {
1590                    let _ = data.list_struct_octet_string.remove(index as usize);
1591                    Ok(())
1592                } else {
1593                    Err(ErrorCode::ConstraintError.into())
1594                }
1595            }
1596        }
1597    }
1598
1599    fn set_long_octet_string(
1600        &self,
1601        _ctx: impl WriteContext,
1602        value: OctetStr<'_>,
1603    ) -> Result<(), Error> {
1604        self.data.borrow_mut().long_octet_string =
1605            value.0.try_into().map_err(|_| ErrorCode::ConstraintError)?;
1606        Ok(())
1607    }
1608
1609    fn set_char_string(&self, _ctx: impl WriteContext, value: Utf8Str<'_>) -> Result<(), Error> {
1610        self.data.borrow_mut().char_string =
1611            value.try_into().map_err(|_| ErrorCode::ConstraintError)?;
1612        Ok(())
1613    }
1614
1615    fn set_long_char_string(
1616        &self,
1617        _ctx: impl WriteContext,
1618        value: Utf8Str<'_>,
1619    ) -> Result<(), Error> {
1620        self.data.borrow_mut().long_char_string =
1621            value.try_into().map_err(|_| ErrorCode::ConstraintError)?;
1622        Ok(())
1623    }
1624
1625    fn set_epoch_us(&self, _ctx: impl WriteContext, value: u64) -> Result<(), Error> {
1626        self.data.borrow_mut().epoch_us = value;
1627        Ok(())
1628    }
1629
1630    fn set_epoch_s(&self, _ctx: impl WriteContext, value: u32) -> Result<(), Error> {
1631        self.data.borrow_mut().epoch_s = value;
1632        Ok(())
1633    }
1634
1635    fn set_vendor_id(&self, _ctx: impl WriteContext, value: u16) -> Result<(), Error> {
1636        self.data.borrow_mut().vendor_id = value;
1637        Ok(())
1638    }
1639
1640    fn set_list_nullables_and_optionals_struct(
1641        &self,
1642        _ctx: impl WriteContext,
1643        value: ArrayAttributeWrite<
1644            TLVArray<'_, NullablesAndOptionalsStruct<'_>>,
1645            NullablesAndOptionalsStruct<'_>,
1646        >,
1647    ) -> Result<(), Error> {
1648        fn to_owned<'a>(
1649            s: &'a NullablesAndOptionalsStruct<'a>,
1650        ) -> impl Init<NullablesAndOptionalsStructOwned, Error> + 'a {
1651            NullablesAndOptionalsStructOwned::init()
1652                .into_fallible()
1653                .chain(|o| o.update(s))
1654        }
1655
1656        let no_space = || ErrorCode::ResourceExhausted.into();
1657
1658        match value {
1659            ArrayAttributeWrite::Replace(arr) => {
1660                if arr.iter().count() > 16 {
1661                    return Err(ErrorCode::ConstraintError.into());
1662                }
1663
1664                let mut data = self.data.borrow_mut();
1665                data.list_nullables_and_optionals_struct.clear();
1666                for i in arr {
1667                    let s = i?;
1668
1669                    data.list_nullables_and_optionals_struct
1670                        .push_init(to_owned(&s), no_space)?;
1671                }
1672
1673                Ok(())
1674            }
1675            ArrayAttributeWrite::Add(item) => {
1676                let mut data = self.data.borrow_mut();
1677                if data.list_nullables_and_optionals_struct.len() < 16 {
1678                    data.list_nullables_and_optionals_struct
1679                        .push_init(to_owned(&item), no_space)
1680                } else {
1681                    Err(ErrorCode::ConstraintError.into())
1682                }
1683            }
1684            ArrayAttributeWrite::Update(index, item) => {
1685                let mut data = self.data.borrow_mut();
1686                if index < data.list_nullables_and_optionals_struct.len() as u16 {
1687                    data.list_nullables_and_optionals_struct[index as usize].update(&item)?;
1688                    Ok(())
1689                } else {
1690                    Err(ErrorCode::ConstraintError.into())
1691                }
1692            }
1693            ArrayAttributeWrite::Remove(index) => {
1694                let mut data = self.data.borrow_mut();
1695                if index < data.list_nullables_and_optionals_struct.len() as u16 {
1696                    let _ = data
1697                        .list_nullables_and_optionals_struct
1698                        .remove(index as usize);
1699                    Ok(())
1700                } else {
1701                    Err(ErrorCode::ConstraintError.into())
1702                }
1703            }
1704        }
1705    }
1706
1707    fn set_enum_attr(&self, _ctx: impl WriteContext, value: SimpleEnum) -> Result<(), Error> {
1708        self.data.borrow_mut().enum_attr = value;
1709        Ok(())
1710    }
1711
1712    fn set_struct_attr(
1713        &self,
1714        _ctx: impl WriteContext,
1715        value: SimpleStruct<'_>,
1716    ) -> Result<(), Error> {
1717        let mut data = self.data.borrow_mut();
1718
1719        let s = &mut data.struct_attr;
1720        s.a = value.a()?;
1721        s.b = value.b()?;
1722        s.c = value.c()?;
1723        s.d = value
1724            .d()?
1725            .0
1726            .try_into()
1727            .map_err(|_| ErrorCode::ConstraintError)?;
1728        s.e = value
1729            .e()?
1730            .try_into()
1731            .map_err(|_| ErrorCode::ConstraintError)?;
1732        s.f = value.f()?;
1733        s.g = value.g()?;
1734        s.h = value.h()?;
1735
1736        Ok(())
1737    }
1738
1739    fn set_range_restricted_int_8_u(
1740        &self,
1741        _ctx: impl WriteContext,
1742        value: u8,
1743    ) -> Result<(), Error> {
1744        const RANGE: core::ops::RangeInclusive<u8> = 20..=100;
1745
1746        if RANGE.contains(&value) {
1747            self.data.borrow_mut().range_restricted_int_8_u = value;
1748        } else {
1749            Err(ErrorCode::ConstraintError)?;
1750        }
1751
1752        Ok(())
1753    }
1754
1755    fn set_range_restricted_int_8_s(
1756        &self,
1757        _ctx: impl WriteContext,
1758        value: i8,
1759    ) -> Result<(), Error> {
1760        const RANGE: core::ops::RangeInclusive<i8> = -40..=50;
1761
1762        if RANGE.contains(&value) {
1763            self.data.borrow_mut().range_restricted_int_8_s = value;
1764        } else {
1765            Err(ErrorCode::ConstraintError)?;
1766        }
1767
1768        Ok(())
1769    }
1770
1771    fn set_range_restricted_int_16_u(
1772        &self,
1773        _ctx: impl WriteContext,
1774        value: u16,
1775    ) -> Result<(), Error> {
1776        const RANGE: core::ops::RangeInclusive<u16> = 100..=1000;
1777
1778        if RANGE.contains(&value) {
1779            self.data.borrow_mut().range_restricted_int_16_u = value;
1780        } else {
1781            Err(ErrorCode::ConstraintError)?;
1782        }
1783
1784        Ok(())
1785    }
1786
1787    fn set_range_restricted_int_16_s(
1788        &self,
1789        _ctx: impl WriteContext,
1790        value: i16,
1791    ) -> Result<(), Error> {
1792        const RANGE: core::ops::RangeInclusive<i16> = -150..=200;
1793
1794        if RANGE.contains(&value) {
1795            self.data.borrow_mut().range_restricted_int_16_s = value;
1796        } else {
1797            Err(ErrorCode::ConstraintError)?;
1798        }
1799
1800        Ok(())
1801    }
1802
1803    fn set_list_long_octet_string(
1804        &self,
1805        _ctx: impl WriteContext,
1806        value: ArrayAttributeWrite<TLVArray<'_, OctetStr<'_>>, OctetStr<'_>>,
1807    ) -> Result<(), Error> {
1808        match value {
1809            ArrayAttributeWrite::Replace(arr) => {
1810                if arr.iter().count() > 16 {
1811                    return Err(ErrorCode::ConstraintError.into());
1812                }
1813
1814                let mut data = self.data.borrow_mut();
1815                data.list_long_octet_string.clear();
1816                for i in arr {
1817                    unwrap!(data
1818                        .list_long_octet_string
1819                        .push(i?.0.try_into().map_err(|_| ErrorCode::ConstraintError)?));
1820                }
1821
1822                Ok(())
1823            }
1824            ArrayAttributeWrite::Add(item) => {
1825                let mut data = self.data.borrow_mut();
1826                if data.list_long_octet_string.len() < 16 {
1827                    unwrap!(data
1828                        .list_long_octet_string
1829                        .push(item.0.try_into().map_err(|_| ErrorCode::ConstraintError)?));
1830                    Ok(())
1831                } else {
1832                    Err(ErrorCode::ConstraintError.into())
1833                }
1834            }
1835            ArrayAttributeWrite::Update(index, item) => {
1836                let mut data = self.data.borrow_mut();
1837                if index < data.list_long_octet_string.len() as u16 {
1838                    data.list_long_octet_string[index as usize] =
1839                        item.0.try_into().map_err(|_| ErrorCode::ConstraintError)?;
1840                    Ok(())
1841                } else {
1842                    Err(ErrorCode::ConstraintError.into())
1843                }
1844            }
1845            ArrayAttributeWrite::Remove(index) => {
1846                let mut data = self.data.borrow_mut();
1847                if index < data.list_long_octet_string.len() as u16 {
1848                    let _ = data.list_long_octet_string.remove(index as usize);
1849                    Ok(())
1850                } else {
1851                    Err(ErrorCode::ConstraintError.into())
1852                }
1853            }
1854        }
1855    }
1856
1857    fn set_list_fabric_scoped(
1858        &self,
1859        ctx: impl WriteContext,
1860        value: ArrayAttributeWrite<TLVArray<'_, TestFabricScoped<'_>>, TestFabricScoped<'_>>,
1861    ) -> Result<(), Error> {
1862        let fab_idx = NonZeroU8::new(ctx.attr().fab_idx).ok_or(ErrorCode::ConstraintError)?;
1863
1864        let mut data = self.data.borrow_mut();
1865        let list = data
1866            .list_fabric_scoped
1867            .iter_mut()
1868            .find(|(idx, _)| *idx == fab_idx)
1869            .map(|(_, items)| items);
1870
1871        let list = if let Some(list) = list {
1872            list
1873        } else {
1874            data.list_fabric_scoped
1875                .push((fab_idx, Vec::new()))
1876                .map_err(|_| ErrorCode::ResourceExhausted)?;
1877
1878            &mut data.list_fabric_scoped.last_mut().unwrap().1
1879        };
1880
1881        match value {
1882            ArrayAttributeWrite::Replace(array) => {
1883                list.clear();
1884
1885                for i in array {
1886                    let s = i?;
1887
1888                    list.push_init(TestFabricScopedOwned::init().into_fallible(), || {
1889                        ErrorCode::ResourceExhausted
1890                    })?;
1891
1892                    list.last_mut().unwrap().update(&s)?;
1893                }
1894            }
1895            ArrayAttributeWrite::Add(s) => {
1896                list.push_init(TestFabricScopedOwned::init().into_fallible(), || {
1897                    ErrorCode::ResourceExhausted
1898                })?;
1899
1900                list.last_mut().unwrap().update(&s)?;
1901            }
1902            ArrayAttributeWrite::Update(index, s) => {
1903                if index as usize >= list.len() {
1904                    Err(ErrorCode::ConstraintError)?;
1905                }
1906
1907                list[index as usize].update(&s)?;
1908            }
1909            ArrayAttributeWrite::Remove(index) => {
1910                if index as usize >= list.len() {
1911                    Err(ErrorCode::ConstraintError)?;
1912                }
1913
1914                let _ = list.remove(index as usize);
1915            }
1916        }
1917
1918        Ok(())
1919    }
1920
1921    fn set_timed_write_boolean(&self, _ctx: impl WriteContext, value: bool) -> Result<(), Error> {
1922        self.data.borrow_mut().timed_write_boolean = value;
1923        Ok(())
1924    }
1925
1926    fn set_general_error_boolean(
1927        &self,
1928        _ctx: impl WriteContext,
1929        _value: bool,
1930    ) -> Result<(), Error> {
1931        Err(ErrorCode::InvalidDataType.into())
1932    }
1933
1934    fn set_cluster_error_boolean(
1935        &self,
1936        _ctx: impl WriteContext,
1937        _value: bool,
1938    ) -> Result<(), Error> {
1939        Err(ErrorCode::Invalid.into())
1940    }
1941
1942    fn set_nullable_boolean(
1943        &self,
1944        _ctx: impl WriteContext,
1945        value: Nullable<bool>,
1946    ) -> Result<(), Error> {
1947        self.data.borrow_mut().nullable_boolean = value;
1948        Ok(())
1949    }
1950
1951    fn set_nullable_bitmap_8(
1952        &self,
1953        _ctx: impl WriteContext,
1954        value: Nullable<Bitmap8MaskMap>,
1955    ) -> Result<(), Error> {
1956        self.data.borrow_mut().nullable_bitmap_8 = value;
1957        Ok(())
1958    }
1959
1960    fn set_nullable_bitmap_16(
1961        &self,
1962        _ctx: impl WriteContext,
1963        value: Nullable<Bitmap16MaskMap>,
1964    ) -> Result<(), Error> {
1965        self.data.borrow_mut().nullable_bitmap_16 = value;
1966        Ok(())
1967    }
1968
1969    fn set_nullable_bitmap_32(
1970        &self,
1971        _ctx: impl WriteContext,
1972        value: Nullable<Bitmap32MaskMap>,
1973    ) -> Result<(), Error> {
1974        self.data.borrow_mut().nullable_bitmap_32 = value;
1975        Ok(())
1976    }
1977
1978    fn set_nullable_bitmap_64(
1979        &self,
1980        _ctx: impl WriteContext,
1981        value: Nullable<Bitmap64MaskMap>,
1982    ) -> Result<(), Error> {
1983        self.data.borrow_mut().nullable_bitmap_64 = value;
1984        Ok(())
1985    }
1986
1987    fn set_nullable_int_8_u(
1988        &self,
1989        _ctx: impl WriteContext,
1990        value: Nullable<u8>,
1991    ) -> Result<(), Error> {
1992        self.data.borrow_mut().nullable_int_8_u = value;
1993        Ok(())
1994    }
1995
1996    fn set_nullable_int_16_u(
1997        &self,
1998        _ctx: impl WriteContext,
1999        value: Nullable<u16>,
2000    ) -> Result<(), Error> {
2001        self.data.borrow_mut().nullable_int_16_u = value;
2002        Ok(())
2003    }
2004
2005    fn set_nullable_int_24_u(
2006        &self,
2007        _ctx: impl WriteContext,
2008        value: Nullable<u32>,
2009    ) -> Result<(), Error> {
2010        self.data.borrow_mut().nullable_int_24_u = value;
2011        Ok(())
2012    }
2013
2014    fn set_nullable_int_32_u(
2015        &self,
2016        _ctx: impl WriteContext,
2017        value: Nullable<u32>,
2018    ) -> Result<(), Error> {
2019        self.data.borrow_mut().nullable_int_32_u = value;
2020        Ok(())
2021    }
2022
2023    fn set_nullable_int_40_u(
2024        &self,
2025        _ctx: impl WriteContext,
2026        value: Nullable<u64>,
2027    ) -> Result<(), Error> {
2028        self.data.borrow_mut().nullable_int_40_u = value;
2029        Ok(())
2030    }
2031
2032    fn set_nullable_int_48_u(
2033        &self,
2034        _ctx: impl WriteContext,
2035        value: Nullable<u64>,
2036    ) -> Result<(), Error> {
2037        self.data.borrow_mut().nullable_int_48_u = value;
2038        Ok(())
2039    }
2040
2041    fn set_nullable_int_56_u(
2042        &self,
2043        _ctx: impl WriteContext,
2044        value: Nullable<u64>,
2045    ) -> Result<(), Error> {
2046        self.data.borrow_mut().nullable_int_56_u = value;
2047        Ok(())
2048    }
2049
2050    fn set_nullable_int_64_u(
2051        &self,
2052        _ctx: impl WriteContext,
2053        value: Nullable<u64>,
2054    ) -> Result<(), Error> {
2055        self.data.borrow_mut().nullable_int_64_u = value;
2056        Ok(())
2057    }
2058
2059    fn set_nullable_int_8_s(
2060        &self,
2061        _ctx: impl WriteContext,
2062        value: Nullable<i8>,
2063    ) -> Result<(), Error> {
2064        self.data.borrow_mut().nullable_int_8_s = value;
2065        Ok(())
2066    }
2067
2068    fn set_nullable_int_16_s(
2069        &self,
2070        _ctx: impl WriteContext,
2071        value: Nullable<i16>,
2072    ) -> Result<(), Error> {
2073        self.data.borrow_mut().nullable_int_16_s = value;
2074        Ok(())
2075    }
2076
2077    fn set_nullable_int_24_s(
2078        &self,
2079        _ctx: impl WriteContext,
2080        value: Nullable<i32>,
2081    ) -> Result<(), Error> {
2082        self.data.borrow_mut().nullable_int_24_s = value;
2083        Ok(())
2084    }
2085
2086    fn set_nullable_int_32_s(
2087        &self,
2088        _ctx: impl WriteContext,
2089        value: Nullable<i32>,
2090    ) -> Result<(), Error> {
2091        self.data.borrow_mut().nullable_int_32_s = value;
2092        Ok(())
2093    }
2094
2095    fn set_nullable_int_40_s(
2096        &self,
2097        _ctx: impl WriteContext,
2098        value: Nullable<i64>,
2099    ) -> Result<(), Error> {
2100        self.data.borrow_mut().nullable_int_40_s = value;
2101        Ok(())
2102    }
2103
2104    fn set_nullable_int_48_s(
2105        &self,
2106        _ctx: impl WriteContext,
2107        value: Nullable<i64>,
2108    ) -> Result<(), Error> {
2109        self.data.borrow_mut().nullable_int_48_s = value;
2110        Ok(())
2111    }
2112
2113    fn set_nullable_int_56_s(
2114        &self,
2115        _ctx: impl WriteContext,
2116        value: Nullable<i64>,
2117    ) -> Result<(), Error> {
2118        self.data.borrow_mut().nullable_int_56_s = value;
2119        Ok(())
2120    }
2121
2122    fn set_nullable_int_64_s(
2123        &self,
2124        _ctx: impl WriteContext,
2125        value: Nullable<i64>,
2126    ) -> Result<(), Error> {
2127        self.data.borrow_mut().nullable_int_64_s = value;
2128        Ok(())
2129    }
2130
2131    fn set_nullable_enum_8(
2132        &self,
2133        _ctx: impl WriteContext,
2134        value: Nullable<u8>,
2135    ) -> Result<(), Error> {
2136        self.data.borrow_mut().nullable_enum_8 = value;
2137        Ok(())
2138    }
2139
2140    fn set_nullable_enum_16(
2141        &self,
2142        _ctx: impl WriteContext,
2143        value: Nullable<u16>,
2144    ) -> Result<(), Error> {
2145        self.data.borrow_mut().nullable_enum_16 = value;
2146        Ok(())
2147    }
2148
2149    fn set_nullable_float_single(
2150        &self,
2151        _ctx: impl WriteContext,
2152        value: Nullable<f32>,
2153    ) -> Result<(), Error> {
2154        self.data.borrow_mut().nullable_float_single = value;
2155        Ok(())
2156    }
2157
2158    fn set_nullable_float_double(
2159        &self,
2160        _ctx: impl WriteContext,
2161        value: Nullable<f64>,
2162    ) -> Result<(), Error> {
2163        self.data.borrow_mut().nullable_float_double = value;
2164        Ok(())
2165    }
2166
2167    fn set_nullable_octet_string(
2168        &self,
2169        _ctx: impl WriteContext,
2170        value: Nullable<OctetStr<'_>>,
2171    ) -> Result<(), Error> {
2172        if let Some(value) = value.into_option() {
2173            self.data.borrow_mut().nullable_octet_string =
2174                Nullable::some(value.0.try_into().map_err(|_| ErrorCode::ConstraintError)?);
2175        } else {
2176            self.data.borrow_mut().nullable_octet_string = Nullable::none();
2177        }
2178
2179        Ok(())
2180    }
2181
2182    fn set_nullable_char_string(
2183        &self,
2184        _ctx: impl WriteContext,
2185        value: Nullable<Utf8Str<'_>>,
2186    ) -> Result<(), Error> {
2187        if let Some(value) = value.into_option() {
2188            self.data.borrow_mut().nullable_char_string =
2189                Nullable::some(value.try_into().map_err(|_| ErrorCode::ConstraintError)?);
2190        } else {
2191            self.data.borrow_mut().nullable_char_string = Nullable::none();
2192        }
2193
2194        Ok(())
2195    }
2196
2197    fn set_nullable_enum_attr(
2198        &self,
2199        _ctx: impl WriteContext,
2200        value: Nullable<SimpleEnum>,
2201    ) -> Result<(), Error> {
2202        self.data.borrow_mut().nullable_enum_attr = value;
2203        Ok(())
2204    }
2205
2206    fn set_nullable_struct(
2207        &self,
2208        _ctx: impl WriteContext,
2209        value: Nullable<SimpleStruct<'_>>,
2210    ) -> Result<(), Error> {
2211        if let Some(s) = value.into_option() {
2212            let mut data = self.data.borrow_mut();
2213            let ns = SimpleStructOwned {
2214                a: s.a()?,
2215                b: s.b()?,
2216                c: s.c()?,
2217                d: s.d()?
2218                    .0
2219                    .try_into()
2220                    .map_err(|_| ErrorCode::ConstraintError)?,
2221                e: s.e()?.try_into().map_err(|_| ErrorCode::ConstraintError)?,
2222                f: s.f()?,
2223                g: s.g()?,
2224                h: s.h()?,
2225                i: s.i()?,
2226            };
2227
2228            data.nullable_struct = Nullable::some(ns);
2229        } else {
2230            self.data.borrow_mut().nullable_struct = Nullable::none();
2231        }
2232
2233        Ok(())
2234    }
2235
2236    fn set_nullable_range_restricted_int_8_u(
2237        &self,
2238        _ctx: impl WriteContext,
2239        value: Nullable<u8>,
2240    ) -> Result<(), Error> {
2241        if let Some(value) = value.into_option() {
2242            const RANGE: core::ops::RangeInclusive<u8> = 20..=100;
2243
2244            if RANGE.contains(&value) {
2245                self.data.borrow_mut().nullable_range_restricted_int_8_u = Nullable::some(value);
2246            } else {
2247                Err(ErrorCode::ConstraintError)?;
2248            }
2249        } else {
2250            self.data
2251                .borrow_mut()
2252                .nullable_range_restricted_int_8_u
2253                .clear();
2254        }
2255
2256        Ok(())
2257    }
2258
2259    fn set_nullable_range_restricted_int_8_s(
2260        &self,
2261        _ctx: impl WriteContext,
2262        value: Nullable<i8>,
2263    ) -> Result<(), Error> {
2264        if let Some(value) = value.into_option() {
2265            const RANGE: core::ops::RangeInclusive<i8> = -40..=50;
2266
2267            if RANGE.contains(&value) {
2268                self.data.borrow_mut().nullable_range_restricted_int_8_s = Nullable::some(value);
2269            } else {
2270                Err(ErrorCode::ConstraintError)?;
2271            }
2272        } else {
2273            self.data
2274                .borrow_mut()
2275                .nullable_range_restricted_int_8_s
2276                .clear();
2277        }
2278
2279        Ok(())
2280    }
2281
2282    fn set_nullable_range_restricted_int_16_u(
2283        &self,
2284        _ctx: impl WriteContext,
2285        value: Nullable<u16>,
2286    ) -> Result<(), Error> {
2287        if let Some(value) = value.into_option() {
2288            const RANGE: core::ops::RangeInclusive<u16> = 100..=1000;
2289
2290            if RANGE.contains(&value) {
2291                self.data.borrow_mut().nullable_range_restricted_int_16_u = Nullable::some(value);
2292            } else {
2293                Err(ErrorCode::ConstraintError)?;
2294            }
2295        } else {
2296            self.data
2297                .borrow_mut()
2298                .nullable_range_restricted_int_16_u
2299                .clear();
2300        }
2301
2302        Ok(())
2303    }
2304
2305    fn set_nullable_range_restricted_int_16_s(
2306        &self,
2307        _ctx: impl WriteContext,
2308        value: Nullable<i16>,
2309    ) -> Result<(), Error> {
2310        if let Some(value) = value.into_option() {
2311            const RANGE: core::ops::RangeInclusive<i16> = -150..=200;
2312
2313            if RANGE.contains(&value) {
2314                self.data.borrow_mut().nullable_range_restricted_int_16_s = Nullable::some(value);
2315            } else {
2316                Err(ErrorCode::ConstraintError)?;
2317            }
2318        } else {
2319            self.data
2320                .borrow_mut()
2321                .nullable_range_restricted_int_16_s
2322                .clear();
2323        }
2324
2325        Ok(())
2326    }
2327
2328    fn set_global_enum(&self, _ctx: impl WriteContext, value: TestGlobalEnum) -> Result<(), Error> {
2329        self.data.borrow_mut().global_enum = value;
2330        Ok(())
2331    }
2332
2333    fn set_global_struct(
2334        &self,
2335        _ctx: impl WriteContext,
2336        value: TestGlobalStruct<'_>,
2337    ) -> Result<(), Error> {
2338        self.data.borrow_mut().global_struct = TestGlobalStructOwned {
2339            name: value
2340                .name()?
2341                .try_into()
2342                .map_err(|_| ErrorCode::InvalidAction)?,
2343            my_bitmap: value.my_bitmap()?,
2344            my_enum: value.my_enum()?,
2345        };
2346        Ok(())
2347    }
2348
2349    fn set_nullable_global_enum(
2350        &self,
2351        _ctx: impl WriteContext,
2352        value: Nullable<TestGlobalEnum>,
2353    ) -> Result<(), Error> {
2354        self.data.borrow_mut().nullable_global_enum = value;
2355        Ok(())
2356    }
2357
2358    fn set_nullable_global_struct(
2359        &self,
2360        _ctx: impl WriteContext,
2361        value: Nullable<TestGlobalStruct<'_>>,
2362    ) -> Result<(), Error> {
2363        if let Some(s) = value.into_option() {
2364            let mut data = self.data.borrow_mut();
2365            let ns = TestGlobalStructOwned {
2366                name: s.name()?.try_into().map_err(|_| ErrorCode::InvalidAction)?,
2367                my_bitmap: s.my_bitmap()?,
2368                my_enum: s.my_enum()?,
2369            };
2370
2371            data.nullable_global_struct = Nullable::some(ns);
2372        } else {
2373            self.data.borrow_mut().nullable_global_struct = Nullable::none();
2374        }
2375
2376        Ok(())
2377    }
2378
2379    fn handle_test(&self, _ctx: impl InvokeContext) -> Result<(), Error> {
2380        Ok(())
2381    }
2382
2383    fn handle_test_not_handled(&self, _ctx: impl InvokeContext) -> Result<(), Error> {
2384        Err(ErrorCode::InvalidCommand.into())
2385    }
2386
2387    fn handle_test_specific<P: TLVBuilderParent>(
2388        &self,
2389        _ctx: impl InvokeContext,
2390        response: TestSpecificResponseBuilder<P>,
2391    ) -> Result<P, Error> {
2392        response.return_value(7)?.end()
2393    }
2394
2395    fn handle_test_add_arguments<P: TLVBuilderParent>(
2396        &self,
2397        _ctx: impl InvokeContext,
2398        request: TestAddArgumentsRequest<'_>,
2399        response: TestAddArgumentsResponseBuilder<P>,
2400    ) -> Result<P, Error> {
2401        let result = request.arg_1()? as u16 + request.arg_2()? as u16;
2402        if result <= u8::MAX as u16 {
2403            response.return_value(result as _)?.end()
2404        } else {
2405            Err(ErrorCode::InvalidCommand.into())
2406        }
2407    }
2408
2409    fn handle_test_simple_argument_request<P: TLVBuilderParent>(
2410        &self,
2411        _ctx: impl InvokeContext,
2412        request: TestSimpleArgumentRequestRequest<'_>,
2413        response: TestSimpleArgumentResponseBuilder<P>,
2414    ) -> Result<P, Error> {
2415        response.return_value(request.arg_1()? as _)?.end()
2416    }
2417
2418    fn handle_test_struct_array_argument_request<P: TLVBuilderParent>(
2419        &self,
2420        _ctx: impl InvokeContext,
2421        _request: TestStructArrayArgumentRequestRequest<'_>,
2422        _response: TestStructArrayArgumentResponseBuilder<P>,
2423    ) -> Result<P, Error> {
2424        unreachable!()
2425    }
2426
2427    fn handle_test_struct_argument_request<P: TLVBuilderParent>(
2428        &self,
2429        _ctx: impl InvokeContext,
2430        request: TestStructArgumentRequestRequest<'_>,
2431        response: BooleanResponseBuilder<P>,
2432    ) -> Result<P, Error> {
2433        let s = request.arg_1()?;
2434
2435        let result = s.a()? == 0
2436            && s.b()?
2437            && s.c()? == SimpleEnum::ValueB
2438            && s.d()?.0 == b"octet_string"
2439            && s.e()? == "char_string"
2440            && s.f()? == SimpleBitmap::VALUE_A
2441            && s.g()? == 0f32
2442            && s.h()? == 0f64;
2443
2444        response.value(result)?.end()
2445    }
2446
2447    fn handle_test_nested_struct_argument_request<P: TLVBuilderParent>(
2448        &self,
2449        _ctx: impl InvokeContext,
2450        request: TestNestedStructArgumentRequestRequest<'_>,
2451        response: BooleanResponseBuilder<P>,
2452    ) -> Result<P, Error> {
2453        let s = request.arg_1()?;
2454
2455        let result = s.a()? == 0 && s.b()? && {
2456            let s = s.c()?;
2457
2458            s.a()? == 0
2459                && s.b()?
2460                && s.c()? == SimpleEnum::ValueB
2461                && s.d()?.0 == b"octet_string"
2462                && s.e()? == "char_string"
2463                && s.f()? == SimpleBitmap::VALUE_A
2464                && s.g()? == 0f32
2465                && s.h()? == 0f64
2466        };
2467
2468        response.value(result)?.end()
2469    }
2470
2471    fn handle_test_list_struct_argument_request<P: TLVBuilderParent>(
2472        &self,
2473        _ctx: impl InvokeContext,
2474        request: TestListStructArgumentRequestRequest<'_>,
2475        response: BooleanResponseBuilder<P>,
2476    ) -> Result<P, Error> {
2477        let l = request.arg_1()?;
2478
2479        let mut result = l.iter().count() == 2;
2480
2481        if result {
2482            let mut iter = l.iter();
2483
2484            let s1 = unwrap!(iter.next())?;
2485            let s2 = unwrap!(iter.next())?;
2486
2487            result = s1.a()? == 0
2488                && s1.b()?
2489                && s1.c()? == SimpleEnum::ValueB
2490                && s1.d()?.0 == b"first_octet_string"
2491                && s1.e()? == "first_char_string"
2492                && s1.f()? == SimpleBitmap::VALUE_A
2493                && s1.g()? == 0f32
2494                && s1.h()? == 0f64
2495                && s2.a()? == 1
2496                && s2.b()?
2497                && s2.c()? == SimpleEnum::ValueC
2498                && s2.d()?.0 == b"second_octet_string"
2499                && s2.e()? == "second_char_string"
2500                && s2.f()? == SimpleBitmap::VALUE_A
2501                && s2.g()? == 0f32
2502                && s2.h()? == 0f64;
2503        }
2504
2505        response.value(result)?.end()
2506    }
2507
2508    fn handle_test_list_int_8_u_argument_request<P: TLVBuilderParent>(
2509        &self,
2510        _ctx: impl InvokeContext,
2511        request: TestListInt8UArgumentRequestRequest<'_>,
2512        response: BooleanResponseBuilder<P>,
2513    ) -> Result<P, Error> {
2514        let l = request.arg_1()?;
2515
2516        let result = l.iter().count() == 9 && {
2517            let mut result = true;
2518
2519            for (i, j) in l.iter().zip(1_u8..10) {
2520                result = result || i? == j;
2521            }
2522
2523            result
2524        };
2525
2526        response.value(result)?.end()
2527    }
2528
2529    fn handle_test_nested_struct_list_argument_request<P: TLVBuilderParent>(
2530        &self,
2531        _ctx: impl InvokeContext,
2532        request: TestNestedStructListArgumentRequestRequest<'_>,
2533        response: BooleanResponseBuilder<P>,
2534    ) -> Result<P, Error> {
2535        let s = request.arg_1()?;
2536
2537        let result = s.a()? == 0
2538            && s.b()?
2539            && {
2540                let ss = s.c()?;
2541
2542                ss.a()? == 0
2543                    && ss.b()?
2544                    && ss.c()? == SimpleEnum::ValueB
2545                    && ss.d()?.0 == b"octet_string"
2546                    && ss.e()? == "char_string"
2547                    && ss.f()? == SimpleBitmap::VALUE_A
2548                    && ss.g()? == 0f32
2549                    && ss.h()? == 0f64
2550            }
2551            && {
2552                let l = s.d()?;
2553
2554                l.iter().count() == 2 && {
2555                    let mut iter = l.iter();
2556                    let ls1 = unwrap!(iter.next())?;
2557                    let ls2 = unwrap!(iter.next())?;
2558
2559                    ls1.a()? == 1
2560                        && ls1.b()?
2561                        && ls1.c()? == SimpleEnum::ValueC
2562                        && ls1.d()?.0 == b"nested_octet_string"
2563                        && ls1.e()? == "nested_char_string"
2564                        && ls1.f()? == SimpleBitmap::VALUE_A
2565                        && ls1.g()? == 0f32
2566                        && ls1.h()? == 0f64
2567                        && ls2.a()? == 2
2568                        && ls2.b()?
2569                        && ls2.c()? == SimpleEnum::ValueC
2570                        && ls2.d()?.0 == b"nested_octet_string"
2571                        && ls2.e()? == "nested_char_string"
2572                        && ls2.f()? == SimpleBitmap::VALUE_A
2573                        && ls2.g()? == 0f32
2574                        && ls2.h()? == 0f64
2575                }
2576            };
2577
2578        response.value(result)?.end()
2579    }
2580
2581    fn handle_test_list_nested_struct_list_argument_request<P: TLVBuilderParent>(
2582        &self,
2583        _ctx: impl InvokeContext,
2584        request: TestListNestedStructListArgumentRequestRequest<'_>,
2585        response: BooleanResponseBuilder<P>,
2586    ) -> Result<P, Error> {
2587        let l = request.arg_1()?;
2588
2589        let result = l.iter().count() == 1 && {
2590            let s = unwrap!(l.iter().next())?;
2591
2592            s.a()? == 0
2593                && s.b()?
2594                && {
2595                    let c = s.c()?;
2596
2597                    c.a()? == 0
2598                        && c.b()?
2599                        && c.c()? == SimpleEnum::ValueB
2600                        && c.d()?.0 == b"octet_string"
2601                        && c.e()? == "char_string"
2602                        && c.f()? == SimpleBitmap::VALUE_A
2603                        && c.g()? == 0f32
2604                        && c.h()? == 0f64
2605                }
2606                && {
2607                    let d = s.d()?;
2608
2609                    d.iter().count() == 2 && {
2610                        let mut iter = d.iter();
2611                        let ls1 = unwrap!(iter.next())?;
2612                        let ls2 = unwrap!(iter.next())?;
2613
2614                        ls1.a()? == 1
2615                            && ls1.b()?
2616                            && ls1.c()? == SimpleEnum::ValueC
2617                            && ls1.d()?.0 == b"nested_octet_string"
2618                            && ls1.e()? == "nested_char_string"
2619                            && ls1.f()? == SimpleBitmap::VALUE_A
2620                            && ls1.g()? == 0f32
2621                            && ls1.h()? == 0f64
2622                            && ls2.a()? == 2
2623                            && ls2.b()?
2624                            && ls2.c()? == SimpleEnum::ValueC
2625                            && ls2.d()?.0 == b"nested_octet_string"
2626                            && ls2.e()? == "nested_char_string"
2627                            && ls2.f()? == SimpleBitmap::VALUE_A
2628                            && ls2.g()? == 0f32
2629                            && ls2.h()? == 0f64
2630                    }
2631                }
2632                && {
2633                    let e = s.e()?;
2634
2635                    e.iter().count() == 3 && {
2636                        let mut result = true;
2637
2638                        for (i, j) in e.iter().zip(1_u32..4) {
2639                            result = result || i? == j;
2640                        }
2641
2642                        result
2643                    }
2644                }
2645                && {
2646                    let f = s.f()?;
2647
2648                    f.iter().count() == 3 && {
2649                        const STRS: &[&[u8]] =
2650                            &[b"octet_string_1", b"octect_string_2", b"octet_string_3"];
2651
2652                        let mut result = true;
2653
2654                        for (i, j) in f.iter().zip(STRS.iter()) {
2655                            result = result || i?.0 == *j;
2656                        }
2657
2658                        result
2659                    }
2660                }
2661                && {
2662                    let g = s.g()?;
2663
2664                    g.iter().count() == 2 && {
2665                        let mut result = true;
2666
2667                        for (i, j) in g.iter().zip([0u8, 255]) {
2668                            result = result || i? == j;
2669                        }
2670
2671                        result
2672                    }
2673                }
2674        };
2675
2676        response.value(result)?.end()
2677    }
2678
2679    fn handle_test_list_int_8_u_reverse_request<P: TLVBuilderParent>(
2680        &self,
2681        _ctx: impl InvokeContext,
2682        request: TestListInt8UReverseRequestRequest<'_>,
2683        response: TestListInt8UReverseResponseBuilder<P>,
2684    ) -> Result<P, Error> {
2685        // Iterating TLV containers backwards is not possible,
2686        // so for once here we'll on-stack allocate a temp buffer
2687
2688        let l = request.arg_1()?;
2689        let mut tmp = heapless::Vec::<u8, 16>::new();
2690        for i in l.iter() {
2691            unwrap!(tmp.push(i?));
2692        }
2693
2694        let mut lo = response.arg_1()?;
2695        for i in tmp.iter().rev() {
2696            lo = lo.push(i)?;
2697        }
2698
2699        lo.end()?.end()
2700    }
2701
2702    fn handle_test_enums_request<P: TLVBuilderParent>(
2703        &self,
2704        _ctx: impl InvokeContext,
2705        request: TestEnumsRequestRequest<'_>,
2706        response: TestEnumsResponseBuilder<P>,
2707    ) -> Result<P, Error> {
2708        response
2709            .arg_1(request.arg_1()?)?
2710            .arg_2(request.arg_2()?)?
2711            .end()
2712    }
2713
2714    fn handle_test_nullable_optional_request<P: TLVBuilderParent>(
2715        &self,
2716        _ctx: impl InvokeContext,
2717        request: TestNullableOptionalRequestRequest<'_>,
2718        response: TestNullableOptionalResponseBuilder<P>,
2719    ) -> Result<P, Error> {
2720        response
2721            .was_present(request.arg_1()?.is_some())?
2722            .was_null(request.arg_1()?.as_ref().map(Nullable::is_none))?
2723            .value(request.arg_1()?.and_then(|value| value.into_option()))?
2724            .original_value(request.arg_1()?)?
2725            .end()
2726    }
2727
2728    fn handle_test_complex_nullable_optional_request<P: TLVBuilderParent>(
2729        &self,
2730        _ctx: impl InvokeContext,
2731        request: TestComplexNullableOptionalRequestRequest<'_>,
2732        response: TestComplexNullableOptionalResponseBuilder<P>,
2733    ) -> Result<P, Error> {
2734        response
2735            .nullable_int_was_null(request.nullable_int()?.is_none())?
2736            .nullable_int_value(request.nullable_int()?.into_option())?
2737            .optional_int_was_present(request.optional_int()?.is_some())?
2738            .optional_int_value(request.optional_int()?)?
2739            .nullable_optional_int_was_present(request.nullable_optional_int()?.is_some())?
2740            .nullable_optional_int_was_null(
2741                request
2742                    .nullable_optional_int()?
2743                    .as_ref()
2744                    .map(Nullable::is_none),
2745            )?
2746            .nullable_optional_int_value(
2747                request
2748                    .nullable_optional_int()?
2749                    .and_then(Nullable::into_option),
2750            )?
2751            .nullable_string_was_null(request.nullable_string()?.is_none())?
2752            .nullable_string_value(request.nullable_string()?.into_option())?
2753            .optional_string_was_present(request.optional_string()?.is_some())?
2754            .optional_string_value(request.optional_string()?)?
2755            .nullable_optional_string_was_present(request.nullable_optional_string()?.is_some())?
2756            .nullable_optional_string_was_null(
2757                request
2758                    .nullable_optional_string()?
2759                    .as_ref()
2760                    .map(Nullable::is_none),
2761            )?
2762            .nullable_optional_string_value(
2763                request
2764                    .nullable_optional_string()?
2765                    .and_then(Nullable::into_option),
2766            )?
2767            .nullable_struct_was_null(request.nullable_struct()?.is_none())?
2768            .nullable_struct_value()?
2769            .with_some(request.nullable_struct()?.into_option(), |i, o| {
2770                o.a(i.a()?)?
2771                    .b(i.b()?)?
2772                    .c(i.c()?)?
2773                    .d(i.d()?)?
2774                    .e(i.e()?)?
2775                    .f(i.f()?)?
2776                    .g(i.g()?)?
2777                    .h(i.h()?)?
2778                    .i(i.i()?)?
2779                    .end()
2780            })?
2781            .optional_struct_was_present(request.optional_struct()?.is_some())?
2782            .optional_struct_value()?
2783            .with_some(request.optional_struct()?, |i, o| {
2784                o.a(i.a()?)?
2785                    .b(i.b()?)?
2786                    .c(i.c()?)?
2787                    .d(i.d()?)?
2788                    .e(i.e()?)?
2789                    .f(i.f()?)?
2790                    .g(i.g()?)?
2791                    .h(i.h()?)?
2792                    .i(i.i()?)?
2793                    .end()
2794            })?
2795            .nullable_optional_struct_was_present(request.nullable_optional_struct()?.is_some())?
2796            .nullable_optional_struct_was_null(
2797                request
2798                    .nullable_optional_struct()?
2799                    .as_ref()
2800                    .map(Nullable::is_none),
2801            )?
2802            .nullable_optional_struct_value()?
2803            .with_some(
2804                request
2805                    .nullable_optional_struct()?
2806                    .and_then(Nullable::into_option),
2807                |i, o| {
2808                    o.a(i.a()?)?
2809                        .b(i.b()?)?
2810                        .c(i.c()?)?
2811                        .d(i.d()?)?
2812                        .e(i.e()?)?
2813                        .f(i.f()?)?
2814                        .g(i.g()?)?
2815                        .h(i.h()?)?
2816                        .i(i.i()?)?
2817                        .end()
2818                },
2819            )?
2820            .nullable_list_was_null(request.nullable_list()?.is_none())?
2821            .nullable_list_value()?
2822            .with_some(request.nullable_list()?.as_opt_ref(), |i, mut o| {
2823                for i in i.iter() {
2824                    o = o.push(&i?)?;
2825                }
2826
2827                o.end()
2828            })?
2829            .optional_list_was_present(request.optional_list()?.is_some())?
2830            .optional_list_value()?
2831            .with_some(request.optional_list()?, |i, mut o| {
2832                for i in i.iter() {
2833                    o = o.push(&i?)?;
2834                }
2835
2836                o.end()
2837            })?
2838            .nullable_optional_list_was_present(request.nullable_optional_list()?.is_some())?
2839            .nullable_optional_list_was_null(
2840                request
2841                    .nullable_optional_list()?
2842                    .as_ref()
2843                    .map(Nullable::is_none),
2844            )?
2845            .nullable_optional_list_value()?
2846            .with_some(
2847                request
2848                    .nullable_optional_list()?
2849                    .and_then(Nullable::into_option),
2850                |i, mut o| {
2851                    for i in i.iter() {
2852                        o = o.push(&i?)?;
2853                    }
2854
2855                    o.end()
2856                },
2857            )?
2858            .end()
2859    }
2860
2861    fn handle_simple_struct_echo_request<P: TLVBuilderParent>(
2862        &self,
2863        _ctx: impl InvokeContext,
2864        request: SimpleStructEchoRequestRequest<'_>,
2865        response: SimpleStructResponseBuilder<P>,
2866    ) -> Result<P, Error> {
2867        let s = request.arg_1()?;
2868
2869        response
2870            .arg_1()?
2871            .a(s.a()?)?
2872            .b(s.b()?)?
2873            .c(s.c()?)?
2874            .d(s.d()?)?
2875            .e(s.e()?)?
2876            .f(s.f()?)?
2877            .g(s.g()?)?
2878            .h(s.h()?)?
2879            .i(s.i()?)?
2880            .end()?
2881            .end()
2882    }
2883
2884    fn handle_timed_invoke_request(&self, _ctx: impl InvokeContext) -> Result<(), Error> {
2885        Ok(())
2886    }
2887
2888    fn handle_test_simple_optional_argument_request(
2889        &self,
2890        _ctx: impl InvokeContext,
2891        request: TestSimpleOptionalArgumentRequestRequest<'_>,
2892    ) -> Result<(), Error> {
2893        if request.arg_1()?.is_some() {
2894            Ok(())
2895        } else {
2896            Err(ErrorCode::ConstraintError.into())
2897        }
2898    }
2899
2900    fn handle_test_emit_test_event_request<P: TLVBuilderParent>(
2901        &self,
2902        ctx: impl InvokeContext,
2903        request: TestEmitTestEventRequestRequest<'_>,
2904        response: TestEmitTestEventResponseBuilder<P>,
2905    ) -> Result<P, Error> {
2906        let event_no = TestEvent::emit(&ctx, |tw| {
2907            tw.arg_1(request.arg_1()?)?
2908                .arg_2(request.arg_2()?)?
2909                .arg_3(request.arg_3()?)?
2910                // NOTE: Why are the remaining TestEvent fields not marked as optional in the IDL?
2911                .arg_4()?
2912                .a(0)?
2913                .b(false)?
2914                .c(SimpleEnum::ValueA)?
2915                .d(Octets(&[]))?
2916                .e("")?
2917                .f(SimpleBitmap::empty())?
2918                .g(0.0)?
2919                .h(0.0)?
2920                .i(None)?
2921                .end()?
2922                .arg_5()?
2923                .end()?
2924                .arg_6()?
2925                .end()?
2926                .end()
2927        })?;
2928
2929        response.value(event_no)?.end()
2930    }
2931
2932    fn handle_test_emit_test_fabric_scoped_event_request<P: TLVBuilderParent>(
2933        &self,
2934        _ctx: impl InvokeContext,
2935        _request: TestEmitTestFabricScopedEventRequestRequest<'_>,
2936        _response: TestEmitTestFabricScopedEventResponseBuilder<P>,
2937    ) -> Result<P, Error> {
2938        todo!()
2939    }
2940
2941    fn handle_test_unknown_command(&self, _ctx: impl InvokeContext) -> Result<(), Error> {
2942        unreachable!()
2943    }
2944
2945    fn handle_test_batch_helper_request<P: TLVBuilderParent>(
2946        &self,
2947        _ctx: impl InvokeContext,
2948        request: TestBatchHelperRequestRequest<'_>,
2949        response: TestBatchHelperResponseBuilder<P>,
2950    ) -> Result<P, Error> {
2951        // Demonstrates how to skip the builder framework and write raw, unchecked TLV
2952        let byte = request.fill_character()?;
2953        let len = request.size_of_response_buffer()? as _;
2954
2955        let mut parent = response.unchecked_into_parent();
2956
2957        let writer = parent.writer();
2958
2959        writer.stri(
2960            &TLVTag::Context(TestBatchHelperResponseTag::Buffer as _),
2961            len,
2962            core::iter::repeat_n(byte, len),
2963        )?;
2964        writer.end_container()?; // TestBatchHelperResponse struct
2965
2966        Ok(parent)
2967    }
2968
2969    fn handle_test_second_batch_helper_request<P: TLVBuilderParent>(
2970        &self,
2971        _ctx: impl InvokeContext,
2972        request: TestSecondBatchHelperRequestRequest<'_>,
2973        response: TestBatchHelperResponseBuilder<P>,
2974    ) -> Result<P, Error> {
2975        // Demonstrates how to skip the builder framework and write raw, unchecked TLV
2976        let byte = request.fill_character()?;
2977        let len = request.size_of_response_buffer()? as _;
2978
2979        let mut parent = response.unchecked_into_parent();
2980
2981        let writer = parent.writer();
2982
2983        writer.stri(
2984            &TLVTag::Context(TestBatchHelperResponseTag::Buffer as _),
2985            len,
2986            core::iter::repeat_n(byte, len),
2987        )?;
2988        writer.end_container()?; // TestBatchHelperResponse struct
2989
2990        Ok(parent)
2991    }
2992
2993    fn mei_int_8_u(&self, _ctx: impl ReadContext) -> Result<u8, Error> {
2994        Ok(self.data.borrow().mei_int_8_u)
2995    }
2996
2997    fn set_mei_int_8_u(&self, _ctx: impl WriteContext, value: u8) -> Result<(), Error> {
2998        self.data.borrow_mut().mei_int_8_u = value;
2999
3000        Ok(())
3001    }
3002
3003    fn handle_test_different_vendor_mei_request<P: TLVBuilderParent>(
3004        &self,
3005        ctx: impl InvokeContext,
3006        request: TestDifferentVendorMeiRequestRequest<'_>,
3007        response: TestDifferentVendorMeiResponseBuilder<P>,
3008    ) -> Result<P, Error> {
3009        let arg = request.arg_1()?;
3010
3011        // Per the cluster spec, this command echoes `arg1` and emits a
3012        // `TestDifferentVendorMeiEvent` whose `arg1` carries the same value;
3013        // the response then surfaces the event's number for the caller to
3014        // read back via `readEvent`. `TestUnitTestingClusterMei` step 7/8
3015        // depend on the event being emitted with a non-zero (real) number.
3016        let event_no = TestDifferentVendorMeiEvent::emit(&ctx, |tw| tw.arg_1(arg)?.end())?;
3017
3018        response.arg_1(arg)?.event_number(event_no)?.end()
3019    }
3020
3021    fn handle_string_echo_request<P: TLVBuilderParent>(
3022        &self,
3023        _ctx: impl InvokeContext,
3024        request: StringEchoRequestRequest<'_>,
3025        response: StringEchoResponseBuilder<P>,
3026    ) -> Result<P, Error> {
3027        response.payload(request.payload()?)?.end()
3028    }
3029
3030    fn handle_global_echo_request<P: TLVBuilderParent>(
3031        &self,
3032        _ctx: impl InvokeContext,
3033        request: GlobalEchoRequestRequest<'_>,
3034        response: GlobalEchoResponseBuilder<P>,
3035    ) -> Result<P, Error> {
3036        let s = request.field_1()?;
3037        response
3038            .field_1()?
3039            .name(s.name()?)?
3040            .my_bitmap(s.my_bitmap()?)?
3041            .my_enum(s.my_enum()?)?
3042            .end()?
3043            .field_2(request.field_2()?)?
3044            .end()
3045    }
3046
3047    fn handle_test_check_command_flags(&self, _ctx: impl InvokeContext) -> Result<(), Error> {
3048        todo!()
3049    }
3050}