Skip to main content

wit_encoder/
function.rs

1use std::fmt::{self, Display};
2
3use crate::{Docs, Type, ident::Ident};
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
8pub struct Params {
9    items: Vec<(Ident, Type)>,
10}
11
12impl<N> From<(N, Type)> for Params
13where
14    N: Into<Ident>,
15{
16    fn from(value: (N, Type)) -> Self {
17        Self {
18            items: vec![(value.0.into(), value.1)],
19        }
20    }
21}
22
23impl<N> FromIterator<(N, Type)> for Params
24where
25    N: Into<Ident>,
26{
27    fn from_iter<T: IntoIterator<Item = (N, Type)>>(iter: T) -> Self {
28        Self {
29            items: iter.into_iter().map(|(n, t)| (n.into(), t)).collect(),
30        }
31    }
32}
33
34impl Display for Params {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        let mut peekable = self.items.iter().peekable();
37        while let Some((name, type_)) = peekable.next() {
38            write!(f, "{name}: {type_}")?;
39            if peekable.peek().is_some() {
40                write!(f, ", ")?;
41            }
42        }
43        Ok(())
44    }
45}
46
47impl Params {
48    pub fn empty() -> Self {
49        Self::default()
50    }
51
52    pub fn push(&mut self, name: impl Into<Ident>, ty: Type) {
53        self.items.push((name.into(), ty));
54    }
55
56    pub fn item(&mut self, name: impl Into<Ident>, item: impl Into<Type>) {
57        self.items.push((name.into(), item.into()));
58    }
59
60    pub fn items(&self) -> &Vec<(Ident, Type)> {
61        &self.items
62    }
63
64    pub fn items_mut(&mut self) -> &mut Vec<(Ident, Type)> {
65        &mut self.items
66    }
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
72pub struct StandaloneFunc {
73    pub(crate) name: Ident,
74    pub(crate) params: Params,
75    pub(crate) result: Option<Type>,
76    pub(crate) docs: Option<Docs>,
77    #[serde(default)]
78    #[serde(rename = "async")]
79    pub(crate) async_: bool,
80}
81
82impl StandaloneFunc {
83    pub fn new(name: impl Into<Ident>, async_: bool) -> Self {
84        Self {
85            name: name.into(),
86            params: Params::empty(),
87            result: None,
88            docs: None,
89            async_,
90        }
91    }
92
93    pub fn set_name(&self) -> &Ident {
94        &self.name
95    }
96
97    pub fn name(&self) -> &Ident {
98        &self.name
99    }
100
101    pub fn name_mut(&mut self) -> &mut Ident {
102        &mut self.name
103    }
104
105    pub fn set_params(&mut self, params: impl Into<Params>) {
106        self.params = params.into();
107    }
108
109    pub fn params(&self) -> &Params {
110        &self.params
111    }
112
113    pub fn params_mut(&mut self) -> &mut Params {
114        &mut self.params
115    }
116
117    pub fn result(&self) -> &Option<Type> {
118        &self.result
119    }
120
121    pub fn set_result(&mut self, result: Option<Type>) {
122        self.result = result;
123    }
124
125    pub fn result_mut(&mut self) -> &mut Option<Type> {
126        &mut self.result
127    }
128
129    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
130        self.docs = docs.map(|d| d.into());
131    }
132
133    pub fn docs(&self) -> &Option<Docs> {
134        &self.docs
135    }
136
137    pub fn set_async(&mut self, async_: bool) {
138        self.async_ = async_;
139    }
140
141    pub fn async_(&self) -> bool {
142        self.async_
143    }
144
145    pub fn async_mut(&mut self) -> &mut bool {
146        &mut self.async_
147    }
148}