From cd94b41d619a27a2a037113f862cdd4b1d1b37e6 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 12 Jul 2021 11:44:48 +0100 Subject: [PATCH] zcash_address: Introduce UA-specific parser error type --- components/zcash_address/src/encoding.rs | 8 ++++++++ components/zcash_address/src/kind/unified.rs | 21 +++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/components/zcash_address/src/encoding.rs b/components/zcash_address/src/encoding.rs index ab7d7768b..aca51dce8 100644 --- a/components/zcash_address/src/encoding.rs +++ b/components/zcash_address/src/encoding.rs @@ -13,6 +13,14 @@ pub enum ParseError { NotZcash, } +impl From for ParseError { + fn from(e: unified::ParseError) -> Self { + match e { + unified::ParseError::InvalidEncoding => Self::InvalidEncoding, + } + } +} + impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { diff --git a/components/zcash_address/src/kind/unified.rs b/components/zcash_address/src/kind/unified.rs index c734ceecf..0a810b0ee 100644 --- a/components/zcash_address/src/kind/unified.rs +++ b/components/zcash_address/src/kind/unified.rs @@ -1,7 +1,9 @@ use std::convert::{TryFrom, TryInto}; +use std::error::Error; +use std::fmt; use std::iter; -use crate::{kind, ParseError}; +use crate::kind; mod f4jumble; @@ -57,6 +59,23 @@ impl From for u8 { } } +/// An error while attempting to parse a string as a Zcash address. +#[derive(Debug, PartialEq)] +pub enum ParseError { + /// The string is an invalid encoding. + InvalidEncoding, +} + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ParseError::InvalidEncoding => write!(f, "Invalid encoding"), + } + } +} + +impl Error for ParseError {} + /// The set of known Receivers for Unified Addresses. /// /// This enum is an internal-only type, and is maintained in preference order, so that the