1use crate::{
2 attributes::{impl_custom_attribute_methods, CustomAttributes},
3 type_info::impl_type_methods,
4 MaybeTyped, PartialReflect, Type, TypeInfo, TypePath,
5};
6use alloc::sync::Arc;
7
8#[derive(Clone, Debug)]
10pub struct NamedField {
11 name: &'static str,
12 type_info: fn() -> Option<&'static TypeInfo>,
13 ty: Type,
14 custom_attributes: Arc<CustomAttributes>,
15 #[cfg(feature = "documentation")]
16 docs: Option<&'static str>,
17}
18
19impl NamedField {
20 pub fn new<T: PartialReflect + MaybeTyped + TypePath>(name: &'static str) -> Self {
22 Self {
23 name,
24 type_info: T::maybe_type_info,
25 ty: Type::of::<T>(),
26 custom_attributes: Arc::new(CustomAttributes::default()),
27 #[cfg(feature = "documentation")]
28 docs: None,
29 }
30 }
31
32 #[cfg(feature = "documentation")]
34 pub fn with_docs(self, docs: Option<&'static str>) -> Self {
35 Self { docs, ..self }
36 }
37
38 pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
40 Self {
41 custom_attributes: Arc::new(custom_attributes),
42 ..self
43 }
44 }
45
46 pub fn name(&self) -> &'static str {
48 self.name
49 }
50
51 pub fn type_info(&self) -> Option<&'static TypeInfo> {
57 (self.type_info)()
58 }
59
60 impl_type_methods!(ty);
61
62 #[cfg(feature = "documentation")]
64 pub fn docs(&self) -> Option<&'static str> {
65 self.docs
66 }
67
68 impl_custom_attribute_methods!(self.custom_attributes, "field");
69}
70
71#[derive(Clone, Debug)]
73pub struct UnnamedField {
74 index: usize,
75 type_info: fn() -> Option<&'static TypeInfo>,
76 ty: Type,
77 custom_attributes: Arc<CustomAttributes>,
78 #[cfg(feature = "documentation")]
79 docs: Option<&'static str>,
80}
81
82impl UnnamedField {
83 pub fn new<T: PartialReflect + MaybeTyped + TypePath>(index: usize) -> Self {
84 Self {
85 index,
86 type_info: T::maybe_type_info,
87 ty: Type::of::<T>(),
88 custom_attributes: Arc::new(CustomAttributes::default()),
89 #[cfg(feature = "documentation")]
90 docs: None,
91 }
92 }
93
94 #[cfg(feature = "documentation")]
96 pub fn with_docs(self, docs: Option<&'static str>) -> Self {
97 Self { docs, ..self }
98 }
99
100 pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
102 Self {
103 custom_attributes: Arc::new(custom_attributes),
104 ..self
105 }
106 }
107
108 pub fn index(&self) -> usize {
110 self.index
111 }
112
113 pub fn type_info(&self) -> Option<&'static TypeInfo> {
119 (self.type_info)()
120 }
121
122 impl_type_methods!(ty);
123
124 #[cfg(feature = "documentation")]
126 pub fn docs(&self) -> Option<&'static str> {
127 self.docs
128 }
129
130 impl_custom_attribute_methods!(self.custom_attributes, "field");
131}