1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::cell::RefCell;
use std::ops::DerefMut;

use crate::{Error, ErrorKind, Result, Secret, SecurityBuffer};

pub struct ChangePassword<'a> {
    pub domain_name: String,
    pub account_name: String,
    pub old_password: Secret<String>,
    pub new_password: Secret<String>,
    pub impersonating: bool,
    pub output: &'a mut [SecurityBuffer],
}

#[derive(Default)]
struct ChangePasswordBuilderInner<'a> {
    domain_name: Option<String>,
    account_name: Option<String>,
    old_password: Option<Secret<String>>,
    new_password: Option<Secret<String>>,
    impersonating: bool,
    output: Option<&'a mut [SecurityBuffer]>,
}

pub struct ChangePasswordBuilder<'a> {
    inner: RefCell<ChangePasswordBuilderInner<'a>>,
}

impl<'a> ChangePasswordBuilder<'a> {
    pub fn new() -> Self {
        Self {
            inner: RefCell::new(ChangePasswordBuilderInner::default()),
        }
    }

    /// Required
    pub fn with_domain_name(&self, domain_name: impl Into<String>) -> &Self {
        self.inner.borrow_mut().domain_name = Some(domain_name.into());
        self
    }

    /// Required
    pub fn with_account_name(&self, account_name: impl Into<String>) -> &Self {
        self.inner.borrow_mut().account_name = Some(account_name.into());
        self
    }

    /// Required
    pub fn with_old_password(&self, old_password: impl Into<String>) -> &Self {
        self.inner.borrow_mut().old_password = Some(old_password.into().into());
        self
    }

    /// Required
    pub fn with_new_password(&self, new_password: impl Into<String>) -> &Self {
        self.inner.borrow_mut().new_password = Some(new_password.into().into());
        self
    }

    /// Optional(default to false if not set)
    pub fn with_impersonating(&self, impersonating: bool) -> &Self {
        self.inner.borrow_mut().impersonating = impersonating;
        self
    }

    /// Required
    pub fn with_output(&self, output: &'a mut [SecurityBuffer]) -> &Self {
        self.inner.borrow_mut().output = Some(output);
        self
    }

    pub fn build(&self) -> Result<ChangePassword<'a>> {
        let mut inner = self.inner.borrow_mut();

        let ChangePasswordBuilderInner {
            domain_name,
            account_name,
            old_password,
            new_password,
            impersonating,
            output,
        } = inner.deref_mut();

        Ok(ChangePassword {
            domain_name: domain_name
                .take()
                .ok_or_else(|| Error::new(ErrorKind::InvalidParameter, "Missing domain_name parameter"))?,
            account_name: account_name
                .take()
                .ok_or_else(|| Error::new(ErrorKind::InvalidParameter, "Missing account_name parameter"))?,
            old_password: old_password
                .take()
                .ok_or_else(|| Error::new(ErrorKind::InvalidParameter, "Missing old_password parameter"))?,
            new_password: new_password
                .take()
                .ok_or_else(|| Error::new(ErrorKind::InvalidParameter, "Missing new_password parameter"))?,
            impersonating: *impersonating,
            output: output
                .take()
                .ok_or_else(|| Error::new(ErrorKind::InvalidParameter, "Missing output parameter"))?,
        })
    }
}

impl<'a> Default for ChangePasswordBuilder<'a> {
    fn default() -> Self {
        Self::new()
    }
}