From a6974c99d39fcbaf3fb9ed38428b21e0301f3602 Mon Sep 17 00:00:00 2001 From: Pyfisch Date: Sat, 2 May 2015 16:40:53 +0200 Subject: [PATCH] refactor(headers): Fail to parse single value header values A single value header value can't be "", so `from_one_raw_str()` now returns `None` on empty values. This makes custom checks in headers obsolete. BREAKING CHANGE: `from_one_raw_str()` returns `None` on empty values. --- src/header/common/pragma.rs | 2 ++ src/header/parsing.rs | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/header/common/pragma.rs b/src/header/common/pragma.rs index 23dbae27c5..a8ddaf0461 100644 --- a/src/header/common/pragma.rs +++ b/src/header/common/pragma.rs @@ -57,4 +57,6 @@ fn test_parse_header() { let c: Pragma = Header::parse_header([b"FoObar".to_vec()].as_ref()).unwrap(); let d = Pragma::Ext("FoObar".to_string()); assert_eq!(c, d); + let e: Option = Header::parse_header([b"".to_vec()].as_ref()); + assert_eq!(e, None); } diff --git a/src/header/parsing.rs b/src/header/parsing.rs index 07ded6c5cb..957a3f9b57 100644 --- a/src/header/parsing.rs +++ b/src/header/parsing.rs @@ -9,10 +9,12 @@ pub fn from_one_raw_str(raw: &[Vec]) -> Option { return None; } // we JUST checked that raw.len() == 1, so raw[0] WILL exist. - match str::from_utf8(&raw[0][..]) { - Ok(s) => str::FromStr::from_str(s).ok(), - Err(_) => None + if let Ok(s) = str::from_utf8(&raw[0][..]) { + if s != "" { + return str::FromStr::from_str(s).ok(); + } } + None } /// Reads a comma-delimited raw header into a Vec.