netconf/message/rpc/operation/
validate.rs1use std::io::Write;
2
3use quick_xml::Writer;
4
5use crate::{
6 capabilities::{Capability, Requirements},
7 message::WriteError,
8 session::Context,
9 Error,
10};
11
12use super::{params::Required, Datastore, EmptyReply, Operation, Source, WriteXml};
13
14#[derive(Debug, Clone)]
15pub struct Validate {
16 source: Source,
17}
18
19impl Operation for Validate {
20 const NAME: &'static str = "validate";
21 const REQUIRED_CAPABILITIES: Requirements =
22 Requirements::Any(&[Capability::ValidateV1_0, Capability::ValidateV1_1]);
23
24 type Builder<'a> = Builder<'a>;
25 type Reply = EmptyReply;
26}
27
28impl WriteXml for Validate {
29 fn write_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), WriteError> {
30 writer
31 .create_element(Self::NAME)
32 .write_inner_content(|writer| {
33 writer
34 .create_element("source")
35 .write_inner_content(|writer| self.source.write_xml(writer))
36 .map(|_| ())
37 })
38 .map(|_| ())
39 }
40}
41
42#[derive(Debug, Clone)]
43#[must_use]
44pub struct Builder<'a> {
45 ctx: &'a Context,
46 source: Required<Source>,
47}
48
49impl Builder<'_> {
50 pub fn source(mut self, source: Datastore) -> Result<Self, Error> {
51 source.try_as_source(self.ctx).map(|source| {
52 self.source.set(Source::Datastore(source));
53 self
54 })
55 }
56
57 pub fn config(mut self, config: String) -> Self {
58 self.source.set(Source::Config(config));
59 self
60 }
61}
62
63impl<'a> super::Builder<'a, Validate> for Builder<'a> {
64 fn new(ctx: &'a Context) -> Self {
65 Self {
66 ctx,
67 source: Required::init(),
68 }
69 }
70
71 fn finish(self) -> Result<Validate, Error> {
72 Ok(Validate {
73 source: self.source.require::<Validate>("source")?,
74 })
75 }
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81 use crate::message::{
82 rpc::{MessageId, Request},
83 ClientMsg,
84 };
85
86 #[test]
87 fn request_to_xml() {
88 let req = Request {
89 message_id: MessageId(101),
90 operation: Validate {
91 source: Source::Datastore(Datastore::Candidate),
92 },
93 };
94 let expect = r#"<rpc message-id="101"><validate><source><candidate/></source></validate></rpc>]]>]]>"#;
95 assert_eq!(req.to_xml().unwrap(), expect);
96 }
97}