rusp_lib/usp_builder/
msg.rs1use crate::usp::mod_Body::OneOfmsg_body;
2use crate::usp::mod_Request::OneOfreq_type;
3use crate::usp::mod_Response::OneOfresp_type;
4use crate::usp::Body;
5use crate::usp::Header;
6use crate::usp::Msg;
7
8use anyhow::anyhow;
9use anyhow::{Context, Result};
10
11#[derive(Clone)]
12pub struct MsgBuilder {
13 msg_id: Option<String>,
14 body: Option<Body>,
15}
16
17impl MsgBuilder {
18 #[must_use]
19 pub const fn new() -> Self {
20 Self {
21 msg_id: None,
22 body: None,
23 }
24 }
25
26 #[must_use]
27 pub fn with_msg_id(mut self, msg_id: String) -> Self {
28 self.msg_id = Some(msg_id);
29 self
30 }
31
32 #[must_use]
33 pub fn with_body(mut self, body: Body) -> Self {
34 self.body = Some(body);
35 self
36 }
37
38 pub fn build(self) -> Result<Msg> {
39 use crate::usp::mod_Body::OneOfmsg_body::{request, response};
40 use crate::usp::mod_Header::MsgType::{
41 ADD, ADD_RESP, DELETE, DELETE_RESP, DEREGISTER, DEREGISTER_RESP, ERROR, GET,
42 GET_INSTANCES, GET_INSTANCES_RESP, GET_RESP, GET_SUPPORTED_DM, GET_SUPPORTED_DM_RESP,
43 GET_SUPPORTED_PROTO, GET_SUPPORTED_PROTO_RESP, NOTIFY, NOTIFY_RESP, OPERATE,
44 OPERATE_RESP, REGISTER, REGISTER_RESP, SET, SET_RESP,
45 };
46 use crate::usp::mod_Request::OneOfreq_type::{
47 add, delete, deregister, get, get_instances, get_supported_dm, get_supported_protocol,
48 notify, operate, register, set,
49 };
50 use crate::usp::mod_Response::OneOfresp_type::{
51 add_resp, delete_resp, deregister_resp, get_instances_resp, get_resp,
52 get_supported_dm_resp, get_supported_protocol_resp, notify_resp, operate_resp,
53 register_resp, set_resp,
54 };
55
56 let msg_id = self
57 .msg_id
58 .with_context(|| "Cannot produce USP Msg without msg_id")?;
59 let body = self
60 .body
61 .with_context(|| "Cannot produce USP Msg without msg_body")?;
62
63 let msg_type = match &body.msg_body {
64 request(ref req) => match &req.req_type {
65 get(_) => GET,
66 get_supported_dm(_) => GET_SUPPORTED_DM,
67 get_instances(_) => GET_INSTANCES,
68 set(_) => SET,
69 add(_) => ADD,
70 delete(_) => DELETE,
71 operate(_) => OPERATE,
72 notify(_) => NOTIFY,
73 get_supported_protocol(_) => GET_SUPPORTED_PROTO,
74 register(_) => REGISTER,
75 deregister(_) => DEREGISTER,
76 OneOfreq_type::None => anyhow::bail!("Request type can't be None"),
77 },
78 response(ref resp) => match &resp.resp_type {
79 get_resp(_) => GET_RESP,
80 get_supported_dm_resp(_) => GET_SUPPORTED_DM_RESP,
81 get_instances_resp(_) => GET_INSTANCES_RESP,
82 set_resp(_) => SET_RESP,
83 add_resp(_) => ADD_RESP,
84 delete_resp(_) => DELETE_RESP,
85 operate_resp(_) => OPERATE_RESP,
86 notify_resp(_) => NOTIFY_RESP,
87 get_supported_protocol_resp(_) => GET_SUPPORTED_PROTO_RESP,
88 register_resp(_) => REGISTER_RESP,
89 deregister_resp(_) => DEREGISTER_RESP,
90 OneOfresp_type::None => Err(anyhow!("Response type can't be None"))?,
91 },
92 OneOfmsg_body::error(_) => ERROR,
93 OneOfmsg_body::None => Err(anyhow!("Body type can't be None"))?,
94 };
95
96 Ok(Msg {
97 header: Some(Header { msg_id, msg_type }),
98 body: Some(body),
99 })
100 }
101}