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
110
//! Types used in methods for managing databases.

use std::iter::{FromIterator, IntoIterator};

use user::types::{NewUser, UserExtra};

/// This struct holds the properties of a database.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Database {
    /// the id of the database
    id: String,
    /// the name of the database
    name: String,
    /// the filesystem path of the database
    path: String,
    /// whether or not the database is the `_system` database
    is_system: bool,
}

impl Database {
    /// Returns the id of the database.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Returns the name of the database.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the filesystem path of the database.
    pub fn path(&self) -> &str {
        &self.path
    }

    /// Returns whether or not the database is the `_system` database.
    ///
    /// Returns `true` if the database is the `_system` database,
    /// `false` otherwise.
    pub fn is_system(&self) -> bool {
        self.is_system
    }
}

/// This struct specifies the properties of a database that is going to be
/// created.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NewDatabase<E>
    where E: UserExtra
{
    /// Has to contain a valid database name.
    name: String,
    /// Has to be an array of user objects to initially create for the new
    /// database.
    ///
    /// User information will not be changed for users that already exist. If
    /// users is not specified or does not contain any users, a default user
    /// root will be created with an empty string password. This ensures that
    /// the new database will be accessible after it is created.
    users: Vec<NewUser<E>>,
}

impl<E> NewDatabase<E>
    where E: UserExtra
{
    /// Constructs a new instance of `NewDatabase` with all attributes
    /// set explicitly.
    pub fn new<N, U>(name: N, users: U) -> Self
        where N: Into<String>, U: IntoIterator<Item=NewUser<E>>
    {
        NewDatabase {
            name: name.into(),
            users: Vec::from_iter(users.into_iter()),
        }
    }

    /// Constructs a new instance of `NewDatabase` with the specified
    /// database name.
    ///
    /// The method will be called with an empty user array.
    pub fn with_name<N>(name: N) -> Self
        where N: Into<String>
    {
        NewDatabase {
            name: name.into(),
            users: Vec::new(),
        }
    }

    /// Sets the users for this `NewDatabase` that should be assigned to the
    /// newly created database.
    pub fn set_users<U>(&mut self, users: U)
        where U: IntoIterator<Item=NewUser<E>>
    {
        self.users = Vec::from_iter(users.into_iter());
    }

    /// Returns the name of the database to be created.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns a slice of users that will be assigned to the newly created
    /// database.
    pub fn users(&self) -> &[NewUser<E>] {
        &self.users
    }
}