#
enum_ordinalize_derive/
panic.rs

    1use core::fmt::{self, Display, Formatter};
    2
    3use proc_macro2::Span;
    4use syn::Ident;
    5
    6struct DisplayStringSlice<'a>(&'a [&'static str]);
    7
    8impl<'a> Display for DisplayStringSlice<'a> {
    9    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    10        for &s in self.0 {
    11            f.write_str("\n    ")?;
    12            f.write_str(s)?;
    13        }
    14
    15        Ok(())
    16    }
    17}
    18
    19#[inline]
    20pub(crate) fn not_enum(span: Span) -> syn::Error {
    21    syn::Error::new(span, "only enums can be ordinalized")
    22}
    23
    24#[inline]
    25pub(crate) fn no_variant(span: Span) -> syn::Error {
    26    syn::Error::new(span, "an ordinalized enum needs to have at least one variant")
    27}
    28
    29#[inline]
    30pub(crate) fn not_unit_variant(span: Span) -> syn::Error {
    31    syn::Error::new(span, "an ordinalized enum can only have unit variants")
    32}
    33
    34#[inline]
    35pub(crate) fn unsupported_discriminant(span: Span) -> syn::Error {
    36    syn::Error::new(
    37        span,
    38        "the discriminant of a variant of an ordinalized enum needs to be a legal literal \
    39         integer, a constant variable/function or a constant expression",
    40    )
    41}
    42#[inline]
    43pub(crate) fn constant_variable_on_non_determined_size_enum(span: Span) -> syn::Error {
    44    syn::Error::new(
    45        span,
    46        "the discriminant of a variant can be assigned not to a literal integer only when the \
    47         ordinalized enum is using the `repr` attribute to determine it's size before compilation",
    48    )
    49}
    50
    51#[inline]
    52pub fn list_attribute_usage(name: &Ident, span: Span) -> syn::Error {
    53    syn::Error::new(span, format!("the `{name}` attribute should be a list"))
    54}
    55
    56#[inline]
    57pub(crate) fn bool_attribute_usage(name: &Ident, span: Span) -> syn::Error {
    58    syn::Error::new(
    59        span,
    60        format!("the `{name}` attribute should be a name-value pair. The value type is boolean"),
    61    )
    62}
    63
    64#[inline]
    65pub(crate) fn sub_attributes_for_ordinalize(span: Span) -> syn::Error {
    66    syn::Error::new(
    67        span,
    68        format!(
    69            "available sub-attributes for the `ordinalize` attribute:{}",
    70            DisplayStringSlice(&[
    71                "impl_trait",
    72                "variant_count",
    73                "variants",
    74                "values",
    75                "ordinal",
    76                "from_ordinal_unsafe",
    77                "from_ordinal",
    78            ])
    79        ),
    80    )
    81}