From 162eee27742e00b35825da75bc5da9edcdb372c4 Mon Sep 17 00:00:00 2001 From: Fina Wilke Date: Mon, 9 Sep 2024 18:00:38 +0200 Subject: [PATCH] uri: Fix URI error codes --- src/core.rs | 4 ++++ src/uri.rs | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core.rs b/src/core.rs index e1b7fc8b..f9433131 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1026,6 +1026,9 @@ impl FromStr for Password { #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Copy, derive_more::Display, Error)] #[non_exhaustive] pub enum ParseCodeError { + /// The code is empty + #[display("The code is empty")] + Empty, /// A code must contain at least one '-' to separate nameplate from password #[display("A code must contain at least one '-' to separate nameplate from password")] SeparatorMissing, @@ -1153,6 +1156,7 @@ impl FromStr for Code { Ok(Self(format!("{}-{}", nameplate, password))) }, + None if s.is_empty() => Err(ParseCodeError::Empty), None => Err(ParseCodeError::SeparatorMissing), } } diff --git a/src/uri.rs b/src/uri.rs index 6f5e9f83..bd124ef5 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -105,7 +105,15 @@ impl TryFrom<&url::Url> for WormholeTransferUri { }; let code: Code = percent_encoding::percent_decode_str(url.path()) .decode_utf8()? - .parse()?; + .parse() + .map_err(|e| { + // TODO: Remove for 0.8 + if matches!(e, ParseCodeError::Empty) { + ParseError::MissingCode + } else { + e.into() + } + })?; Ok(WormholeTransferUri { code,