hydrate_data/
field_wrappers.rs

1use crate::data_set_view::DataContainer;
2use crate::value::ValueEnum;
3use crate::{
4    AssetId, DataContainerRef, DataContainerRefMut, DataSetError, DataSetResult, NullOverride,
5    SchemaSet, SingleObject, Value,
6};
7use hydrate_schema::PropertyPath;
8use std::cell::RefCell;
9use std::marker::PhantomData;
10use std::ops::{Deref, DerefMut};
11use std::rc::Rc;
12use std::sync::Arc;
13use uuid::Uuid;
14
15pub trait FieldAccessor {
16    fn new(property_path: PropertyPath) -> Self;
17}
18
19pub trait FieldRef<'a> {
20    fn new(
21        property_path: PropertyPath,
22        data_container: DataContainerRef<'a>,
23    ) -> Self;
24}
25
26pub trait FieldRefMut<'a> {
27    fn new(
28        property_path: PropertyPath,
29        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
30    ) -> Self;
31}
32
33pub trait Field {
34    fn new(
35        property_path: PropertyPath,
36        data_container: &Rc<RefCell<Option<DataContainer>>>,
37    ) -> Self;
38}
39
40pub trait Enum: Sized {
41    fn to_symbol_name(&self) -> &'static str;
42    fn from_symbol_name(str: &str) -> Option<Self>;
43}
44
45pub trait RecordAccessor {
46    fn schema_name() -> &'static str;
47
48    fn new_single_object(schema_set: &SchemaSet) -> DataSetResult<SingleObject> {
49        let schema = schema_set
50            .find_named_type(Self::schema_name())
51            .unwrap()
52            .as_record()?;
53
54        Ok(SingleObject::new(schema))
55    }
56}
57
58pub trait RecordRef {
59    fn schema_name() -> &'static str;
60
61    //fn new(property_path: PropertyPath, data_container: DataContainerRef) -> Self;
62}
63
64pub trait RecordRefMut {
65    fn schema_name() -> &'static str;
66}
67
68pub trait Record: Sized + Field {
69    type Accessor: RecordAccessor + FieldAccessor;
70    type Reader<'a>: RecordRef + FieldRef<'a>;
71    type Writer<'a>: RecordRefMut + FieldRefMut<'a>;
72
73    fn schema_name() -> &'static str;
74
75    fn new_single_object(schema_set: &SchemaSet) -> DataSetResult<SingleObject> {
76        let schema = schema_set
77            .find_named_type(Self::schema_name())?
78            .as_record()?;
79
80        Ok(SingleObject::new(schema))
81    }
82
83    fn new_builder(schema_set: &SchemaSet) -> RecordBuilder<Self> {
84        RecordBuilder::new(schema_set)
85    }
86}
87
88pub struct RecordBuilder<T: Record + Field>(Rc<RefCell<Option<DataContainer>>>, T, PhantomData<T>);
89
90impl<T: Record + Field> RecordBuilder<T> {
91    pub fn new(schema_set: &SchemaSet) -> Self {
92        let single_object = T::new_single_object(schema_set).unwrap();
93        let data_container = DataContainer::from_single_object(single_object, schema_set.clone());
94        let data_container = Rc::new(RefCell::new(Some(data_container)));
95        let owned = T::new(Default::default(), &data_container);
96        Self(data_container, owned, Default::default())
97    }
98
99    pub fn into_inner(self) -> DataSetResult<SingleObject> {
100        // We are unwrapping an Rc, the RefCell, Option, and the DataContainer
101        Ok(self
102            .0
103            .borrow_mut()
104            .take()
105            .ok_or(DataSetError::DataTaken)?
106            .into_inner())
107    }
108}
109
110impl<T: Record + Field> Deref for RecordBuilder<T> {
111    type Target = T;
112
113    fn deref(&self) -> &Self::Target {
114        &self.1
115    }
116}
117
118impl<T: Record + Field> DerefMut for RecordBuilder<T> {
119    fn deref_mut(&mut self) -> &mut Self::Target {
120        &mut self.1
121    }
122}
123
124pub struct EnumFieldAccessor<T: Enum>(PropertyPath, PhantomData<T>);
125
126impl<T: Enum> FieldAccessor for EnumFieldAccessor<T> {
127    fn new(property_path: PropertyPath) -> Self {
128        EnumFieldAccessor(property_path, PhantomData::default())
129    }
130}
131
132impl<T: Enum> EnumFieldAccessor<T> {
133    pub fn do_get(
134        property_path: &PropertyPath,
135        data_container: DataContainerRef,
136    ) -> DataSetResult<T> {
137        let e = data_container.resolve_property(property_path.path())?;
138        Ok(T::from_symbol_name(e.as_enum().unwrap().symbol_name())
139            .ok_or(DataSetError::UnexpectedEnumSymbol)?)
140    }
141
142    pub fn do_set(
143        property_path: &PropertyPath,
144        data_container: &mut DataContainerRefMut,
145        value: T,
146    ) -> DataSetResult<Option<Value>> {
147        data_container.set_property_override(
148            property_path.path(),
149            Some(Value::Enum(ValueEnum::new(
150                value.to_symbol_name().to_string(),
151            ))),
152        )
153    }
154
155    pub fn get(
156        &self,
157        data_container: DataContainerRef,
158    ) -> DataSetResult<T> {
159        Self::do_get(&self.0, data_container)
160    }
161
162    pub fn set(
163        &self,
164        data_container: &mut DataContainerRefMut,
165        value: T,
166    ) -> DataSetResult<Option<Value>> {
167        Self::do_set(&self.0, data_container, value)
168    }
169}
170
171pub struct EnumFieldRef<'a, T>(pub PropertyPath, DataContainerRef<'a>, PhantomData<T>);
172
173impl<'a, T: Enum> FieldRef<'a> for EnumFieldRef<'a, T> {
174    fn new(
175        property_path: PropertyPath,
176        data_container: DataContainerRef<'a>,
177    ) -> Self {
178        EnumFieldRef(property_path, data_container, PhantomData)
179    }
180}
181
182impl<'a, T: Enum> EnumFieldRef<'a, T> {
183    pub fn get(&self) -> DataSetResult<T> {
184        EnumFieldAccessor::<T>::do_get(&self.0, self.1.clone())
185    }
186}
187
188pub struct EnumFieldRefMut<'a, T: Enum>(
189    pub PropertyPath,
190    Rc<RefCell<DataContainerRefMut<'a>>>,
191    PhantomData<T>,
192);
193
194impl<'a, T: Enum> FieldRefMut<'a> for EnumFieldRefMut<'a, T> {
195    fn new(
196        property_path: PropertyPath,
197        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
198    ) -> Self {
199        EnumFieldRefMut(property_path, data_container.clone(), PhantomData)
200    }
201}
202
203impl<'a, T: Enum> EnumFieldRefMut<'a, T> {
204    pub fn get(&self) -> DataSetResult<T> {
205        EnumFieldAccessor::<T>::do_get(&self.0, self.1.borrow().read())
206    }
207
208    pub fn set(
209        &self,
210        value: T,
211    ) -> DataSetResult<Option<Value>> {
212        EnumFieldAccessor::<T>::do_set(&self.0, &mut *self.1.borrow_mut(), value)
213    }
214}
215
216pub struct EnumField<T: Enum>(
217    pub PropertyPath,
218    Rc<RefCell<Option<DataContainer>>>,
219    PhantomData<T>,
220);
221
222impl<T: Enum> Field for EnumField<T> {
223    fn new(
224        property_path: PropertyPath,
225        data_container: &Rc<RefCell<Option<DataContainer>>>,
226    ) -> Self {
227        EnumField(property_path, data_container.clone(), PhantomData)
228    }
229}
230
231impl<T: Enum> EnumField<T> {
232    pub fn get(&self) -> DataSetResult<T> {
233        EnumFieldAccessor::<T>::do_get(
234            &self.0,
235            self.1
236                .borrow()
237                .as_ref()
238                .ok_or(DataSetError::DataTaken)?
239                .read(),
240        )
241    }
242
243    pub fn set(
244        &self,
245        value: T,
246    ) -> DataSetResult<Option<Value>> {
247        EnumFieldAccessor::<T>::do_set(
248            &self.0,
249            &mut self
250                .1
251                .borrow_mut()
252                .as_mut()
253                .ok_or(DataSetError::DataTaken)?
254                .to_mut(),
255            value,
256        )
257    }
258}
259
260pub struct NullableFieldAccessor<T: FieldAccessor>(pub PropertyPath, PhantomData<T>);
261
262impl<T: FieldAccessor> FieldAccessor for NullableFieldAccessor<T> {
263    fn new(property_path: PropertyPath) -> Self {
264        NullableFieldAccessor(property_path, PhantomData::default())
265    }
266}
267
268impl<T: FieldAccessor> NullableFieldAccessor<T> {
269    pub fn resolve_null(
270        &self,
271        data_container: DataContainerRef,
272    ) -> DataSetResult<Option<T>> {
273        if self.resolve_null_override(data_container)? == NullOverride::SetNonNull {
274            Ok(Some(T::new(self.0.push("value"))))
275        } else {
276            Ok(None)
277        }
278    }
279
280    pub fn resolve_null_override(
281        &self,
282        data_container: DataContainerRef,
283    ) -> DataSetResult<NullOverride> {
284        data_container.resolve_null_override(self.0.path())
285    }
286
287    pub fn set_null_override(
288        &self,
289        data_container: &mut DataContainerRefMut,
290        null_override: NullOverride,
291    ) -> DataSetResult<Option<T>> {
292        let path = self.0.path();
293        data_container.set_null_override(path, null_override)?;
294        if data_container.resolve_null_override(path)? == NullOverride::SetNonNull {
295            Ok(Some(T::new(self.0.push("value"))))
296        } else {
297            Ok(None)
298        }
299    }
300}
301
302pub struct NullableFieldRef<'a, T>(pub PropertyPath, DataContainerRef<'a>, PhantomData<T>);
303
304impl<'a, T: FieldRef<'a>> FieldRef<'a> for NullableFieldRef<'a, T> {
305    fn new(
306        property_path: PropertyPath,
307        data_container: DataContainerRef<'a>,
308    ) -> Self {
309        NullableFieldRef(property_path, data_container, PhantomData)
310    }
311}
312
313impl<'a, T: FieldRef<'a>> NullableFieldRef<'a, T> {
314    pub fn resolve_null(&self) -> DataSetResult<Option<T>> {
315        if self.resolve_null_override()? == NullOverride::SetNonNull {
316            Ok(Some(T::new(self.0.push("value"), self.1.clone())))
317        } else {
318            Ok(None)
319        }
320    }
321
322    pub fn resolve_null_override(&self) -> DataSetResult<NullOverride> {
323        self.1.resolve_null_override(self.0.path())
324    }
325}
326
327pub struct NullableFieldRefMut<'a, T: FieldRefMut<'a>>(
328    pub PropertyPath,
329    Rc<RefCell<DataContainerRefMut<'a>>>,
330    PhantomData<T>,
331);
332
333impl<'a, T: FieldRefMut<'a>> FieldRefMut<'a> for NullableFieldRefMut<'a, T> {
334    fn new(
335        property_path: PropertyPath,
336        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
337    ) -> Self {
338        NullableFieldRefMut(property_path, data_container.clone(), PhantomData)
339    }
340}
341
342impl<'a, T: FieldRefMut<'a>> NullableFieldRefMut<'a, T> {
343    pub fn resolve_null(&'a self) -> DataSetResult<Option<T>> {
344        if self.resolve_null_override()? == NullOverride::SetNonNull {
345            Ok(Some(T::new(self.0.push("value"), &self.1)))
346        } else {
347            Ok(None)
348        }
349    }
350
351    pub fn resolve_null_override(&self) -> DataSetResult<NullOverride> {
352        self.1.borrow_mut().resolve_null_override(self.0.path())
353    }
354
355    pub fn set_null_override(
356        &'a self,
357        null_override: NullOverride,
358    ) -> DataSetResult<Option<T>> {
359        let path = self.0.path();
360        self.1.borrow_mut().set_null_override(path, null_override)?;
361        if self.1.borrow_mut().resolve_null_override(path)? == NullOverride::SetNonNull {
362            Ok(Some(T::new(self.0.push("value"), &self.1)))
363        } else {
364            Ok(None)
365        }
366    }
367}
368
369pub struct NullableField<T: Field>(
370    pub PropertyPath,
371    Rc<RefCell<Option<DataContainer>>>,
372    PhantomData<T>,
373);
374
375impl<T: Field> Field for NullableField<T> {
376    fn new(
377        property_path: PropertyPath,
378        data_container: &Rc<RefCell<Option<DataContainer>>>,
379    ) -> Self {
380        NullableField(property_path, data_container.clone(), PhantomData)
381    }
382}
383
384impl<T: Field> NullableField<T> {
385    pub fn resolve_null(self) -> DataSetResult<Option<T>> {
386        if self.resolve_null_override()? == NullOverride::SetNonNull {
387            Ok(Some(T::new(self.0.push("value"), &self.1)))
388        } else {
389            Ok(None)
390        }
391    }
392
393    pub fn resolve_null_override(&self) -> DataSetResult<NullOverride> {
394        self.1
395            .borrow_mut()
396            .as_ref()
397            .ok_or(DataSetError::DataTaken)?
398            .resolve_null_override(self.0.path())
399    }
400
401    pub fn set_null_override(
402        &self,
403        null_override: NullOverride,
404    ) -> DataSetResult<Option<T>> {
405        let path = self.0.path();
406        self.1
407            .borrow_mut()
408            .as_mut()
409            .ok_or(DataSetError::DataTaken)?
410            .set_null_override(path, null_override)?;
411        if self
412            .1
413            .borrow_mut()
414            .as_mut()
415            .ok_or(DataSetError::DataTaken)?
416            .resolve_null_override(path)?
417            == NullOverride::SetNonNull
418        {
419            Ok(Some(T::new(self.0.push("value"), &self.1)))
420        } else {
421            Ok(None)
422        }
423    }
424}
425
426pub struct BooleanFieldAccessor(pub PropertyPath);
427
428impl FieldAccessor for BooleanFieldAccessor {
429    fn new(property_path: PropertyPath) -> Self {
430        BooleanFieldAccessor(property_path)
431    }
432}
433
434impl BooleanFieldAccessor {
435    fn do_get(
436        property_path: &PropertyPath,
437        data_container: DataContainerRef,
438    ) -> DataSetResult<bool> {
439        Ok(data_container
440            .resolve_property(property_path.path())?
441            .as_boolean()
442            .unwrap())
443    }
444
445    fn do_set(
446        property_path: &PropertyPath,
447        data_container: &mut DataContainerRefMut,
448        value: bool,
449    ) -> DataSetResult<Option<Value>> {
450        data_container.set_property_override(property_path.path(), Some(Value::Boolean(value)))
451    }
452
453    pub fn get(
454        &self,
455        data_container: DataContainerRef,
456    ) -> DataSetResult<bool> {
457        Self::do_get(&self.0, data_container)
458    }
459
460    pub fn set(
461        &self,
462        data_container: &mut DataContainerRefMut,
463        value: bool,
464    ) -> DataSetResult<Option<Value>> {
465        Self::do_set(&self.0, data_container, value)
466    }
467}
468
469pub struct BooleanFieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
470
471impl<'a> FieldRef<'a> for BooleanFieldRef<'a> {
472    fn new(
473        property_path: PropertyPath,
474        data_container: DataContainerRef<'a>,
475    ) -> Self {
476        BooleanFieldRef(property_path, data_container)
477    }
478}
479
480impl<'a> BooleanFieldRef<'a> {
481    pub fn get(&self) -> DataSetResult<bool> {
482        BooleanFieldAccessor::do_get(&self.0, self.1.clone())
483    }
484}
485
486pub struct BooleanFieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
487
488impl<'a> FieldRefMut<'a> for BooleanFieldRefMut<'a> {
489    fn new(
490        property_path: PropertyPath,
491        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
492    ) -> Self {
493        BooleanFieldRefMut(property_path, data_container.clone())
494    }
495}
496
497impl<'a> BooleanFieldRefMut<'a> {
498    pub fn get(&self) -> DataSetResult<bool> {
499        BooleanFieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
500    }
501
502    pub fn set(
503        &self,
504        value: bool,
505    ) -> DataSetResult<Option<Value>> {
506        BooleanFieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
507    }
508}
509
510pub struct BooleanField(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
511
512impl Field for BooleanField {
513    fn new(
514        property_path: PropertyPath,
515        data_container: &Rc<RefCell<Option<DataContainer>>>,
516    ) -> Self {
517        BooleanField(property_path, data_container.clone())
518    }
519}
520
521impl BooleanField {
522    pub fn get(&self) -> DataSetResult<bool> {
523        BooleanFieldAccessor::do_get(
524            &self.0,
525            self.1
526                .borrow_mut()
527                .as_mut()
528                .ok_or(DataSetError::DataTaken)?
529                .read(),
530        )
531    }
532
533    pub fn set(
534        &self,
535        value: bool,
536    ) -> DataSetResult<Option<Value>> {
537        BooleanFieldAccessor::do_set(
538            &self.0,
539            &mut self
540                .1
541                .borrow_mut()
542                .as_mut()
543                .ok_or(DataSetError::DataTaken)?
544                .to_mut(),
545            value,
546        )
547    }
548}
549
550pub struct I32FieldAccessor(pub PropertyPath);
551
552impl FieldAccessor for I32FieldAccessor {
553    fn new(property_path: PropertyPath) -> Self {
554        I32FieldAccessor(property_path)
555    }
556}
557
558impl I32FieldAccessor {
559    fn do_get(
560        property_path: &PropertyPath,
561        data_container: DataContainerRef,
562    ) -> DataSetResult<i32> {
563        Ok(data_container
564            .resolve_property(property_path.path())?
565            .as_i32()
566            .unwrap())
567    }
568
569    fn do_set(
570        property_path: &PropertyPath,
571        data_container: &mut DataContainerRefMut,
572        value: i32,
573    ) -> DataSetResult<Option<Value>> {
574        data_container.set_property_override(property_path.path(), Some(Value::I32(value)))
575    }
576
577    pub fn get(
578        &self,
579        data_container: DataContainerRef,
580    ) -> DataSetResult<i32> {
581        Self::do_get(&self.0, data_container)
582    }
583
584    pub fn set(
585        &self,
586        data_container: &mut DataContainerRefMut,
587        value: i32,
588    ) -> DataSetResult<Option<Value>> {
589        Self::do_set(&self.0, data_container, value)
590    }
591}
592
593pub struct I32FieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
594
595impl<'a> FieldRef<'a> for I32FieldRef<'a> {
596    fn new(
597        property_path: PropertyPath,
598        data_container: DataContainerRef<'a>,
599    ) -> Self {
600        I32FieldRef(property_path, data_container)
601    }
602}
603
604impl<'a> I32FieldRef<'a> {
605    pub fn get(&self) -> DataSetResult<i32> {
606        I32FieldAccessor::do_get(&self.0, self.1.clone())
607    }
608}
609
610pub struct I32FieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
611
612impl<'a> FieldRefMut<'a> for I32FieldRefMut<'a> {
613    fn new(
614        property_path: PropertyPath,
615        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
616    ) -> Self {
617        I32FieldRefMut(property_path, data_container.clone())
618    }
619}
620
621impl<'a> I32FieldRefMut<'a> {
622    pub fn get(&self) -> DataSetResult<i32> {
623        I32FieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
624    }
625
626    pub fn set(
627        &self,
628        value: i32,
629    ) -> DataSetResult<Option<Value>> {
630        I32FieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
631    }
632}
633
634pub struct I32Field(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
635
636impl Field for I32Field {
637    fn new(
638        property_path: PropertyPath,
639        data_container: &Rc<RefCell<Option<DataContainer>>>,
640    ) -> Self {
641        I32Field(property_path, data_container.clone())
642    }
643}
644
645impl I32Field {
646    pub fn get(&self) -> DataSetResult<i32> {
647        I32FieldAccessor::do_get(
648            &self.0,
649            self.1
650                .borrow_mut()
651                .as_mut()
652                .ok_or(DataSetError::DataTaken)?
653                .read(),
654        )
655    }
656
657    pub fn set(
658        &self,
659        value: i32,
660    ) -> DataSetResult<Option<Value>> {
661        I32FieldAccessor::do_set(
662            &self.0,
663            &mut self
664                .1
665                .borrow_mut()
666                .as_mut()
667                .ok_or(DataSetError::DataTaken)?
668                .to_mut(),
669            value,
670        )
671    }
672}
673
674pub struct I64FieldAccessor(pub PropertyPath);
675
676impl FieldAccessor for I64FieldAccessor {
677    fn new(property_path: PropertyPath) -> Self {
678        I64FieldAccessor(property_path)
679    }
680}
681
682impl I64FieldAccessor {
683    fn do_get(
684        property_path: &PropertyPath,
685        data_container: DataContainerRef,
686    ) -> DataSetResult<i64> {
687        Ok(data_container
688            .resolve_property(property_path.path())?
689            .as_i64()
690            .unwrap())
691    }
692
693    fn do_set(
694        property_path: &PropertyPath,
695        data_container: &mut DataContainerRefMut,
696        value: i64,
697    ) -> DataSetResult<Option<Value>> {
698        data_container.set_property_override(property_path.path(), Some(Value::I64(value)))
699    }
700
701    pub fn get(
702        &self,
703        data_container: DataContainerRef,
704    ) -> DataSetResult<i64> {
705        Self::do_get(&self.0, data_container)
706    }
707
708    pub fn set(
709        &self,
710        data_container: &mut DataContainerRefMut,
711        value: i64,
712    ) -> DataSetResult<Option<Value>> {
713        Self::do_set(&self.0, data_container, value)
714    }
715}
716
717pub struct I64FieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
718
719impl<'a> FieldRef<'a> for I64FieldRef<'a> {
720    fn new(
721        property_path: PropertyPath,
722        data_container: DataContainerRef<'a>,
723    ) -> Self {
724        I64FieldRef(property_path, data_container)
725    }
726}
727
728impl<'a> I64FieldRef<'a> {
729    pub fn get(&self) -> DataSetResult<i64> {
730        I64FieldAccessor::do_get(&self.0, self.1.clone())
731    }
732}
733
734pub struct I64FieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
735
736impl<'a> FieldRefMut<'a> for I64FieldRefMut<'a> {
737    fn new(
738        property_path: PropertyPath,
739        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
740    ) -> Self {
741        I64FieldRefMut(property_path, data_container.clone())
742    }
743}
744
745impl<'a> I64FieldRefMut<'a> {
746    pub fn get(&self) -> DataSetResult<i64> {
747        I64FieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
748    }
749
750    pub fn set(
751        &self,
752        value: i64,
753    ) -> DataSetResult<Option<Value>> {
754        I64FieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
755    }
756}
757
758pub struct I64Field(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
759
760impl Field for I64Field {
761    fn new(
762        property_path: PropertyPath,
763        data_container: &Rc<RefCell<Option<DataContainer>>>,
764    ) -> Self {
765        I64Field(property_path, data_container.clone())
766    }
767}
768
769impl I64Field {
770    pub fn get(&self) -> DataSetResult<i64> {
771        I64FieldAccessor::do_get(
772            &self.0,
773            self.1
774                .borrow_mut()
775                .as_mut()
776                .ok_or(DataSetError::DataTaken)?
777                .read(),
778        )
779    }
780
781    pub fn set(
782        &self,
783        value: i64,
784    ) -> DataSetResult<Option<Value>> {
785        I64FieldAccessor::do_set(
786            &self.0,
787            &mut self
788                .1
789                .borrow_mut()
790                .as_mut()
791                .ok_or(DataSetError::DataTaken)?
792                .to_mut(),
793            value,
794        )
795    }
796}
797
798pub struct U32FieldAccessor(pub PropertyPath);
799
800impl FieldAccessor for U32FieldAccessor {
801    fn new(property_path: PropertyPath) -> Self {
802        U32FieldAccessor(property_path)
803    }
804}
805
806impl U32FieldAccessor {
807    fn do_get(
808        property_path: &PropertyPath,
809        data_container: DataContainerRef,
810    ) -> DataSetResult<u32> {
811        Ok(data_container
812            .resolve_property(property_path.path())?
813            .as_u32()
814            .unwrap())
815    }
816
817    fn do_set(
818        property_path: &PropertyPath,
819        data_container: &mut DataContainerRefMut,
820        value: u32,
821    ) -> DataSetResult<Option<Value>> {
822        data_container.set_property_override(property_path.path(), Some(Value::U32(value)))
823    }
824
825    pub fn get(
826        &self,
827        data_container: DataContainerRef,
828    ) -> DataSetResult<u32> {
829        Self::do_get(&self.0, data_container)
830    }
831
832    pub fn set(
833        &self,
834        data_container: &mut DataContainerRefMut,
835        value: u32,
836    ) -> DataSetResult<Option<Value>> {
837        Self::do_set(&self.0, data_container, value)
838    }
839}
840
841pub struct U32FieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
842
843impl<'a> FieldRef<'a> for U32FieldRef<'a> {
844    fn new(
845        property_path: PropertyPath,
846        data_container: DataContainerRef<'a>,
847    ) -> Self {
848        U32FieldRef(property_path, data_container)
849    }
850}
851
852impl<'a> U32FieldRef<'a> {
853    pub fn get(&self) -> DataSetResult<u32> {
854        U32FieldAccessor::do_get(&self.0, self.1.clone())
855    }
856}
857
858pub struct U32FieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
859
860impl<'a> FieldRefMut<'a> for U32FieldRefMut<'a> {
861    fn new(
862        property_path: PropertyPath,
863        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
864    ) -> Self {
865        U32FieldRefMut(property_path, data_container.clone())
866    }
867}
868
869impl<'a> U32FieldRefMut<'a> {
870    pub fn get(&self) -> DataSetResult<u32> {
871        U32FieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
872    }
873
874    pub fn set(
875        &self,
876        value: u32,
877    ) -> DataSetResult<Option<Value>> {
878        U32FieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
879    }
880}
881
882pub struct U32Field(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
883
884impl Field for U32Field {
885    fn new(
886        property_path: PropertyPath,
887        data_container: &Rc<RefCell<Option<DataContainer>>>,
888    ) -> Self {
889        U32Field(property_path, data_container.clone())
890    }
891}
892
893impl U32Field {
894    pub fn get(&self) -> DataSetResult<u32> {
895        U32FieldAccessor::do_get(
896            &self.0,
897            self.1
898                .borrow_mut()
899                .as_mut()
900                .ok_or(DataSetError::DataTaken)?
901                .read(),
902        )
903    }
904
905    pub fn set(
906        &self,
907        value: u32,
908    ) -> DataSetResult<Option<Value>> {
909        U32FieldAccessor::do_set(
910            &self.0,
911            &mut self
912                .1
913                .borrow_mut()
914                .as_mut()
915                .ok_or(DataSetError::DataTaken)?
916                .to_mut(),
917            value,
918        )
919    }
920}
921
922pub struct U64FieldAccessor(pub PropertyPath);
923
924impl FieldAccessor for U64FieldAccessor {
925    fn new(property_path: PropertyPath) -> Self {
926        U64FieldAccessor(property_path)
927    }
928}
929
930impl U64FieldAccessor {
931    fn do_get(
932        property_path: &PropertyPath,
933        data_container: DataContainerRef,
934    ) -> DataSetResult<u64> {
935        Ok(data_container
936            .resolve_property(property_path.path())?
937            .as_u64()
938            .unwrap())
939    }
940
941    fn do_set(
942        property_path: &PropertyPath,
943        data_container: &mut DataContainerRefMut,
944        value: u64,
945    ) -> DataSetResult<Option<Value>> {
946        data_container.set_property_override(property_path.path(), Some(Value::U64(value)))
947    }
948
949    pub fn get(
950        &self,
951        data_container: DataContainerRef,
952    ) -> DataSetResult<u64> {
953        Self::do_get(&self.0, data_container)
954    }
955
956    pub fn set(
957        &self,
958        data_container: &mut DataContainerRefMut,
959        value: u64,
960    ) -> DataSetResult<Option<Value>> {
961        Self::do_set(&self.0, data_container, value)
962    }
963}
964
965pub struct U64FieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
966
967impl<'a> FieldRef<'a> for U64FieldRef<'a> {
968    fn new(
969        property_path: PropertyPath,
970        data_container: DataContainerRef<'a>,
971    ) -> Self {
972        U64FieldRef(property_path, data_container)
973    }
974}
975
976impl<'a> U64FieldRef<'a> {
977    pub fn get(&self) -> DataSetResult<u64> {
978        U64FieldAccessor::do_get(&self.0, self.1.clone())
979    }
980}
981
982pub struct U64FieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
983
984impl<'a> FieldRefMut<'a> for U64FieldRefMut<'a> {
985    fn new(
986        property_path: PropertyPath,
987        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
988    ) -> Self {
989        U64FieldRefMut(property_path, data_container.clone())
990    }
991}
992
993impl<'a> U64FieldRefMut<'a> {
994    pub fn get(&self) -> DataSetResult<u64> {
995        U64FieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
996    }
997
998    pub fn set(
999        &self,
1000        value: u64,
1001    ) -> DataSetResult<Option<Value>> {
1002        U64FieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
1003    }
1004}
1005
1006pub struct U64Field(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
1007
1008impl Field for U64Field {
1009    fn new(
1010        property_path: PropertyPath,
1011        data_container: &Rc<RefCell<Option<DataContainer>>>,
1012    ) -> Self {
1013        U64Field(property_path, data_container.clone())
1014    }
1015}
1016
1017impl U64Field {
1018    pub fn get(&self) -> DataSetResult<u64> {
1019        U64FieldAccessor::do_get(
1020            &self.0,
1021            self.1
1022                .borrow_mut()
1023                .as_mut()
1024                .ok_or(DataSetError::DataTaken)?
1025                .read(),
1026        )
1027    }
1028
1029    pub fn set(
1030        &self,
1031        value: u64,
1032    ) -> DataSetResult<Option<Value>> {
1033        U64FieldAccessor::do_set(
1034            &self.0,
1035            &mut self
1036                .1
1037                .borrow_mut()
1038                .as_mut()
1039                .ok_or(DataSetError::DataTaken)?
1040                .to_mut(),
1041            value,
1042        )
1043    }
1044}
1045
1046pub struct F32FieldAccessor(pub PropertyPath);
1047
1048impl FieldAccessor for F32FieldAccessor {
1049    fn new(property_path: PropertyPath) -> Self {
1050        F32FieldAccessor(property_path)
1051    }
1052}
1053
1054impl F32FieldAccessor {
1055    fn do_get(
1056        property_path: &PropertyPath,
1057        data_container: DataContainerRef,
1058    ) -> DataSetResult<f32> {
1059        Ok(data_container
1060            .resolve_property(property_path.path())?
1061            .as_f32()
1062            .unwrap())
1063    }
1064
1065    fn do_set(
1066        property_path: &PropertyPath,
1067        data_container: &mut DataContainerRefMut,
1068        value: f32,
1069    ) -> DataSetResult<Option<Value>> {
1070        data_container.set_property_override(property_path.path(), Some(Value::F32(value)))
1071    }
1072
1073    pub fn get(
1074        &self,
1075        data_container: DataContainerRef,
1076    ) -> DataSetResult<f32> {
1077        Self::do_get(&self.0, data_container)
1078    }
1079
1080    pub fn set(
1081        &self,
1082        data_container: &mut DataContainerRefMut,
1083        value: f32,
1084    ) -> DataSetResult<Option<Value>> {
1085        Self::do_set(&self.0, data_container, value)
1086    }
1087}
1088
1089pub struct F32FieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
1090
1091impl<'a> FieldRef<'a> for F32FieldRef<'a> {
1092    fn new(
1093        property_path: PropertyPath,
1094        data_container: DataContainerRef<'a>,
1095    ) -> Self {
1096        F32FieldRef(property_path, data_container)
1097    }
1098}
1099
1100impl<'a> F32FieldRef<'a> {
1101    pub fn get(&self) -> DataSetResult<f32> {
1102        F32FieldAccessor::do_get(&self.0, self.1.clone())
1103    }
1104}
1105
1106pub struct F32FieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
1107
1108impl<'a> FieldRefMut<'a> for F32FieldRefMut<'a> {
1109    fn new(
1110        property_path: PropertyPath,
1111        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
1112    ) -> Self {
1113        F32FieldRefMut(property_path, data_container.clone())
1114    }
1115}
1116
1117impl<'a> F32FieldRefMut<'a> {
1118    pub fn get(&self) -> DataSetResult<f32> {
1119        F32FieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
1120    }
1121
1122    pub fn set(
1123        &self,
1124        value: f32,
1125    ) -> DataSetResult<Option<Value>> {
1126        F32FieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
1127    }
1128}
1129
1130pub struct F32Field(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
1131
1132impl Field for F32Field {
1133    fn new(
1134        property_path: PropertyPath,
1135        data_container: &Rc<RefCell<Option<DataContainer>>>,
1136    ) -> Self {
1137        F32Field(property_path, data_container.clone())
1138    }
1139}
1140
1141impl F32Field {
1142    pub fn get(&self) -> DataSetResult<f32> {
1143        F32FieldAccessor::do_get(
1144            &self.0,
1145            self.1
1146                .borrow_mut()
1147                .as_mut()
1148                .ok_or(DataSetError::DataTaken)?
1149                .read(),
1150        )
1151    }
1152
1153    pub fn set(
1154        &self,
1155        value: f32,
1156    ) -> DataSetResult<Option<Value>> {
1157        F32FieldAccessor::do_set(
1158            &self.0,
1159            &mut self
1160                .1
1161                .borrow_mut()
1162                .as_mut()
1163                .ok_or(DataSetError::DataTaken)?
1164                .to_mut(),
1165            value,
1166        )
1167    }
1168}
1169
1170pub struct F64FieldAccessor(pub PropertyPath);
1171
1172impl FieldAccessor for F64FieldAccessor {
1173    fn new(property_path: PropertyPath) -> Self {
1174        F64FieldAccessor(property_path)
1175    }
1176}
1177
1178impl F64FieldAccessor {
1179    fn do_get(
1180        property_path: &PropertyPath,
1181        data_container: DataContainerRef,
1182    ) -> DataSetResult<f64> {
1183        Ok(data_container
1184            .resolve_property(property_path.path())?
1185            .as_f64()
1186            .unwrap())
1187    }
1188
1189    fn do_set(
1190        property_path: &PropertyPath,
1191        data_container: &mut DataContainerRefMut,
1192        value: f64,
1193    ) -> DataSetResult<Option<Value>> {
1194        data_container.set_property_override(property_path.path(), Some(Value::F64(value)))
1195    }
1196
1197    pub fn get(
1198        &self,
1199        data_container: DataContainerRef,
1200    ) -> DataSetResult<f64> {
1201        Self::do_get(&self.0, data_container)
1202    }
1203
1204    pub fn set(
1205        &self,
1206        data_container: &mut DataContainerRefMut,
1207        value: f64,
1208    ) -> DataSetResult<Option<Value>> {
1209        Self::do_set(&self.0, data_container, value)
1210    }
1211}
1212
1213pub struct F64FieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
1214
1215impl<'a> FieldRef<'a> for F64FieldRef<'a> {
1216    fn new(
1217        property_path: PropertyPath,
1218        data_container: DataContainerRef<'a>,
1219    ) -> Self {
1220        F64FieldRef(property_path, data_container)
1221    }
1222}
1223
1224impl<'a> F64FieldRef<'a> {
1225    pub fn get(&self) -> DataSetResult<f64> {
1226        F64FieldAccessor::do_get(&self.0, self.1.clone())
1227    }
1228}
1229
1230pub struct F64FieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
1231
1232impl<'a> FieldRefMut<'a> for F64FieldRefMut<'a> {
1233    fn new(
1234        property_path: PropertyPath,
1235        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
1236    ) -> Self {
1237        F64FieldRefMut(property_path, data_container.clone())
1238    }
1239}
1240
1241impl<'a> F64FieldRefMut<'a> {
1242    pub fn get(&self) -> DataSetResult<f64> {
1243        F64FieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
1244    }
1245
1246    pub fn set(
1247        &self,
1248        value: f64,
1249    ) -> DataSetResult<Option<Value>> {
1250        F64FieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
1251    }
1252}
1253
1254pub struct F64Field(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
1255
1256impl Field for F64Field {
1257    fn new(
1258        property_path: PropertyPath,
1259        data_container: &Rc<RefCell<Option<DataContainer>>>,
1260    ) -> Self {
1261        F64Field(property_path, data_container.clone())
1262    }
1263}
1264
1265impl F64Field {
1266    pub fn get(&self) -> DataSetResult<f64> {
1267        F64FieldAccessor::do_get(
1268            &self.0,
1269            self.1
1270                .borrow_mut()
1271                .as_mut()
1272                .ok_or(DataSetError::DataTaken)?
1273                .read(),
1274        )
1275    }
1276
1277    pub fn set(
1278        &self,
1279        value: f64,
1280    ) -> DataSetResult<Option<Value>> {
1281        F64FieldAccessor::do_set(
1282            &self.0,
1283            &mut self
1284                .1
1285                .borrow_mut()
1286                .as_mut()
1287                .ok_or(DataSetError::DataTaken)?
1288                .to_mut(),
1289            value,
1290        )
1291    }
1292}
1293
1294pub struct BytesFieldAccessor(pub PropertyPath);
1295
1296impl FieldAccessor for BytesFieldAccessor {
1297    fn new(property_path: PropertyPath) -> Self {
1298        BytesFieldAccessor(property_path)
1299    }
1300}
1301
1302impl BytesFieldAccessor {
1303    fn do_get<'a>(
1304        property_path: &PropertyPath,
1305        data_container: &'a DataContainerRef<'a>,
1306    ) -> DataSetResult<&'a Arc<Vec<u8>>> {
1307        Ok(data_container
1308            .resolve_property(property_path.path())?
1309            .as_bytes()
1310            .unwrap())
1311    }
1312
1313    fn do_set<T: Into<Arc<Vec<u8>>>>(
1314        property_path: &PropertyPath,
1315        data_container: &mut DataContainerRefMut,
1316        value: T,
1317    ) -> DataSetResult<Option<Value>> {
1318        data_container.set_property_override(property_path.path(), Some(Value::Bytes(value.into())))
1319    }
1320
1321    pub fn get<'a, 'b>(
1322        &'a self,
1323        data_container: &'b DataContainerRef<'b>,
1324    ) -> DataSetResult<&'b Arc<Vec<u8>>> {
1325        Self::do_get(&self.0, &data_container)
1326    }
1327
1328    pub fn set(
1329        &self,
1330        data_container: &mut DataContainerRefMut,
1331        value: Arc<Vec<u8>>,
1332    ) -> DataSetResult<Option<Value>> {
1333        Self::do_set(&self.0, data_container, value)
1334    }
1335}
1336
1337pub struct BytesFieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
1338
1339impl<'a> FieldRef<'a> for BytesFieldRef<'a> {
1340    fn new(
1341        property_path: PropertyPath,
1342        data_container: DataContainerRef<'a>,
1343    ) -> Self {
1344        BytesFieldRef(property_path, data_container)
1345    }
1346}
1347
1348impl<'a> BytesFieldRef<'a> {
1349    pub fn get(&self) -> DataSetResult<&Arc<Vec<u8>>> {
1350        BytesFieldAccessor::do_get(&self.0, &self.1)
1351    }
1352}
1353
1354pub struct BytesFieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
1355
1356impl<'a> FieldRefMut<'a> for BytesFieldRefMut<'a> {
1357    fn new(
1358        property_path: PropertyPath,
1359        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
1360    ) -> Self {
1361        BytesFieldRefMut(property_path, data_container.clone())
1362    }
1363}
1364
1365impl<'a> BytesFieldRefMut<'a> {
1366    pub fn get(&self) -> DataSetResult<Arc<Vec<u8>>> {
1367        // The RefMut has to clone because we can't return a reference to the interior of the Rc<RefCell<T>>
1368        // We could fix this by making the bytes type be an Arc<[u8]>
1369        Ok(self
1370            .1
1371            .borrow_mut()
1372            .resolve_property(self.0.path())?
1373            .as_bytes()
1374            .unwrap()
1375            .clone())
1376    }
1377
1378    pub fn set<T: Into<Arc<Vec<u8>>>>(
1379        &self,
1380        value: Arc<Vec<u8>>,
1381    ) -> DataSetResult<Option<Value>> {
1382        BytesFieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
1383    }
1384}
1385
1386pub struct BytesField(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
1387
1388impl Field for BytesField {
1389    fn new(
1390        property_path: PropertyPath,
1391        data_container: &Rc<RefCell<Option<DataContainer>>>,
1392    ) -> Self {
1393        BytesField(property_path, data_container.clone())
1394    }
1395}
1396
1397impl BytesField {
1398    pub fn get(&self) -> DataSetResult<Arc<Vec<u8>>> {
1399        // The RefMut has to clone because we can't return a reference to the interior of the Rc<RefCell<T>>
1400        // We could fix this by making the bytes type be an Arc<[u8]>
1401        Ok(self
1402            .1
1403            .borrow_mut()
1404            .as_mut()
1405            .ok_or(DataSetError::DataTaken)?
1406            .resolve_property(self.0.path())?
1407            .as_bytes()
1408            .unwrap()
1409            .clone())
1410    }
1411
1412    pub fn set<T: Into<Arc<Vec<u8>>>>(
1413        &self,
1414        value: T,
1415    ) -> DataSetResult<Option<Value>> {
1416        BytesFieldAccessor::do_set(
1417            &self.0,
1418            &mut self
1419                .1
1420                .borrow_mut()
1421                .as_mut()
1422                .ok_or(DataSetError::DataTaken)?
1423                .to_mut(),
1424            value,
1425        )
1426    }
1427}
1428
1429pub struct StringFieldAccessor(pub PropertyPath);
1430
1431impl FieldAccessor for StringFieldAccessor {
1432    fn new(property_path: PropertyPath) -> Self {
1433        StringFieldAccessor(property_path)
1434    }
1435}
1436
1437impl StringFieldAccessor {
1438    fn do_get(
1439        property_path: &PropertyPath,
1440        data_container: DataContainerRef,
1441    ) -> DataSetResult<Arc<String>> {
1442        Ok(data_container
1443            .resolve_property(property_path.path())?
1444            .as_string()
1445            .unwrap()
1446            .clone())
1447    }
1448
1449    fn do_set<T: Into<Arc<String>>>(
1450        property_path: &PropertyPath,
1451        data_container: &mut DataContainerRefMut,
1452        value: T,
1453    ) -> DataSetResult<Option<Value>> {
1454        data_container.set_property_override(
1455            property_path.path(),
1456            Some(Value::String(value.into().clone())),
1457        )
1458    }
1459
1460    pub fn get(
1461        &self,
1462        data_container: DataContainerRef,
1463    ) -> DataSetResult<Arc<String>> {
1464        Self::do_get(&self.0, data_container)
1465    }
1466
1467    pub fn set<'a, T: Into<Arc<String>>>(
1468        &self,
1469        data_container: &'a mut DataContainerRefMut,
1470        value: T,
1471    ) -> DataSetResult<Option<Value>> {
1472        Self::do_set(&self.0, data_container, value)
1473    }
1474}
1475
1476pub struct StringFieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
1477
1478impl<'a> FieldRef<'a> for StringFieldRef<'a> {
1479    fn new(
1480        property_path: PropertyPath,
1481        data_container: DataContainerRef<'a>,
1482    ) -> Self {
1483        StringFieldRef(property_path, data_container)
1484    }
1485}
1486
1487impl<'a> StringFieldRef<'a> {
1488    pub fn get(&'a self) -> DataSetResult<Arc<String>> {
1489        StringFieldAccessor::do_get(&self.0, self.1.clone())
1490    }
1491}
1492
1493pub struct StringFieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
1494
1495impl<'a> FieldRefMut<'a> for StringFieldRefMut<'a> {
1496    fn new(
1497        property_path: PropertyPath,
1498        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
1499    ) -> Self {
1500        StringFieldRefMut(property_path, data_container.clone())
1501    }
1502}
1503
1504impl<'a> StringFieldRefMut<'a> {
1505    pub fn get(&'a self) -> DataSetResult<Arc<String>> {
1506        StringFieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
1507    }
1508
1509    pub fn set<T: Into<Arc<String>>>(
1510        &self,
1511        value: T,
1512    ) -> DataSetResult<Option<Value>> {
1513        StringFieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
1514    }
1515}
1516
1517pub struct StringField(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
1518
1519impl Field for StringField {
1520    fn new(
1521        property_path: PropertyPath,
1522        data_container: &Rc<RefCell<Option<DataContainer>>>,
1523    ) -> Self {
1524        StringField(property_path, data_container.clone())
1525    }
1526}
1527
1528impl StringField {
1529    pub fn get(&self) -> DataSetResult<Arc<String>> {
1530        StringFieldAccessor::do_get(
1531            &self.0,
1532            self.1
1533                .borrow_mut()
1534                .as_mut()
1535                .ok_or(DataSetError::DataTaken)?
1536                .read(),
1537        )
1538    }
1539
1540    pub fn set<T: Into<Arc<String>>>(
1541        &self,
1542        value: T,
1543    ) -> DataSetResult<Option<Value>> {
1544        StringFieldAccessor::do_set(
1545            &self.0,
1546            &mut self
1547                .1
1548                .borrow_mut()
1549                .as_mut()
1550                .ok_or(DataSetError::DataTaken)?
1551                .to_mut(),
1552            value,
1553        )
1554    }
1555}
1556
1557pub struct StaticArrayFieldAccessor<T: FieldAccessor>(pub PropertyPath, PhantomData<T>);
1558
1559impl<T: FieldAccessor> FieldAccessor for StaticArrayFieldAccessor<T> {
1560    fn new(property_path: PropertyPath) -> Self {
1561        StaticArrayFieldAccessor(property_path, PhantomData::default())
1562    }
1563}
1564
1565impl<T: FieldAccessor> StaticArrayFieldAccessor<T> {
1566    pub fn resolve_entries(
1567        &self,
1568        data_container: DataContainerRef,
1569    ) -> DataSetResult<Box<[Uuid]>> {
1570        data_container.resolve_dynamic_array_entries(self.0.path())
1571    }
1572
1573    pub fn entry(
1574        &self,
1575        index: usize,
1576    ) -> T {
1577        T::new(self.0.push(&index.to_string()))
1578    }
1579}
1580
1581pub struct StaticArrayFieldRef<'a, T: FieldRef<'a>>(
1582    pub PropertyPath,
1583    DataContainerRef<'a>,
1584    PhantomData<T>,
1585);
1586
1587impl<'a, T: FieldRef<'a>> FieldRef<'a> for StaticArrayFieldRef<'a, T> {
1588    fn new(
1589        property_path: PropertyPath,
1590        data_container: DataContainerRef<'a>,
1591    ) -> Self {
1592        StaticArrayFieldRef(property_path, data_container, PhantomData)
1593    }
1594}
1595
1596impl<'a, T: FieldRef<'a>> StaticArrayFieldRef<'a, T> {
1597    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1598        self.1.resolve_dynamic_array_entries(self.0.path())
1599    }
1600
1601    pub fn entry(
1602        &self,
1603        index: usize,
1604    ) -> T {
1605        T::new(self.0.push(&index.to_string()), self.1.clone())
1606    }
1607}
1608
1609pub struct StaticArrayFieldRefMut<'a, T: FieldRefMut<'a>>(
1610    pub PropertyPath,
1611    Rc<RefCell<DataContainerRefMut<'a>>>,
1612    PhantomData<T>,
1613);
1614
1615impl<'a, T: FieldRefMut<'a>> FieldRefMut<'a> for StaticArrayFieldRefMut<'a, T> {
1616    fn new(
1617        property_path: PropertyPath,
1618        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
1619    ) -> Self {
1620        StaticArrayFieldRefMut(property_path, data_container.clone(), PhantomData)
1621    }
1622}
1623
1624impl<'a, T: FieldRefMut<'a>> StaticArrayFieldRefMut<'a, T> {
1625    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1626        self.1
1627            .borrow_mut()
1628            .resolve_dynamic_array_entries(self.0.path())
1629    }
1630
1631    pub fn entry(
1632        &'a self,
1633        index: usize,
1634    ) -> T {
1635        T::new(self.0.push(&index.to_string()), &self.1)
1636    }
1637}
1638
1639pub struct StaticArrayField<T: Field>(
1640    pub PropertyPath,
1641    Rc<RefCell<Option<DataContainer>>>,
1642    PhantomData<T>,
1643);
1644
1645impl<'a, T: Field> Field for StaticArrayField<T> {
1646    fn new(
1647        property_path: PropertyPath,
1648        data_container: &Rc<RefCell<Option<DataContainer>>>,
1649    ) -> Self {
1650        StaticArrayField(property_path, data_container.clone(), PhantomData)
1651    }
1652}
1653
1654impl<'a, T: Field> StaticArrayField<T> {
1655    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1656        self.1
1657            .borrow_mut()
1658            .as_mut()
1659            .ok_or(DataSetError::DataTaken)?
1660            .resolve_dynamic_array_entries(self.0.path())
1661    }
1662
1663    pub fn entry(
1664        &'a self,
1665        index: usize,
1666    ) -> T {
1667        T::new(self.0.push(&index.to_string()), &self.1)
1668    }
1669}
1670
1671pub struct DynamicArrayFieldAccessor<T: FieldAccessor>(pub PropertyPath, PhantomData<T>);
1672
1673impl<T: FieldAccessor> FieldAccessor for DynamicArrayFieldAccessor<T> {
1674    fn new(property_path: PropertyPath) -> Self {
1675        DynamicArrayFieldAccessor(property_path, PhantomData::default())
1676    }
1677}
1678
1679impl<T: FieldAccessor> DynamicArrayFieldAccessor<T> {
1680    pub fn resolve_entries(
1681        &self,
1682        data_container: DataContainerRef,
1683    ) -> DataSetResult<Box<[Uuid]>> {
1684        data_container.resolve_dynamic_array_entries(self.0.path())
1685    }
1686
1687    pub fn entry(
1688        &self,
1689        entry_uuid: Uuid,
1690    ) -> T {
1691        T::new(self.0.push(&entry_uuid.to_string()))
1692    }
1693
1694    pub fn add_entry(
1695        &self,
1696        data_container: &mut DataContainerRefMut,
1697    ) -> DataSetResult<Uuid> {
1698        data_container.add_dynamic_array_entry(self.0.path())
1699    }
1700
1701    pub fn remove_entry(
1702        &self,
1703        data_container: &mut DataContainerRefMut,
1704        entry_id: Uuid,
1705    ) -> DataSetResult<bool> {
1706        data_container.remove_dynamic_array_entry(self.0.path(), entry_id)
1707    }
1708}
1709
1710pub struct DynamicArrayFieldRef<'a, T: FieldRef<'a>>(
1711    pub PropertyPath,
1712    DataContainerRef<'a>,
1713    PhantomData<T>,
1714);
1715
1716impl<'a, T: FieldRef<'a>> FieldRef<'a> for DynamicArrayFieldRef<'a, T> {
1717    fn new(
1718        property_path: PropertyPath,
1719        data_container: DataContainerRef<'a>,
1720    ) -> Self {
1721        DynamicArrayFieldRef(property_path, data_container, PhantomData)
1722    }
1723}
1724
1725impl<'a, T: FieldRef<'a>> DynamicArrayFieldRef<'a, T> {
1726    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1727        self.1.resolve_dynamic_array_entries(self.0.path())
1728    }
1729
1730    pub fn entry(
1731        &self,
1732        entry_uuid: Uuid,
1733    ) -> T {
1734        T::new(self.0.push(&entry_uuid.to_string()), self.1.clone())
1735    }
1736}
1737
1738pub struct DynamicArrayFieldRefMut<'a, T: FieldRefMut<'a>>(
1739    pub PropertyPath,
1740    Rc<RefCell<DataContainerRefMut<'a>>>,
1741    PhantomData<T>,
1742);
1743
1744impl<'a, T: FieldRefMut<'a>> FieldRefMut<'a> for DynamicArrayFieldRefMut<'a, T> {
1745    fn new(
1746        property_path: PropertyPath,
1747        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
1748    ) -> Self {
1749        DynamicArrayFieldRefMut(property_path, data_container.clone(), PhantomData)
1750    }
1751}
1752
1753impl<'a, T: FieldRefMut<'a>> DynamicArrayFieldRefMut<'a, T> {
1754    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1755        self.1
1756            .borrow_mut()
1757            .resolve_dynamic_array_entries(self.0.path())
1758    }
1759
1760    pub fn entry(
1761        &'a self,
1762        entry_uuid: Uuid,
1763    ) -> T {
1764        T::new(self.0.push(&entry_uuid.to_string()), &self.1)
1765    }
1766
1767    pub fn add_entry(&self) -> DataSetResult<Uuid> {
1768        self.1.borrow_mut().add_dynamic_array_entry(self.0.path())
1769    }
1770
1771    pub fn remove_entry(
1772        &self,
1773        entry_id: Uuid,
1774    ) -> DataSetResult<bool> {
1775        self.1
1776            .borrow_mut()
1777            .remove_dynamic_array_entry(self.0.path(), entry_id)
1778    }
1779}
1780
1781pub struct DynamicArrayField<T: Field>(
1782    pub PropertyPath,
1783    Rc<RefCell<Option<DataContainer>>>,
1784    PhantomData<T>,
1785);
1786
1787impl<'a, T: Field> Field for DynamicArrayField<T> {
1788    fn new(
1789        property_path: PropertyPath,
1790        data_container: &Rc<RefCell<Option<DataContainer>>>,
1791    ) -> Self {
1792        DynamicArrayField(property_path, data_container.clone(), PhantomData)
1793    }
1794}
1795
1796impl<'a, T: Field> DynamicArrayField<T> {
1797    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1798        self.1
1799            .borrow_mut()
1800            .as_mut()
1801            .ok_or(DataSetError::DataTaken)?
1802            .resolve_dynamic_array_entries(self.0.path())
1803    }
1804
1805    pub fn entry(
1806        &'a self,
1807        entry_uuid: Uuid,
1808    ) -> T {
1809        T::new(self.0.push(&entry_uuid.to_string()), &self.1)
1810    }
1811
1812    pub fn add_entry(&self) -> DataSetResult<Uuid> {
1813        self.1
1814            .borrow_mut()
1815            .as_mut()
1816            .ok_or(DataSetError::DataTaken)?
1817            .add_dynamic_array_entry(self.0.path())
1818    }
1819
1820    pub fn remove_entry(
1821        &self,
1822        entry_id: Uuid,
1823    ) -> DataSetResult<bool> {
1824        self.1
1825            .borrow_mut()
1826            .as_mut()
1827            .ok_or(DataSetError::DataTaken)?
1828            .remove_dynamic_array_entry(self.0.path(), entry_id)
1829    }
1830}
1831
1832pub struct MapFieldAccessor<KeyT: FieldAccessor, ValueT: FieldAccessor>(
1833    pub PropertyPath,
1834    PhantomData<(KeyT, ValueT)>,
1835);
1836
1837impl<KeyT: FieldAccessor, ValueT: FieldAccessor> FieldAccessor for MapFieldAccessor<KeyT, ValueT> {
1838    fn new(property_path: PropertyPath) -> Self {
1839        MapFieldAccessor(property_path, PhantomData::default())
1840    }
1841}
1842
1843impl<KeyT: FieldAccessor, ValueT: FieldAccessor> MapFieldAccessor<KeyT, ValueT> {
1844    pub fn resolve_entries(
1845        &self,
1846        data_container: DataContainerRef,
1847    ) -> DataSetResult<Box<[Uuid]>> {
1848        data_container.resolve_map_entries(self.0.path())
1849    }
1850
1851    pub fn key(
1852        &self,
1853        entry_uuid: Uuid,
1854    ) -> KeyT {
1855        KeyT::new(self.0.push(&entry_uuid.to_string()))
1856    }
1857
1858    pub fn value(
1859        &self,
1860        entry_uuid: Uuid,
1861    ) -> ValueT {
1862        ValueT::new(self.0.push(&entry_uuid.to_string()))
1863    }
1864
1865    pub fn add_entry(
1866        &self,
1867        data_container: &mut DataContainerRefMut,
1868    ) -> DataSetResult<Uuid> {
1869        data_container.add_map_entry(self.0.path())
1870    }
1871
1872    pub fn remove_entry(
1873        &self,
1874        data_container: &mut DataContainerRefMut,
1875        entry_id: Uuid,
1876    ) -> DataSetResult<bool> {
1877        data_container.remove_map_entry(self.0.path(), entry_id)
1878    }
1879}
1880
1881pub struct MapFieldRef<'a, KeyT: FieldRef<'a>, ValueT: FieldRef<'a>>(
1882    pub PropertyPath,
1883    DataContainerRef<'a>,
1884    PhantomData<(KeyT, ValueT)>,
1885);
1886
1887impl<'a, KeyT: FieldRef<'a>, ValueT: FieldRef<'a>> FieldRef<'a> for MapFieldRef<'a, KeyT, ValueT> {
1888    fn new(
1889        property_path: PropertyPath,
1890        data_container: DataContainerRef<'a>,
1891    ) -> Self {
1892        MapFieldRef(property_path, data_container, PhantomData)
1893    }
1894}
1895
1896impl<'a, KeyT: FieldRef<'a>, ValueT: FieldRef<'a>> MapFieldRef<'a, KeyT, ValueT> {
1897    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1898        self.1.resolve_map_entries(self.0.path())
1899    }
1900
1901    pub fn key(
1902        &self,
1903        entry_uuid: Uuid,
1904    ) -> KeyT {
1905        KeyT::new(self.0.push(&entry_uuid.to_string()), self.1.clone())
1906    }
1907
1908    pub fn value(
1909        &self,
1910        entry_uuid: Uuid,
1911    ) -> ValueT {
1912        ValueT::new(self.0.push(&entry_uuid.to_string()), self.1.clone())
1913    }
1914}
1915
1916pub struct MapFieldRefMut<'a, KeyT: FieldRefMut<'a>, ValueT: FieldRefMut<'a>>(
1917    pub PropertyPath,
1918    Rc<RefCell<DataContainerRefMut<'a>>>,
1919    PhantomData<(KeyT, ValueT)>,
1920);
1921
1922impl<'a, KeyT: FieldRefMut<'a>, ValueT: FieldRefMut<'a>> FieldRefMut<'a>
1923    for MapFieldRefMut<'a, KeyT, ValueT>
1924{
1925    fn new(
1926        property_path: PropertyPath,
1927        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
1928    ) -> Self {
1929        MapFieldRefMut(property_path, data_container.clone(), PhantomData)
1930    }
1931}
1932
1933impl<'a, KeyT: FieldRefMut<'a>, ValueT: FieldRefMut<'a>> MapFieldRefMut<'a, KeyT, ValueT> {
1934    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1935        self.1.borrow_mut().resolve_map_entries(self.0.path())
1936    }
1937
1938    pub fn key(
1939        &'a self,
1940        entry_uuid: Uuid,
1941    ) -> KeyT {
1942        KeyT::new(self.0.push(&entry_uuid.to_string()), &self.1)
1943    }
1944
1945    pub fn value(
1946        &'a self,
1947        entry_uuid: Uuid,
1948    ) -> ValueT {
1949        ValueT::new(self.0.push(&entry_uuid.to_string()), &self.1)
1950    }
1951
1952    pub fn add_entry(&self) -> DataSetResult<Uuid> {
1953        self.1.borrow_mut().add_map_entry(self.0.path())
1954    }
1955
1956    pub fn remove_entry(
1957        &self,
1958        entry_id: Uuid,
1959    ) -> DataSetResult<bool> {
1960        self.1
1961            .borrow_mut()
1962            .remove_map_entry(self.0.path(), entry_id)
1963    }
1964}
1965
1966pub struct MapField<KeyT: Field, ValueT: Field>(
1967    pub PropertyPath,
1968    Rc<RefCell<Option<DataContainer>>>,
1969    PhantomData<(KeyT, ValueT)>,
1970);
1971
1972impl<'a, KeyT: Field, ValueT: Field> Field for MapField<KeyT, ValueT> {
1973    fn new(
1974        property_path: PropertyPath,
1975        data_container: &Rc<RefCell<Option<DataContainer>>>,
1976    ) -> Self {
1977        MapField(property_path, data_container.clone(), PhantomData)
1978    }
1979}
1980
1981impl<'a, KeyT: Field, ValueT: Field> MapField<KeyT, ValueT> {
1982    pub fn resolve_entries(&self) -> DataSetResult<Box<[Uuid]>> {
1983        self.1
1984            .borrow_mut()
1985            .as_mut()
1986            .ok_or(DataSetError::DataTaken)?
1987            .resolve_map_entries(self.0.path())
1988    }
1989
1990    pub fn key(
1991        &'a self,
1992        entry_uuid: Uuid,
1993    ) -> KeyT {
1994        KeyT::new(self.0.push(&entry_uuid.to_string()), &self.1)
1995    }
1996
1997    pub fn value(
1998        &'a self,
1999        entry_uuid: Uuid,
2000    ) -> ValueT {
2001        ValueT::new(self.0.push(&entry_uuid.to_string()), &self.1)
2002    }
2003
2004    pub fn add_entry(&self) -> DataSetResult<Uuid> {
2005        self.1
2006            .borrow_mut()
2007            .as_mut()
2008            .ok_or(DataSetError::DataTaken)?
2009            .add_map_entry(self.0.path())
2010    }
2011
2012    pub fn remove_entry(
2013        &self,
2014        entry_id: Uuid,
2015    ) -> DataSetResult<bool> {
2016        self.1
2017            .borrow_mut()
2018            .as_mut()
2019            .ok_or(DataSetError::DataTaken)?
2020            .remove_map_entry(self.0.path(), entry_id)
2021    }
2022}
2023
2024pub struct AssetRefFieldAccessor(pub PropertyPath);
2025
2026impl FieldAccessor for AssetRefFieldAccessor {
2027    fn new(property_path: PropertyPath) -> Self {
2028        AssetRefFieldAccessor(property_path)
2029    }
2030}
2031
2032impl AssetRefFieldAccessor {
2033    fn do_get(
2034        property_path: &PropertyPath,
2035        data_container: DataContainerRef,
2036    ) -> DataSetResult<AssetId> {
2037        Ok(data_container
2038            .resolve_property(property_path.path())?
2039            .as_asset_ref()
2040            .unwrap())
2041    }
2042
2043    fn do_set(
2044        property_path: &PropertyPath,
2045        data_container: &mut DataContainerRefMut,
2046        value: AssetId,
2047    ) -> DataSetResult<Option<Value>> {
2048        data_container.set_property_override(property_path.path(), Some(Value::AssetRef(value)))
2049    }
2050
2051    pub fn get(
2052        &self,
2053        data_container: DataContainerRef,
2054    ) -> DataSetResult<AssetId> {
2055        Self::do_get(&self.0, data_container)
2056    }
2057
2058    pub fn set(
2059        &self,
2060        data_container: &mut DataContainerRefMut,
2061        value: AssetId,
2062    ) -> DataSetResult<Option<Value>> {
2063        Self::do_set(&self.0, data_container, value)
2064    }
2065}
2066
2067pub struct AssetRefFieldRef<'a>(pub PropertyPath, DataContainerRef<'a>);
2068
2069impl<'a> FieldRef<'a> for AssetRefFieldRef<'a> {
2070    fn new(
2071        property_path: PropertyPath,
2072        data_container: DataContainerRef<'a>,
2073    ) -> Self {
2074        AssetRefFieldRef(property_path, data_container)
2075    }
2076}
2077
2078impl<'a> AssetRefFieldRef<'a> {
2079    pub fn get(&self) -> DataSetResult<AssetId> {
2080        AssetRefFieldAccessor::do_get(&self.0, self.1.clone())
2081    }
2082}
2083
2084pub struct AssetRefFieldRefMut<'a>(pub PropertyPath, Rc<RefCell<DataContainerRefMut<'a>>>);
2085
2086impl<'a> FieldRefMut<'a> for AssetRefFieldRefMut<'a> {
2087    fn new(
2088        property_path: PropertyPath,
2089        data_container: &'a Rc<RefCell<DataContainerRefMut<'a>>>,
2090    ) -> Self {
2091        AssetRefFieldRefMut(property_path, data_container.clone())
2092    }
2093}
2094
2095impl<'a> AssetRefFieldRefMut<'a> {
2096    pub fn get(&self) -> DataSetResult<AssetId> {
2097        AssetRefFieldAccessor::do_get(&self.0, self.1.borrow_mut().read())
2098    }
2099
2100    pub fn set(
2101        &self,
2102        value: AssetId,
2103    ) -> DataSetResult<Option<Value>> {
2104        AssetRefFieldAccessor::do_set(&self.0, &mut *self.1.borrow_mut(), value)
2105    }
2106}
2107
2108pub struct AssetRefField(pub PropertyPath, Rc<RefCell<Option<DataContainer>>>);
2109
2110impl Field for AssetRefField {
2111    fn new(
2112        property_path: PropertyPath,
2113        data_container: &Rc<RefCell<Option<DataContainer>>>,
2114    ) -> Self {
2115        AssetRefField(property_path, data_container.clone())
2116    }
2117}
2118
2119impl AssetRefField {
2120    pub fn get(&self) -> DataSetResult<AssetId> {
2121        AssetRefFieldAccessor::do_get(
2122            &self.0,
2123            self.1
2124                .borrow_mut()
2125                .as_mut()
2126                .ok_or(DataSetError::DataTaken)?
2127                .read(),
2128        )
2129    }
2130
2131    pub fn set(
2132        &self,
2133        value: AssetId,
2134    ) -> DataSetResult<Option<Value>> {
2135        AssetRefFieldAccessor::do_set(
2136            &self.0,
2137            &mut self
2138                .1
2139                .borrow_mut()
2140                .as_mut()
2141                .ok_or(DataSetError::DataTaken)?
2142                .to_mut(),
2143            value,
2144        )
2145    }
2146}