Skip to content

Commit

Permalink
refactor: Use Cow rather than a Box<dyn Display>
Browse files Browse the repository at this point in the history
Since we're already behind a method, let's rely on the implementors to
specify the dynamic display and use Cow for common case where the code
is well-known and static, i.e. "TS1234".
  • Loading branch information
Xanewok committed May 15, 2024
1 parent 66a733f commit 578c340
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 21 deletions.
6 changes: 4 additions & 2 deletions crates/codegen/parser/runtime/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt::Display;

use crate::text_index::TextRange;

Expand All @@ -13,7 +13,9 @@ pub enum Severity {

pub trait Diagnostic: Error {
fn range(&self) -> TextRange;
fn code(&self) -> Box<dyn Display>;
fn code(&self) -> Option<Cow<'_, str>> {
None
}
fn severity(&self) -> Severity;
fn message(&self) -> String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ impl Diagnostic {

#[napi]
pub fn code(&self) -> String {
self.0.code().to_string()
self.0.code().unwrap_or_default().into_owned()
}
}
4 changes: 0 additions & 4 deletions crates/codegen/parser/runtime/src/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ impl Diagnostic for ParseError {
self.text_range.clone()
}

fn code(&self) -> Box<dyn std::fmt::Display> {
Box::new("ParseError")
}

fn severity(&self) -> diagnostic::Severity {
diagnostic::Severity::Error
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 578c340

Please sign in to comment.