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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use hyper;
use std;

use DatabaseName;
use Error;
use error::BadPathKind;

/// Trait for converting a type into a `DatabasePath`.
pub trait IntoDatabasePath {
    /// Converts self into a `DatabasePath`.
    fn into_database_path(self) -> Result<DatabasePath, Error>;
}

impl<'a> IntoDatabasePath for &'a str {
    fn into_database_path(self) -> Result<DatabasePath, Error> {
        use std::str::FromStr;
        DatabasePath::from_str(self)
    }
}

impl<T: Into<DatabasePath>> IntoDatabasePath for T {
    fn into_database_path(self) -> Result<DatabasePath, Error> {
        Ok(self.into())
    }
}

/// Path part of a URI specifying a database.
///
/// A database path comprises a single URI path component specifying a database
/// name—the `/db` part of the HTTP request to GET `http://example.com:5984/db`.
///
/// Database paths are percent-encoded. For example, `/foo%2Fbar` identifies the
/// database named `foo/bar`. When a `DatabasePath` is constructed from a
/// `DatabaseName`, the percent-encoding is done automatically. When
/// constructing a `DatabasePath` from a string, the string must be
/// percent-encoded.
///
/// Although the `DatabasePath` type implements the `Ord` and `PartialOrd`
/// traits, it provides no guarantees how that ordering is defined and may
/// change the definition between any two releases of the couchdb crate. That
/// is, for two `DatabasePath` values `a` and `b`, the expression `a < b` may
/// hold true now but not in a subsequent release. Consequently, applications
/// must not rely upon any particular ordering definition.
///
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DatabasePath {
    db_name: DatabaseName,
}

impl DatabasePath {
    /// Constructs a `DatabasePath` from a given string.
    ///
    /// The `path` string must begin with a leading slash and be
    /// percent-encoded—e.g., `/foo%2Fbar` for the database named `foo/bar`.
    /// For valid CouchDB database names, this matters only if the name contains
    /// a slash character.
    ///
    pub fn parse<T: AsRef<str>>(path: T) -> Result<Self, Error> {
        use std::str::FromStr;
        DatabasePath::from_str(path.as_ref())
    }

    /// Converts self into a URI.
    pub fn into_uri(self, base_uri: hyper::Url) -> hyper::Url {

        let mut uri = base_uri;

        {
            use super::percent::percent_encode_uri_path;

            let mut p = uri.path_mut().unwrap();
            if p.last().map_or(false, |x| x.is_empty()) {
                p.pop();
            }

            p.push(percent_encode_uri_path(&self.db_name));
        }

        uri
    }
}

impl std::fmt::Display for DatabasePath {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        use super::percent::percent_encode_uri_path;
        write!(f, "/{}", percent_encode_uri_path(&self.db_name))
    }
}

impl std::str::FromStr for DatabasePath {
    type Err = Error;
    fn from_str(path: &str) -> Result<Self, Self::Err> {

        if !path.starts_with("/") {
            return Err(Error::BadDatabasePath(BadPathKind::NoLeadingSlash));
        }

        let path = &path[1..];

        // CouchDB allows database names to contain a slash, but we require any
        // slash within a name to be percent-encoded.
        if path.contains("/") {
            return Err(Error::BadDatabasePath(BadPathKind::NotDatabase));
        }

        if path.is_empty() {
            return Err(Error::BadDatabasePath(BadPathKind::NotDatabase));
        }

        let path = try!(super::percent::percent_decode(&path)
                            .map_err(|_| Error::BadDatabasePath(BadPathKind::BadPercentEncoding)));
        let db_path = DatabasePath { db_name: DatabaseName::from(path) };

        Ok(db_path)
    }
}

impl From<DatabaseName> for DatabasePath {
    fn from(db_name: DatabaseName) -> Self {
        DatabasePath { db_name: db_name }
    }
}

impl From<DatabasePath> for DatabaseName {
    fn from(db_path: DatabasePath) -> Self {
        db_path.db_name
    }
}

#[cfg(test)]
mod tests {

    use hyper;

    use DatabaseName;
    use DatabasePath;
    use Error;
    use IntoDatabasePath;
    use error::BadPathKind;

    #[test]
    fn into_database_path_from_str_ref_ok() {
        let expected = DatabasePath { db_name: "foo".into() };
        let got = "/foo".into_database_path().unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn into_database_path_from_str_ref_nok() {
        "bad_path".into_database_path().unwrap_err();
    }

    #[test]
    fn into_database_path_from_database_path() {
        let expected = DatabasePath { db_name: "foo".into() };
        let got = DatabasePath { db_name: "foo".into() }.into_database_path().unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn into_database_path_from_database_name_basic() {
        let expected = DatabasePath { db_name: "foo/% bar".into() };
        let got = DatabaseName::from("foo/% bar").into_database_path().unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn database_path_parse_ok() {
        let expected = DatabasePath { db_name: "foo".into() };
        let got = DatabasePath::parse("/foo").unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn database_path_parse_nok() {
        DatabasePath::parse("bad_path").unwrap_err();
    }

    #[test]
    fn database_path_into_uri_basic() {
        let expected = "http://example.com:1234/foo";
        let base = hyper::Url::parse("http://example.com:1234").unwrap();
        let uri = DatabasePath { db_name: "foo".into() }.into_uri(base);
        assert_eq!(expected, uri.to_string());
    }

    #[test]
    fn database_path_into_uri_trailing_slash() {
        let expected = "http://example.com:1234/foo";
        let base = hyper::Url::parse("http://example.com:1234/").unwrap();
        let uri = DatabasePath { db_name: "foo".into() }.into_uri(base);
        assert_eq!(expected, uri.to_string());
    }

    #[test]
    fn database_path_into_uri_nonempty_path() {
        let expected = "http://example.com:1234/nonempty_path/foo";
        let base = hyper::Url::parse("http://example.com:1234/nonempty_path").unwrap();
        let uri = DatabasePath { db_name: "foo".into() }.into_uri(base);
        assert_eq!(expected, uri.to_string());
    }

    #[test]
    fn database_path_into_uri_nonempty_path_with_trailing_slash() {
        let expected = "http://example.com:1234/nonempty_path/foo";
        let base = hyper::Url::parse("http://example.com:1234/nonempty_path/").unwrap();
        let uri = DatabasePath { db_name: "foo".into() }.into_uri(base);
        assert_eq!(expected, uri.to_string());
    }

    #[test]
    fn database_path_into_uri_percent_encoded() {
        let expected = "http://example.com:1234/foo%2F%25%20bar";
        let base = hyper::Url::parse("http://example.com:1234").unwrap();
        let uri = DatabasePath { db_name: "foo/% bar".into() }.into_uri(base);
        assert_eq!(expected, uri.to_string());
    }

    #[test]
    fn database_path_display() {
        let expected = "/foo%2F%25%20bar";
        let got = format!("{}", DatabasePath { db_name: "foo/% bar".into() });
        assert_eq!(expected, got);
    }

    #[test]
    fn database_path_from_str_ok() {
        use std::str::FromStr;
        let expected = DatabasePath { db_name: "foo/% bar".into() };
        let got = DatabasePath::from_str("/foo%2F%25%20bar").unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn database_path_from_str_nok_no_leading_slash() {
        use std::str::FromStr;
        let got = DatabasePath::from_str("foo");
        expect_path_parse_error!(got, BadDatabasePath, NoLeadingSlash);
    }

    #[test]
    fn database_path_from_str_nok_too_many_path_components() {
        use std::str::FromStr;
        let got = DatabasePath::from_str("/foo/bar");
        expect_path_parse_error!(got, BadDatabasePath, NotDatabase);
    }

    #[test]
    fn database_path_from_str_nok_empty_database_name() {
        use std::str::FromStr;
        let got = DatabasePath::from_str("/");
        expect_path_parse_error!(got, BadDatabasePath, NotDatabase);
    }

    #[test]
    fn database_path_from_str_nok_bad_percent_encoding() {
        use std::str::FromStr;
        let got = DatabasePath::from_str("/foo%");
        expect_path_parse_error!(got, BadDatabasePath, BadPercentEncoding);
    }

    #[test]
    fn database_path_from_database_name() {
        let expected = DatabasePath { db_name: "foo/% bar".into() };
        let got = DatabasePath::from(DatabaseName::from("foo/% bar"));
        assert_eq!(expected, got);
    }

    #[test]
    fn database_name_from_database_path() {
        let expected = DatabaseName::from("foo/% bar");
        let got = DatabaseName::from(DatabasePath { db_name: "foo/% bar".into() });
        assert_eq!(expected, got);
    }
}