|
19 | 19 | //!
|
20 | 20 | //! ```toml
|
21 | 21 | //! [dependencies]
|
22 |
| -//! pem = "0.8" |
| 22 | +//! pem = "1" |
23 | 23 | //! ```
|
24 | 24 | //!
|
25 | 25 | //! and this to your crate root:
|
|
28 | 28 | //! extern crate pem;
|
29 | 29 | //! ```
|
30 | 30 | //!
|
| 31 | +//! Using the `serde` feature will implement the serde traits for |
| 32 | +//! the `Pem` struct. |
| 33 | +//! |
31 | 34 | //! # Example: parse a single chunk of PEM-encoded text
|
32 | 35 | //!
|
33 | 36 | //! Generally, PEM-encoded files contain a single chunk of PEM-encoded
|
@@ -421,6 +424,51 @@ pub fn encode_many_config(pems: &[Pem], config: EncodeConfig) -> String {
|
421 | 424 | .join(line_ending)
|
422 | 425 | }
|
423 | 426 |
|
| 427 | +#[cfg(feature = "serde")] |
| 428 | +mod serde_impl { |
| 429 | + use super::{encode, parse, Pem}; |
| 430 | + use serde::{ |
| 431 | + de::{Error, Visitor}, |
| 432 | + Deserialize, Serialize, |
| 433 | + }; |
| 434 | + use std::fmt; |
| 435 | + |
| 436 | + impl Serialize for Pem { |
| 437 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 438 | + where |
| 439 | + S: serde::Serializer, |
| 440 | + { |
| 441 | + serializer.serialize_str(&encode(self)) |
| 442 | + } |
| 443 | + } |
| 444 | + |
| 445 | + struct PemVisitor; |
| 446 | + |
| 447 | + impl<'de> Visitor<'de> for PemVisitor { |
| 448 | + type Value = Pem; |
| 449 | + |
| 450 | + fn expecting(&self, _formatter: &mut fmt::Formatter) -> fmt::Result { |
| 451 | + Ok(()) |
| 452 | + } |
| 453 | + |
| 454 | + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 455 | + where |
| 456 | + E: Error, |
| 457 | + { |
| 458 | + parse(v).map_err(Error::custom) |
| 459 | + } |
| 460 | + } |
| 461 | + |
| 462 | + impl<'de> Deserialize<'de> for Pem { |
| 463 | + fn deserialize<D>(deserializer: D) -> Result<Pem, D::Error> |
| 464 | + where |
| 465 | + D: serde::Deserializer<'de>, |
| 466 | + { |
| 467 | + deserializer.deserialize_str(PemVisitor) |
| 468 | + } |
| 469 | + } |
| 470 | +} |
| 471 | + |
424 | 472 | #[cfg(test)]
|
425 | 473 | mod test {
|
426 | 474 | use super::*;
|
@@ -613,4 +661,16 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg
|
613 | 661 |
|
614 | 662 | assert_eq!(SAMPLE_LF, encoded);
|
615 | 663 | }
|
| 664 | + |
| 665 | + #[cfg(feature = "serde")] |
| 666 | + #[test] |
| 667 | + fn test_serde() { |
| 668 | + let pem = Pem { |
| 669 | + tag: String::from("Mock tag"), |
| 670 | + contents: "Mock contents".as_bytes().to_vec(), |
| 671 | + }; |
| 672 | + let value = serde_json::to_string_pretty(&pem).unwrap(); |
| 673 | + let result = serde_json::from_str(&value).unwrap(); |
| 674 | + assert_eq!(pem, result); |
| 675 | + } |
616 | 676 | }
|
0 commit comments