summaryrefslogtreecommitdiff
path: root/src/diagnostics/non_alphabetic_attribute_name.rs
blob: 1cb31d9c012c7f5c660b7b3816b1e21823bb437f (plain)
use miette::Diagnostic;
use thiserror::Error;

use crate::{
	diagnostics::TokenDiagnostic,
	tokenize::{AttributeToken, AttributeTokenType, Span},
};

#[derive(Error, Debug, Diagnostic)]
#[error("Attribute names must be alphabetic")]
#[diagnostic(code(NonAlphabeticAttributeName), severity(Error))]
pub struct NonAlphabeticAttributeName {
	#[label(
		primary,
		"attribute names must only contain uppercase and lowercase letters"
	)]
	attribute_name: Span,
}

impl TokenDiagnostic for NonAlphabeticAttributeName {
	fn handle_attribute_token(token: &AttributeToken<'_>) -> Option<Self> {
		let AttributeTokenType::Identifier { lowercase_name, .. } = token.ty else {
			return None;
		};

		if lowercase_name
			.chars()
			.all(|char| char.is_ascii_alphabetic())
		{
			return None;
		}

		Some(Self {
			attribute_name: token.span,
		})
	}
}