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
use crate::connection::DatabaseConnection;
use crate::lowlevel;

/// Connection string for LiteDB
///
/// Rust representation of the `LiteDB.ConnectionString`.
/// This struct holds the values of the connections string and will be converted to
/// `LiteDB.ConnectionString` when passed to the C# code.
pub struct ConnectionString<'a> {
    filename: &'a str,
    readonly: bool,
}

impl<'a> ConnectionString<'a> {
    pub fn connect(&self) -> crate::Result<DatabaseConnection> {
        DatabaseConnection::connect(self)
    }
}

impl<'a> ConnectionString<'a> {
    /// Create a new connection string
    pub fn new(filename: &'a str) -> Self {
        Self {
            filename,
            readonly: false,
        }
    }

    /// Set the connection string to read only
    pub fn readonly(&mut self, readonly: bool) -> &mut Self {
        self.readonly = readonly;
        self
    }
}

#[repr(C)]
pub(crate) struct ConnectionStringFFI {
    filename: lowlevel::FFISlice,
    readonly: bool,
}

impl ConnectionStringFFI {
    pub fn from(cs: &ConnectionString) -> Self {
        Self {
            filename: lowlevel::FFISlice::from_byte_slice(cs.filename.as_bytes()),
            readonly: cs.readonly,
        }
    }
}