Skip to content

Commit

Permalink
Support optional serde feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpfs committed Jul 12, 2022
1 parent 2ebf2b1 commit 79cf854
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ rust-version = "1.34.2"

[dependencies]
base64 = "0.13.0"
serde = { version = "1", optional = true, features = ["serde_derive"] }

[dev-dependencies]
criterion = "0.3.0"
serde_json = "1"

[[bench]]
name = "pem_benchmark"
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
unused_qualifications
)]

#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};

mod errors;
mod parser;
use parser::{parse_captures, parse_captures_iter, Captures};
Expand Down Expand Up @@ -131,6 +134,7 @@ pub struct EncodeConfig {

/// A representation of Pem-encoded data
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Pem {
/// The tag extracted from the Pem-encoded data
pub tag: String,
Expand Down Expand Up @@ -613,4 +617,16 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg

assert_eq!(SAMPLE_LF, encoded);
}

#[cfg(feature = "serde")]
#[test]
fn test_serde() {
let pem = Pem {
tag: String::from("Mock tag"),
contents: "Mock contents".as_bytes().to_vec(),
};
let value = serde_json::to_string_pretty(&pem).unwrap();
let result = serde_json::from_str(&value).unwrap();
assert_eq!(pem, result);
}
}

0 comments on commit 79cf854

Please sign in to comment.