Skip to content

Commit

Permalink
make parse_many return a Result<Vec<Pem>>
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Oct 1, 2021
1 parent b2cfec1 commit 936da99
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion benches/pem_benchmark.rs
Expand Up @@ -30,7 +30,7 @@ fn pem_parse() {
}

fn pem_parse_many() {
pem::parse_many(SAMPLE);
pem::parse_many(SAMPLE).unwrap();
}

fn pem_encode(pem: &pem::Pem) {
Expand Down
22 changes: 14 additions & 8 deletions src/lib.rs
Expand Up @@ -84,7 +84,7 @@
//! -----END CERTIFICATE-----
//! ";
//!
//! let pems = parse_many(SAMPLE);
//! let pems = parse_many(SAMPLE).unwrap();
//! assert_eq!(pems.len(), 2);
//! assert_eq!(pems[0].tag, "INTERMEDIATE CERT");
//! assert_eq!(pems[1].tag, "CERTIFICATE");
Expand Down Expand Up @@ -278,7 +278,7 @@ pub fn parse<B: AsRef<[u8]>>(input: B) -> Result<Pem> {
/// ";
/// let SAMPLE_BYTES: Vec<u8> = SAMPLE.into();
///
/// let pems = parse_many(SAMPLE_BYTES);
/// let pems = parse_many(SAMPLE_BYTES).unwrap();
/// assert_eq!(pems.len(), 2);
/// assert_eq!(pems[0].tag, "INTERMEDIATE CERT");
/// assert_eq!(pems[1].tag, "CERTIFICATE");
Expand Down Expand Up @@ -312,16 +312,16 @@ pub fn parse<B: AsRef<[u8]>>(input: B) -> Result<Pem> {
/// ";
/// let SAMPLE_STRING: Vec<u8> = SAMPLE.into();
///
/// let pems = parse_many(SAMPLE_STRING);
/// let pems = parse_many(SAMPLE_STRING).unwrap();
/// assert_eq!(pems.len(), 2);
/// assert_eq!(pems[0].tag, "INTERMEDIATE CERT");
/// assert_eq!(pems[1].tag, "CERTIFICATE");
/// ```
pub fn parse_many<B: AsRef<[u8]>>(input: B) -> Vec<Pem> {
pub fn parse_many<B: AsRef<[u8]>>(input: B) -> Result<Vec<Pem>> {
// Each time our regex matches a PEM section, we need to decode it.
ASCII_ARMOR
.captures_iter(&input.as_ref())
.filter_map(|caps| Pem::new_from_captures(caps).ok())
.map(|caps| Pem::new_from_captures(caps))
.collect()
}

Expand Down Expand Up @@ -562,12 +562,18 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg

#[test]
fn test_parse_many_works() {
let pems = parse_many(SAMPLE_CRLF);
let pems = parse_many(SAMPLE_CRLF).unwrap();
assert_eq!(pems.len(), 2);
assert_eq!(pems[0].tag, "RSA PRIVATE KEY");
assert_eq!(pems[1].tag, "RSA PUBLIC KEY");
}

#[test]
fn test_parse_many_errors_on_invalid_section() {
let input = SAMPLE_LF.to_owned() + "-----BEGIN -----\n-----END -----";
assert_eq!(parse_many(&input), Err(PemError::MissingBeginTag));
}

#[test]
fn test_encode_empty_contents() {
let pem = Pem {
Expand Down Expand Up @@ -596,7 +602,7 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg

#[test]
fn test_encode_many() {
let pems = parse_many(SAMPLE_CRLF);
let pems = parse_many(SAMPLE_CRLF).unwrap();
let encoded = encode_many(&pems);

assert_eq!(SAMPLE_CRLF, encoded);
Expand All @@ -620,7 +626,7 @@ RzHX0lkJl9Stshd/7Gbt65/QYq+v+xvAeT0CoyIg

#[test]
fn test_encode_many_config() {
let pems = parse_many(SAMPLE_LF);
let pems = parse_many(SAMPLE_LF).unwrap();
let config = EncodeConfig {
line_ending: LineEnding::LF,
};
Expand Down

0 comments on commit 936da99

Please sign in to comment.