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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

//! [![Build Status](https://travis-ci.org/lawliet89/unicase_serde.svg)](https://travis-ci.org/lawliet89/unicase_serde)
//! [![Crates.io](https://img.shields.io/crates/v/unicase_serde.svg)](https://crates.io/crates/unicase_serde)
//! [![Repository](https://img.shields.io/github/tag/lawliet89/unicase_serde.svg)](https://github.com/lawliet89/unicase_serde)
//! [![Documentation](https://docs.rs/unicase_serde/badge.svg)](https://docs.rs/unicase_serde)
//!
//! - Documentation:  [stable](https://docs.rs/unicase_serde/) | [master branch](https://lawliet89.github.io/unicase_serde)
//!
//! serde Serialization and Deserialization for [UniCase](https://crates.io/crates/unicase) crate
//!
//! Support for serialization and deserialization using
//! [serde](https://serde.rs/).
//!
//! ## Installation
//!
//! Add the following to Cargo.toml:
//!
//! ```toml
//! serde = "1.0"
//! serde_derive = "1.0"
//! unicase="2.0"
//! unicase_serde = "0.1.0"
//! ```
//!
//! ## Usage
//!
//! You will have to use the serde `with` [field attribute](https://serde.rs/field-attrs.html) to
//! annotate any `UniCase` or `Ascii` fields with the appropriate module. See examples below.
//!
//! ## Serialization
//! Serialization for any `UniCase<S>` and `Ascii<S>` where  `S: AsRef<str>` is
//! implemented. Examples for `S` include `&str`, `Cow<'a, str>`, and `String`.
//!
//! The same `Serialize` implementation is provided in all the modules.
//!
//! ## Deserialization
//!
//! You can deserialize strings into `UniCase<S>` and `Ascii<S>` where
//!
//! - `S: FromStr + AsRef<str>`
//! - `S: From<&'de str> + AsRef<str> + 'de`
//!
//! ### `S: FromStr + AsRef<str>`
//! The `Deserialize` implementation is provided in the `unicase_serde::unicase` and
//! `unicase_serde::ascii` modules.
//! Conversion is done using the `FromStr::from_str` function.
//!
//! Typically, you will use the this implementation for any Rust built in type that owns the data
//! and does not borrow anything.
//! Example include `String`.
//!
//! You will know when you need to use the second case
//! when you get trait bound errors such
//! "as the trait bound `&'a str: std::convert::From<&str>` is not satisfied".
//!
//! ### `S: From<&'de str> + AsRef<str> + 'de`
//! The `Deserialize` implementation is provided in the `unicase_serde::unicase::borrowed` and
//! `unicase_serde::ascii::borrowed` modules.
//!
//! The second case is meant for usage with Rust built in types that borrow data. Conversion
//! is done using the `Into::into` function.
//!
//! If you use the second case with any type that owns data, you will get an error at runtime.
//!
//! ## Example Serialization and Deserialization
//! ```rust
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//! extern crate unicase;
//! extern crate unicase_serde;
//!
//! use std::borrow::Cow;
//! use unicase::{UniCase, Ascii};
//!
//! #[derive(Serialize, Deserialize)]
//! struct TestUniCase<'a> {
//!     #[serde(with = "unicase_serde::unicase")]
//!     owned: UniCase<String>,
//!     #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
//!     borrowed_str: UniCase<&'a str>,
//!     #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
//!     cow_str: UniCase<Cow<'a, str>>,
//! }
//!
//! #[derive(Serialize, Deserialize)]
//! struct TestAscii<'a> {
//!     #[serde(with = "unicase_serde::ascii")]
//!     owned: Ascii<String>,
//!     #[serde(borrow, with = "unicase_serde::ascii::borrowed")]
//!     borrowed_str: Ascii<&'a str>,
//!     #[serde(borrow, with = "unicase_serde::ascii::borrowed")]
//!     cow_str: Ascii<Cow<'a, str>>,
//! }
//!
//! # fn main() {
//! # }
//! ```
//!
//! ## Example with Custom "string" types
//! This example will demonstrate how you can use a "custom" string type and
//! still use serialization and deserialization when wrapped inside a `UniCase` or `Ascii`. This
//! is particularly useful for types like `UniCase<Cow<'a, String>>` or `UniCase<&'a String>`
//! because `&String` does not implement `From::<&str>`.
//!
//! As you can see from the example below, you can use the direct `Deserialize` implementation
//! to deserialize borrowed data. This usually does not work with Rust built in types due to
//! missing trait implementation. However, because the conversion is done using the
//! `FromStr::from_str` function, and the function signature indicates that the `&str` passed in
//! might have an ephemeral lifetime, implementors will have to convert the `&str` into an owned
//! version. So you are better off using the borrowed deserializer.
//!
//! ```rust
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//! extern crate unicase;
//! extern crate unicase_serde;
//!
//! use std::borrow::Cow;
//! use std::str::FromStr;
//! use unicase::UniCase;
//!
//! #[derive(Eq, PartialEq, Debug)]
//! struct CustomStr<'a>(Cow<'a, str>);
//!
//! impl<'a> AsRef<str> for CustomStr<'a> {
//!     fn as_ref(&self) -> &str {
//!         self.0.as_ref()
//!     }
//! }
//!
//! impl<'a> FromStr for CustomStr<'a> {
//!     type Err = ();
//!     fn from_str(s: &str) -> Result<Self, Self::Err> {
//!         Ok(CustomStr(Cow::from(s.to_string())))
//!     }
//! }
//!
//! impl<'a> From<&'a str> for CustomStr<'a> {
//!     fn from(s: &'a str) -> Self {
//!         CustomStr(Cow::from(s))
//!     }
//! }
//!
//! #[derive(Eq, PartialEq, Debug)]
//! struct CustomString<'a>(Cow<'a, String>);
//!
//! impl<'a> AsRef<str> for CustomString<'a> {
//!     fn as_ref(&self) -> &str {
//!         self.0.as_ref()
//!     }
//! }
//!
//! impl<'a> FromStr for CustomString<'a> {
//!     type Err = ();
//!     fn from_str(s: &str) -> Result<Self, Self::Err> {
//!         Ok(CustomString(Cow::Owned(s.to_string())))
//!     }
//! }
//!
//! impl<'a> From<&'a str> for CustomString<'a> {
//!     fn from(s: &'a str) -> Self {
//!         CustomString(Cow::Owned(s.to_string()))
//!     }
//! }
//!
//! #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
//! struct TestCustomStruct<'a> {
//!     #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
//!     test_str: UniCase<CustomStr<'a>>,
//!     #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
//!     test_string: UniCase<CustomString<'a>>,
//!     #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
//!     test_str_borrowed: UniCase<CustomStr<'a>>,
//!     #[serde(borrow, with = "unicase_serde::unicase::borrowed")]
//!     test_string_borrowed: UniCase<CustomString<'a>>,
//! }
//!
//! # fn main() {
//! # }
//! ```
extern crate serde as serdelib;
extern crate unicase as unicase_lib;

