Skip to content

match enum struct variant defect? #103

@LittleEntity

Description

@LittleEntity

I try to match the following enum with internal struct variants

pub enum ValidationError {
    LengthOutOfBounds {
        min: usize,
        max: usize,
    },
    InvalidName {
        invalid_chars: Vec<char>,
    },
    UniqueConstraintViolation,
}

with

@use crate::ValidationError::{self, *};

@(error: &ValidationError)

@match error {
    UniqueConstraintViolation => {
        This name is taken.
    }
    LengthOutOfBounds { min, max } => {
        This field must contain at min @min and at max @max characters.
    }
    InvalidName { invalid_chars } => {
        @format!("The name must not contain any of the following characters: {:?}", invalid_chars)
    }
}

The error message is:

warning: Template parse error in "template_src/validation_error_display_part.rs.html":
warning:    5:@match error {
warning:      ^ Error in expression starting here:
warning:    5:@match error {
warning:             ^ Error in match expression:
warning:    8:    }
warning:           ^ Error in match arm starting here:

this works:

@use crate::ValidationError::{self, *};

@(error: &ValidationError)

@match error {
    UniqueConstraintViolation(value) => {
        This name is taken.
    }
    _ => {
        other error
    }
}

In rust this works:

impl<'s> std::fmt::Display for ValidationError<'s> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        use ValidationError::*;
        match self {
            LengthOutOfBounds { min, max } => {
                write!(
                    f,
                    "This field must contain at min {} and at max {} characters.",
                    min, max
                )
            }
            InvalidName {
                invalid_chars,
            } => {
                write!(
                    f,
                    "The name must not contain any of the following characters: {:?}",
                    invalid_chars
                )
            }
            UniqueConstraintViolation => {
                write!(f, "This name is taken.")
            }
        }
    }
}

Am I doing something wrong? Is this a defect of the library?

I enjoy using ructe 🦀 thankyou for sharing 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions