~kmaasrud/pimalaya-webfinger

e46b565b75c6fda73d6eead69aca0d4c13f17583 — Knut Magnus Aasrud 10 months ago 6056890
fix: add proper error handling to Rel parsing
1 files changed, 16 insertions(+), 4 deletions(-)

M src/rel.rs
M src/rel.rs => src/rel.rs +16 -4
@@ 1,5 1,17 @@
use std::fmt::Display;

use serde::{Deserialize, Serialize};

pub struct UnknownRelError<'a> {
    rel: &'a str,
}

impl<'a> Display for UnknownRelError<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "unknown relation type {}", self.rel)
    }
}

macro_rules! rel {
    { $( ($variant:ident, $name:literal), )* } => {
        /// A link relation type as defined in [RFC 8288](https://www.rfc-editor.org/rfc/rfc8288.html).


@@ 11,15 23,15 @@ macro_rules! rel {
            ),*
        }

        impl TryFrom<&str> for Rel {
            type Error = ();
        impl<'a> TryFrom<&'a str> for Rel {
            type Error = UnknownRelError<'a>;

            fn try_from(value: &str) -> Result<Self, Self::Error> {
            fn try_from(value: &'a str) -> Result<Self, Self::Error> {
                match value {
                    $(
                        $name => Ok(Self::$variant),
                    )*
                    _ => Err(()),
                    rel => Err(UnknownRelError { rel }),
                }
            }
        }