Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add struct for dynamic creation of diagnostics #259

Closed
gavrilikhin-d opened this issue May 2, 2023 · 2 comments · Fixed by #262
Closed

Add struct for dynamic creation of diagnostics #259

gavrilikhin-d opened this issue May 2, 2023 · 2 comments · Fixed by #262
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@gavrilikhin-d
Copy link
Contributor

gavrilikhin-d commented May 2, 2023

It would be cool to have builtin way to create dynamic diagnostics in miette, instead of writing your own

#[derive(Debug, Error, PartialEq, Eq, Clone)]
pub struct CustomError {
    pub severity: Severity,
    pub message: String,
    pub code: Option<String>,
    pub help: Option<String>,
    pub labels: Option<Vec<LabeledSpan>>,
    pub url: Option<String>,
}

impl Display for CustomError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl Diagnostic for CustomError {
    fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.code.as_ref().map(|s| Box::new(s) as Box<dyn Display>)
    }
    fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.help.as_ref().map(|s| Box::new(s) as Box<dyn Display>)
    }
    fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
        self.labels
            .as_ref()
            .map(|labels| Box::new(labels.iter().cloned()) as Box<dyn Iterator<Item = LabeledSpan>>)
    }
    fn severity(&self) -> Option<miette::Severity> {
        Some(self.severity)
    }
    fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        self.url.as_ref().map(|s| Box::new(s) as Box<dyn Display>)
    }
}
@zkat zkat added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels May 5, 2023
@zkat
Copy link
Owner

zkat commented May 5, 2023

I've actually talked with folks in the past about including something like this as a convenience! I'd totally take this as a PR :)

(you'll need to manually implement std::error::Error, ofc. derive(Error) is a thiserror thing and I don't want that dependency)

@gavrilikhin-d
Copy link
Contributor Author

What name should it have? My suggestions are:

  • DynamicDiagnostic
  • CustomDiagnostic
  • MietteDiagnostic

@zkat zkat linked a pull request May 13, 2023 that will close this issue
@zkat zkat closed this as completed in #262 May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants