yang_base/definition/
param.rs1use super::{FieldName, FieldRef, IntoFieldSpec, ParamSource, ParamSpec};
4
5#[derive(Debug, Clone, PartialEq, Eq, Default)]
7pub struct Params(Vec<ParamSpec>);
8
9pub trait ParamInput {
11 fn params() -> Params;
13
14 fn decode(request: &mut crate::action::Request) -> Result<Self, crate::error::BaseError>
19 where
20 Self: serde::de::DeserializeOwned + Sized,
21 {
22 serde_json::from_value(std::mem::take(&mut request.body)).map_err(|error| {
23 crate::error::BaseError::ParamInvalid("input".into(), error.to_string())
24 })
25 }
26}
27
28impl Params {
29 pub const fn new() -> Self {
31 Self(Vec::new())
32 }
33
34 pub fn param<B>(mut self, name: FieldName, source: ParamSource, builder: B) -> Self
36 where
37 B: IntoFieldSpec,
38 {
39 let field = builder.into_field_spec(name.clone());
40 self.0.push(ParamSpec::from_spec(name, source, field));
41 self
42 }
43
44 pub fn from_field(mut self, name: FieldName, source: ParamSource, field: FieldRef) -> Self {
46 self.0.push(ParamSpec::new(name, source).from_field(field));
47 self
48 }
49
50 pub fn as_slice(&self) -> &[ParamSpec] {
52 &self.0
53 }
54
55 pub(crate) fn into_vec(self) -> Vec<ParamSpec> {
56 self.0
57 }
58}