radiate_expr/datatype/
field.rs1use super::DataType;
2use radiate_utils::SmallStr;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5use std::fmt::Display;
6
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
9pub struct Field {
10 pub name: SmallStr,
11 dtype: DataType,
12}
13
14impl Field {
15 pub const fn new_const(name: &'static str, dtype: DataType) -> Self {
16 Field {
17 name: SmallStr::from_static(name),
18 dtype,
19 }
20 }
21
22 pub fn new(name: SmallStr, dtype: DataType) -> Self {
23 Field { name, dtype }
24 }
25
26 pub fn with_dtype(&self, dtype: DataType) -> Self {
27 Field {
28 name: self.name.clone(),
29 dtype,
30 }
31 }
32
33 pub fn with_name(&self, name: SmallStr) -> Self {
34 Field {
35 name,
36 dtype: self.dtype.clone(),
37 }
38 }
39
40 #[inline]
41 pub fn name(&self) -> &SmallStr {
42 &self.name
43 }
44
45 #[inline]
46 pub fn dtype(&self) -> &DataType {
47 &self.dtype
48 }
49}
50
51impl From<(&str, DataType)> for Field {
52 fn from(s: (&str, DataType)) -> Self {
53 Self::new(s.0.into(), s.1)
54 }
55}
56
57impl From<(String, DataType)> for Field {
58 fn from(s: (String, DataType)) -> Self {
59 Self::new(s.0.into(), s.1)
60 }
61}
62
63impl Display for Field {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 write!(f, "{}: {}", self.name, self.dtype)
66 }
67}