use crate::attributes::{impl_custom_attribute_methods, CustomAttributes};
use crate::{Reflect, TypePath, TypePathTable};
use std::any::{Any, TypeId};
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct NamedField {
name: &'static str,
type_path: TypePathTable,
type_id: TypeId,
custom_attributes: Arc<CustomAttributes>,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
}
impl NamedField {
pub fn new<T: Reflect + TypePath>(name: &'static str) -> Self {
Self {
name,
type_path: TypePathTable::of::<T>(),
type_id: TypeId::of::<T>(),
custom_attributes: Arc::new(CustomAttributes::default()),
#[cfg(feature = "documentation")]
docs: None,
}
}
#[cfg(feature = "documentation")]
pub fn with_docs(self, docs: Option<&'static str>) -> Self {
Self { docs, ..self }
}
pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
Self {
custom_attributes: Arc::new(custom_attributes),
..self
}
}
pub fn name(&self) -> &'static str {
self.name
}
pub fn type_path_table(&self) -> &TypePathTable {
&self.type_path
}
pub fn type_path(&self) -> &'static str {
self.type_path_table().path()
}
pub fn type_id(&self) -> TypeId {
self.type_id
}
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
}
#[cfg(feature = "documentation")]
pub fn docs(&self) -> Option<&'static str> {
self.docs
}
impl_custom_attribute_methods!(self.custom_attributes, "field");
}
#[derive(Clone, Debug)]
pub struct UnnamedField {
index: usize,
type_path: TypePathTable,
type_id: TypeId,
custom_attributes: Arc<CustomAttributes>,
#[cfg(feature = "documentation")]
docs: Option<&'static str>,
}
impl UnnamedField {
pub fn new<T: Reflect + TypePath>(index: usize) -> Self {
Self {
index,
type_path: TypePathTable::of::<T>(),
type_id: TypeId::of::<T>(),
custom_attributes: Arc::new(CustomAttributes::default()),
#[cfg(feature = "documentation")]
docs: None,
}
}
#[cfg(feature = "documentation")]
pub fn with_docs(self, docs: Option<&'static str>) -> Self {
Self { docs, ..self }
}
pub fn with_custom_attributes(self, custom_attributes: CustomAttributes) -> Self {
Self {
custom_attributes: Arc::new(custom_attributes),
..self
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn type_path_table(&self) -> &TypePathTable {
&self.type_path
}
pub fn type_path(&self) -> &'static str {
self.type_path_table().path()
}
pub fn type_id(&self) -> TypeId {
self.type_id
}
pub fn is<T: Any>(&self) -> bool {
TypeId::of::<T>() == self.type_id
}
#[cfg(feature = "documentation")]
pub fn docs(&self) -> Option<&'static str> {
self.docs
}
impl_custom_attribute_methods!(self.custom_attributes, "field");
}