` opened at (1:1) was never closed"));
- assert!(tree_inspect.contains("MISSING_CLOSING_TAG_ERROR"));
+ assert!(tree_inspect.contains("MissingClosingTagError"));
assert!(tree_inspect.contains("Opening tag `
` at (1:1) doesn't have a matching closing tag"));
}
@@ -18,7 +18,7 @@ fn test_tag_names_mismatch_error() {
let result = parse(source).unwrap();
let tree_inspect = result.tree_inspect();
- assert!(tree_inspect.contains("TAG_NAMES_MISMATCH_ERROR"));
+ assert!(tree_inspect.contains("TagNamesMismatchError"));
assert!(tree_inspect.contains("Opening tag `
` at (1:1) closed with ``"));
}
diff --git a/templates/rust/src/errors.rs.erb b/templates/rust/src/errors.rs.erb
index dbd859601..447120543 100644
--- a/templates/rust/src/errors.rs.erb
+++ b/templates/rust/src/errors.rs.erb
@@ -1,4 +1,5 @@
use crate::{Location, Token};
+use colored::*;
fn escape_string(s: &str) -> String {
s.replace('\\', "\\\\")
@@ -9,25 +10,25 @@ fn escape_string(s: &str) -> String {
}
fn format_string_value(value: &str) -> String {
- format!("\"{}\"", escape_string(value))
+ format!("\"{}\"", escape_string(value)).green().to_string()
}
fn format_token_value(token: &Option) -> String {
- token.as_ref().map(|t| t.tree_inspect()).unwrap_or_else(|| "∅".to_string())
+ token.as_ref().map(|t| t.tree_inspect()).unwrap_or_else(|| "∅".magenta().to_string())
}
fn format_token_type_value(token_type: &Option) -> String {
- token_type.as_ref().map(|t| format!("\"{}\"", t)).unwrap_or_else(|| "∅".to_string())
+ token_type.as_ref().map(|t| format!("\"{}\"", t).green().to_string()).unwrap_or_else(|| "∅".magenta().to_string())
}
#[allow(dead_code)]
fn format_position_value(position: &Option) -> String {
- position.as_ref().map(|p| p.to_string()).unwrap_or_else(|| "∅".to_string())
+ position.as_ref().map(|p| p.to_string()).unwrap_or_else(|| "∅".magenta().to_string())
}
#[allow(dead_code)]
fn format_size_value(value: usize) -> String {
- value.to_string()
+ value.to_string().magenta().bold().to_string()
}
#[derive(Debug, Clone, PartialEq)]
@@ -167,22 +168,26 @@ impl <%= error.name %> {
pub fn tree_inspect(&self) -> String {
let mut output = String::new();
- output.push_str(&format!("@ <%= error.type %> {}\n", self.location));
+ output.push_str(&format!("{} {} {}\n",
+ "@".white(),
+ "<%= error.name %>".red().bold(),
+ format!("(location: {})", self.location).dimmed()
+ ));
<%- symbol = error.fields.any? ? "├──" : "└──" -%>
- output.push_str(&format!("<%= symbol %> message: {}\n", format_string_value(&self.message)));
+ output.push_str(&format!("{} {}: {}\n", "<%= symbol %>".white(), "message".white(), format_string_value(&self.message)));
<%- error.fields.each do |field| -%>
<%- symbol = error.fields.last == field ? "└──" : "├──" -%>
<%- case field -%>
<%- when Herb::Template::StringField -%>
- output.push_str(&format!("<%= symbol %> <%= field.name %>: {}\n", format_string_value(&self.<%= field.name %>)));
+ output.push_str(&format!("{} {}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_string_value(&self.<%= field.name %>)));
<%- when Herb::Template::TokenField -%>
- output.push_str(&format!("<%= symbol %> <%= field.name %>: {}\n", format_token_value(&self.<%= field.name %>)));
+ output.push_str(&format!("{} {}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_token_value(&self.<%= field.name %>)));
<%- when Herb::Template::TokenTypeField -%>
- output.push_str(&format!("<%= symbol %> <%= field.name %>: {}\n", format_token_type_value(&self.<%= field.name %>)));
+ output.push_str(&format!("{} {}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_token_type_value(&self.<%= field.name %>)));
<%- when Herb::Template::PositionField -%>
- output.push_str(&format!("<%= symbol %> <%= field.name %>: {}\n", format_position_value(&self.<%= field.name %>)));
+ output.push_str(&format!("{} {}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_position_value(&self.<%= field.name %>)));
<%- when Herb::Template::SizeTField -%>
- output.push_str(&format!("<%= symbol %> <%= field.name %>: {}\n", format_size_value(self.<%= field.name %>)));
+ output.push_str(&format!("{} {}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_size_value(self.<%= field.name %>)));
<%- end -%>
<%- end -%>
diff --git a/templates/rust/src/nodes.rs.erb b/templates/rust/src/nodes.rs.erb
index c68784233..3bc8b4123 100644
--- a/templates/rust/src/nodes.rs.erb
+++ b/templates/rust/src/nodes.rs.erb
@@ -1,5 +1,6 @@
use crate::errors::{AnyError, ErrorNode};
use crate::{Location, Token};
+use colored::*;
fn escape_string(s: &str) -> String {
s.replace('\\', "\\\\")
@@ -10,15 +11,15 @@ fn escape_string(s: &str) -> String {
}
fn format_string_value(value: &str) -> String {
- format!("\"{}\"", escape_string(value))
+ format!("\"{}\"", escape_string(value)).green().to_string()
}
fn format_token_value(token: &Option) -> String {
- token.as_ref().map(|t| t.tree_inspect()).unwrap_or_else(|| "∅".to_string())
+ token.as_ref().map(|t| t.tree_inspect()).unwrap_or_else(|| "∅".magenta().to_string())
}
fn format_bool_value(value: bool) -> String {
- value.to_string()
+ value.to_string().magenta().bold().to_string()
}
fn format_node_value(node: &Option>, prefix: &str, add_spacing: bool) -> String {
@@ -31,7 +32,7 @@ fn format_node_value(node: &Option>, prefix: &str, add_
}
output
} else {
- "∅\n".to_string()
+ format!("{}\n", "∅".magenta())
}
}
@@ -49,22 +50,24 @@ fn inspect_errors(errors: &[AnyError], prefix: &str) -> String {
}
let mut output = String::new();
- output.push_str(&format!("├── errors: ({} error{})\n",
- errors.len(),
- if errors.len() == 1 { "" } else { "s" }
+ output.push_str(&format!("{} {}: {}\n",
+ "├──".white(),
+ "errors".red().bold(),
+ format!("({} error{})", errors.len(), if errors.len() == 1 { "" } else { "s" }).dimmed()
));
for (i, error) in errors.iter().enumerate() {
let is_last = i == errors.len() - 1;
let symbol = if is_last { "└── " } else { "├── " };
- let next_prefix = if is_last { " " } else { "│ " };
+ let next_prefix_str = if is_last { " " } else { "│ " };
+ let next_prefix = next_prefix_str.white().to_string();
let tree = error.tree_inspect();
let tree = tree.trim_end_matches('\n');
- output.push_str(&format!("{}{}{}\n", prefix, symbol, tree.replace('\n', &format!("\n{}{}", prefix, next_prefix))));
+ output.push_str(&format!("{}{}{}\n", prefix, symbol.white(), tree.replace('\n', &format!("\n{}{}", prefix, next_prefix))));
if !is_last {
- output.push_str(&format!("{}│ \n", prefix));
+ output.push_str(&format!("{}{}\n", prefix, "│ ".white()));
}
}
@@ -74,28 +77,29 @@ fn inspect_errors(errors: &[AnyError], prefix: &str) -> String {
fn inspect_array(array: &[AnyNode], prefix: &str) -> String {
if array.is_empty() {
- return "[]\n".to_string();
+ return format!("{}\n", "[]".dimmed());
}
let mut output = String::new();
- output.push_str(&format!("({} item{})\n", array.len(), if array.len() == 1 { "" } else { "s" }));
+ output.push_str(&format!("{}\n", format!("({} item{})", array.len(), if array.len() == 1 { "" } else { "s" }).dimmed()));
for (i, item) in array.iter().enumerate() {
let is_last = i == array.len() - 1;
let symbol = if is_last { "└── " } else { "├── " };
- let next_prefix = if is_last { " " } else { "│ " };
+ let next_prefix_str = if is_last { " " } else { "│ " };
+ let next_prefix = next_prefix_str.white().to_string();
let tree = item.tree_inspect();
let tree = tree.trim_end_matches('\n');
output.push_str(prefix);
- output.push_str(symbol);
+ output.push_str(&symbol.white().to_string());
output.push_str(&tree.replace('\n', &format!("\n{}{}", prefix, next_prefix)));
output.push('\n');
if !is_last {
output.push_str(prefix);
- output.push_str(next_prefix);
+ output.push_str(&next_prefix);
output.push('\n');
}
}
@@ -109,12 +113,12 @@ fn inspect_node_field(node: &T, prefix: &str) -> String {
let lines: Vec<&str> = tree.split('\n').collect();
if lines.is_empty() {
- return "∅\n".to_string();
+ return format!("{}\n", "∅".magenta());
}
let mut result = String::new();
result.push_str(prefix);
- result.push_str("└── ");
+ result.push_str(&"└── ".white().to_string());
result.push_str(lines[0]);
result.push('\n');
@@ -336,27 +340,31 @@ impl Node for <%= node.name %> {
fn tree_inspect(&self) -> String {
let mut output = String::new();
- output.push_str(&format!("@ <%= node.name %> (location: {})\n", self.location));
- output.push_str(&format_errors_field(&self.errors, <%- if node.fields.any? -%>"│ "<%- else -%>" "<%- end -%>));
+ output.push_str(&format!("{} {} {}\n",
+ "@".white(),
+ "<%= node.name %>".yellow().bold(),
+ format!("(location: {})", self.location).dimmed()
+ ));
+ output.push_str(&format_errors_field(&self.errors, &<%- if node.fields.any? -%>"│ "<%- else -%>" "<%- end -%>.white().to_string()));
<%- if node.fields.any? -%>
<%- node.fields.each_with_index do |field, index| -%>
<%- is_last = index == node.fields.length - 1 -%>
<%- symbol = is_last ? "└── " : "├── " -%>
<%- case field -%>
<%- when Herb::Template::StringField, Herb::Template::ElementSourceField -%>
- output.push_str(&format!("<%= symbol %><%= field.name %>: {}\n", format_string_value(&self.<%= field.name %>)));
+ output.push_str(&format!("{}{}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_string_value(&self.<%= field.name %>)));
<%- when Herb::Template::TokenField -%>
- output.push_str(&format!("<%= symbol %><%= field.name %>: {}\n", format_token_value(&self.<%= field.name %>)));
+ output.push_str(&format!("{}{}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_token_value(&self.<%= field.name %>)));
<%- when Herb::Template::BooleanField -%>
- output.push_str(&format!("<%= symbol %><%= field.name %>: {}\n", format_bool_value(self.<%= field.name %>)));
+ output.push_str(&format!("{}{}: {}\n", "<%= symbol %>".white(), "<%= field.name %>".white(), format_bool_value(self.<%= field.name %>)));
<%- when Herb::Template::ArrayField -%>
- output.push_str(&format!("<%= symbol %><%= field.name %>: {}", format_array_value(&self.<%= field.name %>, "<%= is_last ? " " : "│ " %>")));
+ output.push_str(&format!("{}{}: {}", "<%= symbol %>".white(), "<%= field.name %>".white(), format_array_value(&self.<%= field.name %>, &"<%= is_last ? " " : "│ " %>".white().to_string())));
<%- when Herb::Template::NodeField -%>
- output.push_str(&format!("<%= symbol %><%= field.name %>: {}", format_node_value(&self.<%= field.name %>, "<%= is_last ? " " : "│ " %>", <%= !is_last %>)));
+ output.push_str(&format!("{}{}: {}", "<%= symbol %>".white(), "<%= field.name %>".white(), format_node_value(&self.<%= field.name %>, &"<%= is_last ? " " : "│ " %>".white().to_string(), <%= !is_last %>)));
<%- end -%>
<%- end -%>
<%- else -%>
- output.push_str("└── (no fields)\n");
+ output.push_str(&format!("{} {}\n", "└──".white(), "(no fields)".dimmed()));
<%- end -%>
output