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