cql3_parser/
create_user.rs

1use crate::common::Identifier;
2use std::fmt::{Display, Formatter};
3
4/// data for the `create user` statement.
5#[derive(PartialEq, Debug, Clone)]
6pub struct CreateUser {
7    /// the user name
8    pub name: Identifier,
9    /// the password for the user.
10    pub password: Option<String>,
11    /// if true the `SUPERUSER` option is specified
12    pub superuser: bool,
13    /// it true the `NOSUPERUSER` option is specified.
14    pub no_superuser: bool,
15    /// only create if the user does not exist.
16    pub if_not_exists: bool,
17}
18
19impl Display for CreateUser {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        let mut with = String::new();
22
23        if let Some(password) = &self.password {
24            with.push_str(" PASSWORD ");
25            with.push_str(password);
26        }
27        if self.superuser {
28            with.push_str(" SUPERUSER");
29        }
30        if self.no_superuser {
31            with.push_str(" NOSUPERUSER");
32        }
33        if with.is_empty() {
34            write!(
35                f,
36                "USER {}{}",
37                if self.if_not_exists {
38                    "IF NOT EXISTS "
39                } else {
40                    ""
41                },
42                self.name
43            )
44        } else {
45            write!(
46                f,
47                "USER {}{} WITH{}",
48                if self.if_not_exists {
49                    "IF NOT EXISTS "
50                } else {
51                    ""
52                },
53                self.name,
54                with
55            )
56        }
57    }
58}