1use crate::{Docs, Type, ident::Ident};
2
3#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
6pub struct Record {
7 pub(crate) fields: Vec<Field>,
8}
9
10impl Record {
11 pub fn new(fields: impl IntoIterator<Item = impl Into<Field>>) -> Self {
12 Self {
13 fields: fields.into_iter().map(|f| f.into()).collect(),
14 }
15 }
16
17 pub fn fields(&self) -> &[Field] {
18 &self.fields
19 }
20
21 pub fn fields_mut(&mut self) -> &mut Vec<Field> {
22 &mut self.fields
23 }
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
29pub struct Field {
30 pub(crate) name: Ident,
31 #[cfg_attr(feature = "serde", serde(rename = "type"))]
32 pub(crate) type_: Type,
33 pub(crate) docs: Option<Docs>,
34}
35
36impl Field {
37 pub fn new(name: impl Into<Ident>, ty: Type) -> Self {
38 Self {
39 name: name.into(),
40 type_: ty,
41 docs: None,
42 }
43 }
44
45 pub fn name(&self) -> &Ident {
46 &self.name
47 }
48
49 pub fn set_name(&mut self, name: impl Into<Ident>) {
50 self.name = name.into();
51 }
52
53 pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
54 self.docs = docs.map(|d| d.into());
55 }
56
57 pub fn docs(&self) -> &Option<Docs> {
58 &self.docs
59 }
60
61 pub fn type_(&self) -> &Type {
62 &self.type_
63 }
64
65 pub fn type_mut(&mut self) -> &mut Type {
66 &mut self.type_
67 }
68
69 pub fn set_type(&mut self, type_: impl Into<Type>) {
70 self.type_ = type_.into();
71 }
72}
73
74impl<N> Into<Field> for (N, Type)
75where
76 N: Into<Ident>,
77{
78 fn into(self) -> Field {
79 Field::new(self.0, self.1)
80 }
81}
82
83impl<N, D> Into<Field> for (N, Type, D)
84where
85 N: Into<Ident>,
86 D: Into<Docs>,
87{
88 fn into(self) -> Field {
89 let mut field = Field::new(self.0, self.1);
90 field.set_docs(Some(self.2.into()));
91 field
92 }
93}