From 936da997944f3f04f73c05bf7662dba8523be6b8 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 1 Oct 2021 17:05:28 +0200 Subject: [PATCH] make `parse_many` return a `Result>` --- benches/pem_benchmark.rs | 2 +- src/lib.rs | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/benches/pem_benchmark.rs b/benches/pem_benchmark.rs index 9b4ff45..3f29a29 100644 --- a/benches/pem_benchmark.rs +++ b/benches/pem_benchmark.rs @@ -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) { diff --git a/src/lib.rs b/src/lib.rs index 8ac143a..1793a85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); @@ -278,7 +278,7 @@ pub fn parse>(input: B) -> Result { /// "; /// let SAMPLE_BYTES: Vec = 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"); @@ -312,16 +312,16 @@ pub fn parse>(input: B) -> Result { /// "; /// let SAMPLE_STRING: Vec = 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>(input: B) -> Vec { +pub fn parse_many>(input: B) -> Result> { // 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() } @@ -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 { @@ -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); @@ -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, };