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
use serde;
use std;

use DesignDocumentName;
use DocumentName;

/// Document identifier.
///
/// A document id specifies a document's type and name. For example, given the
/// HTTP request to `GET http://example.com:5984/db/_design/design-doc`, the
/// document id comprises `_design/design-doc` and specifies a design document
/// with the name `design-doc`.
///
/// There are three types of documents: *normal*, *design* (i.e., `_design`),
/// and *local* (i.e., `_local`). Each type is expressed as an enum variant that
/// owns the underlying document name.
///
/// Although the `DocumentId` 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
/// `DocumentId` 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 enum DocumentId {
    /// Normal document—i.e., neither a design document nor a local document.
    Normal(DocumentName),
    /// Design document (i.e., `_design`).
    Design(DesignDocumentName),

    /// Local document (i.e., `_local`).
    Local(DocumentName),
}

impl std::fmt::Display for DocumentId {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        match *self {
            DocumentId::Normal(ref name) => write!(f, "{}", name),
            DocumentId::Design(ref name) => write!(f, "_design/{}", name),
            DocumentId::Local(ref name) => write!(f, "_local/{}", name),
        }
    }
}

impl<'a> From<&'a str> for DocumentId {
    fn from(name: &str) -> Self {
        DocumentId::from(name.to_string())
    }
}

impl From<String> for DocumentId {
    fn from(name: String) -> Self {
        let design = "_design/";
        let local = "_local/";
        if name.starts_with(design) {
            let name = name[design.len()..].to_string();
            DocumentId::Design(name.into())
        } else if name.starts_with(local) {
            let name = name[local.len()..].to_string();
            DocumentId::Local(name.into())
        } else {
            DocumentId::Normal(name.into())
        }
    }
}

impl From<DocumentId> for String {
    fn from(doc_id: DocumentId) -> Self {
        match doc_id {
            DocumentId::Normal(name) => name.into(),
            DocumentId::Design(name) => format!("_design/{}", name),
            DocumentId::Local(name) => format!("_local/{}", name),
        }
    }
}

impl serde::Serialize for DocumentId {
    fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
        where S: serde::Serializer
    {
        match *self {
            DocumentId::Normal(ref name) => serializer.visit_str(name.as_ref()),
            _ => serializer.visit_str(self.to_string().as_ref()),
        }
    }
}

impl serde::Deserialize for DocumentId {
    fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
        where D: serde::Deserializer
    {
        struct Visitor;

        impl serde::de::Visitor for Visitor {
            type Value = DocumentId;

            fn visit_str<E>(&mut self, v: &str) -> Result<Self::Value, E>
                where E: serde::de::Error
            {
                Ok(DocumentId::from(v))
            }

            fn visit_string<E>(&mut self, v: String) -> Result<Self::Value, E>
                where E: serde::de::Error
            {
                Ok(DocumentId::from(v))
            }
        }

        deserializer.visit(Visitor)
    }
}
#[cfg(test)]
mod tests {

    use serde_json;

    use DocumentId;

    #[test]
    fn document_id_display_normal() {
        let expected = "foo";
        let got = format!("{}", DocumentId::Normal("foo".into()));
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_display_design() {
        let expected = "_design/foo";
        let got = format!("{}", DocumentId::Design("foo".into()));
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_display_local() {
        let expected = "_local/foo";
        let got = format!("{}", DocumentId::Local("foo".into()));
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_from_str_ref_normal() {
        let expected = DocumentId::Normal("foo".into());
        let got = DocumentId::from("foo");
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_from_str_ref_design() {
        let expected = DocumentId::Design("foo".into());
        let got = DocumentId::from("_design/foo");
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_from_str_ref_local() {
        let expected = DocumentId::Local("foo".into());
        let got = DocumentId::from("_local/foo");
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_from_string_normal() {
        let expected = DocumentId::Normal("foo".into());
        let got = DocumentId::from("foo".to_string());
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_from_string_design() {
        let expected = DocumentId::Design("foo".into());
        let got = DocumentId::from("_design/foo".to_string());
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_from_string_local() {
        let expected = DocumentId::Local("foo".into());
        let got = DocumentId::from("_local/foo".to_string());
        assert_eq!(expected, got);
    }

    #[test]
    fn string_from_document_id_normal() {
        let expected = "foo".to_string();
        let got = String::from(DocumentId::Normal("foo".into()));
        assert_eq!(expected, got);
    }

    #[test]
    fn string_from_document_id_design() {
        let expected = "_design/foo".to_string();
        let got = String::from(DocumentId::Design("foo".into()));
        assert_eq!(expected, got);
    }

    #[test]
    fn string_from_document_id_local() {
        let expected = "_local/foo".to_string();
        let got = String::from(DocumentId::Local("foo".into()));
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_serialization_normal() {
        let expected = serde_json::Value::String("foo".to_string());
        let source = DocumentId::Normal("foo".into());
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&s).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_serialization_design() {
        let expected = serde_json::Value::String("_design/foo".to_string());
        let source = DocumentId::Design("foo".into());
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&s).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_serialization_local() {
        let expected = serde_json::Value::String("_local/foo".to_string());
        let source = DocumentId::Local("foo".into());
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&s).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_deserialization_normal() {
        let expected = DocumentId::Normal("foo".into());
        let source = serde_json::Value::String("foo".to_string());
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&s).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_deserialization_design() {
        let expected = DocumentId::Design("foo".into());
        let source = serde_json::Value::String("_design/foo".to_string());
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&s).unwrap();
        assert_eq!(expected, got);
    }

    #[test]
    fn document_id_deserialization_local() {
        let expected = DocumentId::Local("foo".into());
        let source = serde_json::Value::String("_local/foo".to_string());
        let s = serde_json::to_string(&source).unwrap();
        let got = serde_json::from_str(&s).unwrap();
        assert_eq!(expected, got);
    }
}