| 1 |
use crate::{Docs, Params, 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"))] |
| 6 |
pub struct Resource { |
| 7 |
pub(crate) funcs: Vec<ResourceFunc>, |
| 8 |
} |
| 9 |
|
| 10 |
impl Resource { |
| 11 |
pub fn empty() -> Self { |
| 12 |
Self { funcs: vec![] } |
| 13 |
} |
| 14 |
|
| 15 |
pub fn func(&mut self, func: ResourceFunc) { |
| 16 |
self.funcs.push(func); |
| 17 |
} |
| 18 |
|
| 19 |
pub fn funcs(&self) -> &[ResourceFunc] { |
| 20 |
&self.funcs |
| 21 |
} |
| 22 |
|
| 23 |
pub fn funcs_mut(&mut self) -> &mut Vec<ResourceFunc> { |
| 24 |
&mut self.funcs |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 29 |
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 30 |
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))] |
| 31 |
pub struct ResourceFunc { |
| 32 |
pub(crate) kind: ResourceFuncKind, |
| 33 |
pub(crate) params: Params, |
| 34 |
pub(crate) docs: Option<Docs>, |
| 35 |
} |
| 36 |
|
| 37 |
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 38 |
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 39 |
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))] |
| 40 |
pub enum ResourceFuncKind { |
| 41 |
Method(Ident, bool, Option<Type>), |
| 42 |
Static(Ident, bool, Option<Type>), |
| 43 |
Constructor(Option<Type>), |
| 44 |
} |
| 45 |
|
| 46 |
impl ResourceFunc { |
| 47 |
pub fn method(name: impl Into<Ident>, async_: bool) -> Self { |
| 48 |
Self { |
| 49 |
kind: ResourceFuncKind::Method(name.into(), async_, None), |
| 50 |
params: Params::empty(), |
| 51 |
docs: None, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
pub fn static_(name: impl Into<Ident>, async_: bool) -> Self { |
| 56 |
Self { |
| 57 |
kind: ResourceFuncKind::Static(name.into(), async_, None), |
| 58 |
params: Params::empty(), |
| 59 |
docs: None, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
pub fn constructor() -> Self { |
| 64 |
Self { |
| 65 |
kind: ResourceFuncKind::Constructor(None), |
| 66 |
params: Params::empty(), |
| 67 |
docs: None, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
pub fn set_name(&mut self, name: impl Into<Ident>) { |
| 72 |
match &mut self.kind { |
| 73 |
ResourceFuncKind::Method(n, ..) => { |
| 74 |
*n = name.into(); |
| 75 |
} |
| 76 |
ResourceFuncKind::Static(n, ..) => { |
| 77 |
*n = name.into(); |
| 78 |
} |
| 79 |
ResourceFuncKind::Constructor(..) => panic!("constructors cannot have a name"), |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
pub fn kind(&self) -> &ResourceFuncKind { |
| 84 |
&self.kind |
| 85 |
} |
| 86 |
|
| 87 |
pub fn set_params(&mut self, params: impl Into<Params>) { |
| 88 |
self.params = params.into(); |
| 89 |
} |
| 90 |
|
| 91 |
pub fn params(&self) -> &Params { |
| 92 |
&self.params |
| 93 |
} |
| 94 |
|
| 95 |
pub fn params_mut(&mut self) -> &mut Params { |
| 96 |
&mut self.params |
| 97 |
} |
| 98 |
|
| 99 |
pub fn set_result(&mut self, result: Option<Type>) { |
| 100 |
*self.result_mut() = result; |
| 101 |
} |
| 102 |
|
| 103 |
pub fn result(&self) -> &Option<Type> { |
| 104 |
match &self.kind { |
| 105 |
ResourceFuncKind::Method(.., result) => result, |
| 106 |
ResourceFuncKind::Static(.., result) => result, |
| 107 |
ResourceFuncKind::Constructor(result) => result, |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
pub fn result_mut(&mut self) -> &mut Option<Type> { |
| 112 |
match &mut self.kind { |
| 113 |
ResourceFuncKind::Method(.., result) => result, |
| 114 |
ResourceFuncKind::Static(.., result) => result, |
| 115 |
ResourceFuncKind::Constructor(result) => result, |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) { |
| 120 |
self.docs = docs.map(|d| d.into()); |
| 121 |
} |
| 122 |
|
| 123 |
pub fn docs(&self) -> &Option<Docs> { |
| 124 |
&self.docs |
| 125 |
} |
| 126 |
} |