Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #835 from gtk-rs/pick
Browse files Browse the repository at this point in the history
Backport #830
  • Loading branch information
GuillaumeGomez authored Jul 29, 2023
2 parents 41d7fec + eba4b28 commit 46287d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
1 change: 0 additions & 1 deletion gtk3-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 33 additions & 7 deletions gtk3-macros/src/attribute_parser.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -45,13 +44,40 @@ impl Parse for TemplateSource {
}
}

pub fn parse_template_source(input: &DeriveInput) -> Result<TemplateSource> {
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<TemplateSource, ParseTemplateSourceError> {
input
.attrs
.iter()
.find(|a| a.path().is_ident("template"))
.ok_or(ParseTemplateSourceError::MissingAttribute)?
.parse_args()
.map_err(ParseTemplateSourceError::Parse)
}

pub enum FieldAttributeArg {
Expand Down

0 comments on commit 46287d0

Please sign in to comment.