#[cfg(test)]
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
extern crate serde_test;

pub mod unicase {
    //! Serialization and Deserialization Implementation for `UniCase` that owns data
    //!
    //! See crate level documentation for details.
    use std::fmt;
    use std::marker::PhantomData;
    use std::str::FromStr;

    use unicase_lib::UniCase;
    use serdelib::{Serializer, Deserializer};
    use serdelib::de;

    /// Straightforward Serialization for UniCase
    pub fn serialize<S, Ser>(value: &UniCase<S>, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        S: AsRef<str>,
        Ser: Serializer,
    {
        serializer.serialize_str(value.as_ref())
    }

    /// Deserialization for `UniCase<S>` where `S: FromStr + AsRef<str>`
    pub fn deserialize<'de, S, D>(deserializer: D) -> Result<UniCase<S>, D::Error>
    where
        S: FromStr + AsRef<str>,
        D: Deserializer<'de>,
    {
        struct UniCaseVisitor<S>(PhantomData<S>);

        impl<'de, S: FromStr + AsRef<str>> de::Visitor<'de> for UniCaseVisitor<S> {
            type Value = UniCase<S>;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a string")
            }

            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                FromStr::from_str(s).map_err(|_| E::custom("FromStr conversion failed"))
            }
        }

        deserializer.deserialize_str(UniCaseVisitor(PhantomData))
    }

    pub mod borrowed {
        //! Serialization and Deserialization Implementation for `UniCase` that borrow data.
        //!
        //! See crate level documentation for details.
        use super::*;
        pub use super::serialize;

        /// Borrowed Deserializer for `UniCase`.
        ///
        /// Typically, you will use this for any types that borrow data. Example include `&str`.
        /// If you use this with any type that owns data, you will get an error at runtime.
        pub fn deserialize<'de, S, D>(deserializer: D) -> Result<UniCase<S>, D::Error>
        where
            S: From<&'de str> + AsRef<str> + 'de,
            D: Deserializer<'de>,
        {
            struct UniCaseVisitor<S>(PhantomData<S>);

            impl<'de, S: From<&'de str> + AsRef<str>> de::Visitor<'de> for UniCaseVisitor<S> {
                type Value = UniCase<S>;

                fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                    formatter.write_str("a string")
                }

                fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E>
                where
                    E: de::Error,
                {
                    Ok(UniCase::unicode(s.into()))
                }
            }

            deserializer.deserialize_str(UniCaseVisitor(PhantomData))
        }
    }
}

