From eba4b28eee02a63cc386a1c2b7f9c159513e9487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Fri, 21 Jul 2023 19:50:37 +0200 Subject: [PATCH] Remove `anyhow` dependency --- gtk3-macros/Cargo.toml | 1 - gtk3-macros/src/attribute_parser.rs | 40 ++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/gtk3-macros/Cargo.toml b/gtk3-macros/Cargo.toml index fb798b6132b..11ab30488f3 100644 --- a/gtk3-macros/Cargo.toml +++ b/gtk3-macros/Cargo.toml @@ -20,7 +20,6 @@ rust-version = "1.70" proc-macro = true [dependencies] -anyhow = "1.0" proc-macro-error = "1.0" proc-macro2 = "1.0" quote = "1.0" diff --git a/gtk3-macros/src/attribute_parser.rs b/gtk3-macros/src/attribute_parser.rs index a2a21e946fd..15d0525f003 100644 --- a/gtk3-macros/src/attribute_parser.rs +++ b/gtk3-macros/src/attribute_parser.rs @@ -1,6 +1,5 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use anyhow::{bail, Result}; use proc_macro2::Span; use syn::spanned::Spanned; use syn::{ @@ -45,13 +44,40 @@ impl Parse for TemplateSource { } } -pub fn parse_template_source(input: &DeriveInput) -> Result { - let attr = match input.attrs.iter().find(|a| a.path().is_ident("template")) { - Some(attr) => attr, - _ => bail!("Missing 'template' attribute"), - }; +#[derive(Debug)] +pub enum ParseTemplateSourceError { + MissingAttribute, + Parse(syn::Error), +} + +impl std::fmt::Display for ParseTemplateSourceError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::MissingAttribute => write!(f, "Missing 'template' attribute"), + Self::Parse(err) => write!(f, "{}", err), + } + } +} + +impl std::error::Error for ParseTemplateSourceError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::MissingAttribute => None, + Self::Parse(err) => Some(err), + } + } +} - Ok(attr.parse_args()?) +pub fn parse_template_source( + input: &DeriveInput, +) -> Result { + input + .attrs + .iter() + .find(|a| a.path().is_ident("template")) + .ok_or(ParseTemplateSourceError::MissingAttribute)? + .parse_args() + .map_err(ParseTemplateSourceError::Parse) } pub enum FieldAttributeArg {