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
pub mod sync {

    use crate::database::sync::Database;
    use crate::inner_client::sync::InnerClient;
    use crate::{Error, UrlError};

    /// A synchronous CouchDB client.
    ///
    /// The synchronous API is a tad easier to work with and reason about
    /// than the asynchronous one, with the downside that the operations will
    /// block the current thread until complete.
    pub struct Client {
        client: InnerClient,
    }

    impl Client {
        /// Create a new synchronous client.
        ///
        /// # Example
        /// ```
        /// use chesterfield::sync::Client;
        ///
        /// let client = Client::new("http://localhost:5984").unwrap();
        /// ```
        pub fn new(url: impl AsRef<str>) -> Result<Self, Error> {
            let client = InnerClient::new(url)?;

            Ok(Client { client })
        }

        /// Create an interface to a CouchDB database.
        ///
        /// # Example
        /// ```
        /// use chesterfield::sync::Client;
        ///
        /// let client = Client::new("http://localhost:5984").unwrap();
        ///
        /// let database = client.database("some_collection").unwrap();
        /// ```
        pub fn database(&self, name: impl AsRef<str>) -> Result<Database, UrlError> {
            let client = self.client.join(format!("{}/", name.as_ref()))?;
            Ok(Database::new(client))
        }
    }
}

pub mod r#async {

    use crate::database::r#async::Database;
    use crate::inner_client::r#async::InnerClient;
    use crate::{Error, UrlError};

    /// An asynchronous CouchDB client
    pub struct Client {
        client: InnerClient,
    }

    impl Client {
        /// Create a new asynchronous client.
        ///
        /// # Example
        /// ```
        /// use chesterfield::Client;
        ///
        /// let client = Client::new("http://localhost:5984").unwrap();
        /// ```
        pub fn new(url: impl AsRef<str>) -> Result<Self, Error> {
            let client = InnerClient::new(url)?;

            Ok(Client { client })
        }

        /// Create an interface to a CouchDB database.
        ///
        /// # Example
        /// ```
        /// use chesterfield::Client;
        ///
        /// let client = Client::new("http://localhost:5984").unwrap();
        ///
        /// let database = client.database("some_collection").unwrap();
        /// ```
        pub fn database(&self, name: impl AsRef<str>) -> Result<Database, UrlError> {
            let client = self.client.join(format!("{}/", name.as_ref()))?;
            Ok(Database::new(client))
        }
    }

}