improve error messages in parse_rfc3339

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2020-10-05 09:16:25 +02:00
parent 307cb04795
commit 9fa9c76a9c

View File

@ -201,8 +201,8 @@ pub fn epoch_to_rfc3339(epoch: i64) -> Result<String, Error> {
} }
/// Parse RFC3339 into Unix epoch /// Parse RFC3339 into Unix epoch
pub fn parse_rfc3339(i: &str) -> Result<i64, Error> { pub fn parse_rfc3339(input_str: &str) -> Result<i64, Error> {
let input = i.as_bytes(); let input = input_str.as_bytes();
let expect = |pos: usize, c: u8| { let expect = |pos: usize, c: u8| {
if input[pos] != c { if input[pos] != c {
@ -227,24 +227,24 @@ pub fn parse_rfc3339(i: &str) -> Result<i64, Error> {
}; };
crate::try_block!({ crate::try_block!({
if i.len() < 20 || i.len() > 25 { if input.len() < 20 || input.len() > 25 {
bail!("wrong length"); bail!("timestamp of unexpected length");
} }
let tz = input[19]; let tz = input[19];
match tz { match tz {
b'Z' => { b'Z' => {
if i.len() != 20 { if input.len() != 20 {
bail!("wrong length"); bail!("unexpected length in UTC timestamp");
} }
} }
b'+' | b'-' => { b'+' | b'-' => {
if i.len() != 25 { if input.len() != 25 {
bail!("wrong length"); bail!("unexpected length in timestamp");
} }
} }
_ => bail!("got unknown timezone indicator"), _ => bail!("unexpected timezone indicator"),
} }
let mut tm = TmEditor::new(true); let mut tm = TmEditor::new(true);
@ -282,7 +282,7 @@ pub fn parse_rfc3339(i: &str) -> Result<i64, Error> {
Ok(epoch) Ok(epoch)
}) })
.map_err(|err| format_err!("parse_rfc_3339 failed - {}", err)) .map_err(|err| format_err!("failed to parse rfc3339 timestamp ({:?}) - {}", input_str, err))
} }
#[test] #[test]