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

Refactor(compiler-base): Replaced DiagnosticStyle in trait Component with generic T: Clone + PartialEq + Eq + Style. #152

Merged
merged 5 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kclvm/compiler_base/error/src/diagnostic/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub enum Label {
Help,
}

impl Component for Label {
impl Component<DiagnosticStyle> for Label {
fn format(&self, sb: &mut StyledBuffer<DiagnosticStyle>) {
let (text, style, code) = match self {
Label::Error(ecode) => ("error", DiagnosticStyle::NeedFix, Some(ecode)),
Expand All @@ -56,7 +56,7 @@ impl Component for Label {
/// `String` can be considered as a component of diagnostic.
///
/// The result of component `String` rendering is a `String` who has no style.
impl Component for String {
impl Component<DiagnosticStyle> for String {
zong-zhe marked this conversation as resolved.
Show resolved Hide resolved
fn format(&self, sb: &mut StyledBuffer<DiagnosticStyle>) {
sb.appendl(&self, None);
}
Expand Down
38 changes: 25 additions & 13 deletions kclvm/compiler_base/error/src/diagnostic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use self::style::DiagnosticStyle;
pub use rustc_errors::styled_buffer::StyledBuffer;
use rustc_errors::Style;

pub mod components;
pub mod style;
Expand All @@ -8,7 +8,10 @@ pub mod style;
mod tests;

/// 'Component' specifies the method `format()` that all diagnostic components should implement.
pub trait Component {
pub trait Component<T>
where
T: Clone + PartialEq + Eq + Style,
{
/// `format()` formats components into `StyledString` and saves them in `StyledBuffer`.
///
/// # Examples
Expand All @@ -18,15 +21,15 @@ pub trait Component {
/// text: String
/// }
///
/// impl Component for ComponentWithStyleLogo {
/// impl Component<DiagnosticStyle> for ComponentWithStyleLogo {
/// fn format(&self, sb: &mut StyledBuffer<DiagnosticStyle>) {
/// // set style
/// sb.pushs(&self.text, Some(DiagnosticStyle::Logo));
/// }
/// }
///
/// ```
fn format(&self, sb: &mut StyledBuffer<DiagnosticStyle>);
fn format(&self, sb: &mut StyledBuffer<T>);
}

/// `Diagnostic` is a collection of various components,
Expand Down Expand Up @@ -67,7 +70,7 @@ pub trait Component {
///
/// // "error" - DiagnosticStyle::NeedFix
/// // "[E3033]" - DiagnosticStyle::Helpful
/// // ": this is an error!" - DiagnosticStyle::NoStyle
/// // ": this is an error!" - None
///
/// // `DiagnosticStyle` can be rendered into different text colors and formats when diaplaying.
///
Expand All @@ -78,28 +81,37 @@ pub trait Component {
///
/// assert_eq!(result.get(0).unwrap().get(0).unwrap().style, Some(DiagnosticStyle::NeedFix));
/// assert_eq!(result.get(0).unwrap().get(1).unwrap().style, Some(DiagnosticStyle::Helpful));
/// assert_eq!(result.get(0).unwrap().get(2).unwrap().style, Some(DiagnosticStyle::NoStyle));
/// assert_eq!(result.get(0).unwrap().get(2).unwrap().style, None);
/// ```
pub struct Diagnostic {
components: Vec<Box<dyn Component>>,
pub struct Diagnostic<T>
where
T: Clone + PartialEq + Eq + Style,
{
components: Vec<Box<dyn Component<T>>>,
}

impl Diagnostic {
impl<T> Diagnostic<T>
where
T: Clone + PartialEq + Eq + Style,
{
pub fn new() -> Self {
Diagnostic { components: vec![] }
}

pub fn append_component(&mut self, component: Box<dyn Component>) {
pub fn append_component(&mut self, component: Box<dyn Component<T>>) {
self.components.push(component);
}

pub fn prepend_component(&mut self, component: Box<dyn Component>) {
pub fn prepend_component(&mut self, component: Box<dyn Component<T>>) {
self.components.insert(0, component);
}
}

impl Component for Diagnostic {
fn format(&self, sb: &mut StyledBuffer<DiagnosticStyle>) {
impl<T> Component<T> for Diagnostic<T>
where
T: Clone + PartialEq + Eq + Style,
{
fn format(&self, sb: &mut StyledBuffer<T>) {
for component in &self.components {
component.format(sb);
}
Expand Down