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
111
112
113
114
115
116
117
118
119
//! Options for database-level commands.
use bson::{Bson, Document};
use common::WriteConcern;
use db::roles::Role;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct CreateCollectionOptions {
    pub capped: Option<bool>,
    pub auto_index_id: Option<bool>,
    pub size: Option<i64>,
    pub max: Option<i64>,
    pub use_power_of_two_sizes: Option<bool>,
    pub no_padding: Option<bool>,
}

impl CreateCollectionOptions {
    pub fn new() -> CreateCollectionOptions {
        Default::default()
    }
}

impl From<CreateCollectionOptions> for Document {
    fn from(options: CreateCollectionOptions) -> Self {
        let mut document = Document::new();

        if let Some(capped) = options.capped {
            document.insert("capped", Bson::Boolean(capped));
        }

        if let Some(auto_index_id) = options.auto_index_id {
            document.insert("autoIndexId", Bson::Boolean(auto_index_id));
        }

        if let Some(size) = options.size {
            document.insert("size", Bson::I64(size));
        }

        if let Some(max) = options.max {
            document.insert("max", Bson::I64(max));
        }

        let mut flags = 0;

        if let Some(true) = options.use_power_of_two_sizes {
            flags |= 1;
        }

        if let Some(true) = options.no_padding {
            flags |= 2;
        }

        if flags != 0 {
            document.insert("flags", flags);
        }

        document
    }
}

#[derive(Default, Clone, Debug, PartialEq)]
pub struct CreateUserOptions {
    pub custom_data: Option<Document>,
    pub roles: Vec<Role>,
    pub write_concern: Option<WriteConcern>,
}

impl CreateUserOptions {
    pub fn new() -> CreateUserOptions {
        Default::default()
    }
}

impl From<CreateUserOptions> for Document {
    fn from(options: CreateUserOptions) -> Self {
        let mut document = Document::new();

        if let Some(custom_data) = options.custom_data {
            document.insert("customData", Bson::Document(custom_data));
        }

        let roles_barr = options.roles.iter().map(Role::to_bson).collect();

        document.insert("roles", Bson::Array(roles_barr));

        if let Some(write_concern) = options.write_concern {
            document.insert("writeConcern", write_concern.to_bson());
        }

        document
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct UserInfoOptions {
    pub show_credentials: Option<bool>,
    pub show_privileges: Option<bool>,
}

impl UserInfoOptions {
    pub fn new() -> UserInfoOptions {
        Default::default()
    }
}

impl From<UserInfoOptions> for Document {
    fn from(options: UserInfoOptions) -> Self {
        let mut document = Document::new();

        if let Some(show_credentials) = options.show_credentials {
            document.insert("showCredentials", show_credentials);
        }

        if let Some(show_privileges) = options.show_privileges {
            document.insert("showPrivileges", show_privileges);
        }

        document
    }
}