pub mod ascii {
    //! Serialization and Deserialization Implementation for `Ascii` that owns data
    //!
    //! See crate level documentation for details.
    use std::fmt;
    use std::marker::PhantomData;
    use std::str::FromStr;

    use unicase_lib::Ascii;
    use serdelib::{Serializer, Deserializer};
    use serdelib::de;

    /// Straightforward Serialization for UniCase
    pub fn serialize<S, Ser>(value: &Ascii<S>, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
    where
        S: AsRef<str>,
        Ser: Serializer,
    {
        serializer.serialize_str(value.as_ref())
    }

    /// Deserialization for `UniCase<S>` where `S: FromStr + AsRef<str>`
    pub fn deserialize<'de, S, D>(deserializer: D) -> Result<Ascii<S>, D::Error>
    where
        S: FromStr + AsRef<str>,
        D: Deserializer<'de>,
    {
        struct AsciiVisitor<S>(PhantomData<S>);

        impl<'de, S: FromStr + AsRef<str>> de::Visitor<'de> for AsciiVisitor<S> {
            type Value = Ascii<S>;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a string")
            }

            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                FromStr::from_str(s).map_err(|_| E::custom("FromStr conversion failed"))
            }
        }

        deserializer.deserialize_str(AsciiVisitor(PhantomData))
    }

    pub mod borrowed {
        //! Serialization and Deserialization Implementation for `Ascii` that borrows data
        //!
        //! See crate level documentation for details.
        use super::*;
        pub use super::serialize;

        /// Borrowed Deserializer for `Ascii`.
        ///
        /// Typically, you will use this for any types that borrow data. Example include `&str`.
        /// If you use this with any type that owns data, you will get an error at runtime.
        pub fn deserialize<'de, S, D>(deserializer: D) -> Result<Ascii<S>, D::Error>
        where
            S: From<&'de str> + AsRef<str> + 'de,
            D: Deserializer<'de>,
        {
            struct AsciiVisitor<S>(PhantomData<S>);

            impl<'de, S: From<&'de str> + AsRef<str>> de::Visitor<'de> for AsciiVisitor<S> {
                type Value = Ascii<S>;

                fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                    formatter.write_str("a string")
                }

                fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E>
                where
                    E: de::Error,
                {
                    Ok(Ascii::new(s.into()))
                }
            }

            deserializer.deserialize_str(AsciiVisitor(PhantomData))
        }
    }
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use serde_test::{Token, assert_tokens};
    use unicase_lib::{UniCase, Ascii};

    #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
    struct TestUniCase<'a> {
        #[serde(with = "::unicase")]
        owned: UniCase<String>,
        #[serde(borrow, with = "::unicase::borrowed")]
        borrowed_str: UniCase<&'a str>,
        #[serde(borrow, with = "::unicase::borrowed")]
        cow_str: UniCase<Cow<'a, str>>,
    }

    #[derive(Eq, PartialEq, Debug, Serialize, Deserialize)]
    struct TestAscii<'a> {
        #[serde(with = "::ascii")]
        owned: Ascii<String>,
        #[serde(borrow, with = "::ascii::borrowed")]
        borrowed_str: Ascii<&'a str>,
        #[serde(borrow, with = "::ascii::borrowed")]
        cow_str: Ascii<Cow<'a, str>>,
    }

    #[test]
    fn test_unicase_serde() {
        let test = TestUniCase {
            owned: UniCase::unicode("owned string".to_string()),
            borrowed_str: UniCase::unicode("borrowed str"),
            cow_str: UniCase::unicode(Cow::from("Cow str")),
        };

        assert_tokens(
            &test,
            &[
                Token::Struct {
                    name: "TestUniCase",
                    len: 3,
                },
                Token::Str("owned"),
                Token::String("owned string"),
                Token::Str("borrowed_str"),
                Token::BorrowedStr("borrowed str"),
                Token::Str("cow_str"),
                Token::BorrowedStr("Cow str"),
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn test_ascii_serde() {
        let test = TestAscii {
            owned: Ascii::new("owned string".to_string()),
            borrowed_str: Ascii::new("borrowed str"),
            cow_str: Ascii::new(Cow::from("Cow str")),
        };

        assert_tokens(
            &test,
            &[
                Token::Struct {
                    name: "TestAscii",
                    len: 3,
                },
                Token::Str("owned"),
                Token::String("owned string"),
                Token::Str("borrowed_str"),
                Token::BorrowedStr("borrowed str"),
                Token::Str("cow_str"),
                Token::BorrowedStr("Cow str"),
                Token::StructEnd,
            ],
        );
    }

    // The following code tests that custom structs as inputs to UniCase<S> can compile
    use std::str::FromStr;

    #[derive(Eq, PartialEq, Debug)]
    struct CustomStr<'a>(Cow<'a, str>);
    impl<'a> AsRef<str> for CustomStr<'a> {
        fn as_ref(&self) -> &str {
            self.0.as_ref()
        }
    }

    impl<'a> FromStr for CustomStr<'a> {
        type Err = ();
        fn from_str(s: &str) -> Result<Self, Self::Err> {
            Ok(CustomStr(Cow::from(s.to_string())))
        }
    }

    impl<'a> From<&'a str> for CustomStr<'a> {
        fn from(s: &'a str) -> Self {
            CustomStr(Cow::from(s))
        }
    }

    #[derive(Eq, PartialEq, Debug)]
    struct CustomString<'a>(Cow<'a, String>);
    impl<'a> AsRef<str> for CustomString<'a> {
        fn as_ref(&self) -> &str {
            self.0.as_ref()
        }
    }

    impl<'a> FromStr for CustomString<'a> {
        type Err = ();
        fn from_str(s: &str) -> Result<Self, Self::Err> {
            Ok(CustomString(Cow::Owned(s.to_string())))
        }
    }

    impl<'a> From<&'a str> for CustomString<'a> {
        fn from(s: &'a str) -> Self {
            CustomString(Cow::Owned(s.to_string()))
        }
    }

    #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
    struct TestCustomStruct<'a> {
        #[serde(borrow, with = "::unicase::borrowed")]
        test_str: UniCase<CustomStr<'a>>,
        #[serde(borrow, with = "::unicase::borrowed")]
        test_string: UniCase<CustomString<'a>>,
        #[serde(borrow, with = "::unicase::borrowed")]
        test_str_borrowed: UniCase<CustomStr<'a>>,
        #[serde(borrow, with = "::unicase::borrowed")]
        test_string_borrowed: UniCase<CustomString<'a>>,
    }
}