-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor[value/format] rewrite formatting functions with visitors
A visitor trait was added to the value package, and the formatting functions were rewritten as visitors. They were also renamed to more meaningful names.
- Loading branch information
Showing
5 changed files
with
173 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod format; | ||
pub mod source_map; | ||
pub mod value; | ||
pub mod visitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::value::*; | ||
|
||
pub trait Visitor<'a>: Sized { | ||
fn visit_value(&mut self, n: &'a Value) { | ||
walk_value(self, n); | ||
} | ||
|
||
fn visit_list(&mut self, n: &'a List) { | ||
walk_list(self, n); | ||
} | ||
|
||
fn visit_node(&mut self, n: &'a Node) { | ||
walk_node(self, n); | ||
} | ||
|
||
fn visit_char(&mut self, _: &'a Char) {} | ||
|
||
fn visit_string(&mut self, _: &'a String) {} | ||
|
||
fn visit_error(&mut self, _: &'a Error) {} | ||
} | ||
|
||
pub fn walk_value<'a, V: Visitor<'a>>(visitor: &mut V, n: &'a Value) { | ||
match n { | ||
Value::Char(v) => visitor.visit_char(v), | ||
Value::String(v) => visitor.visit_string(v), | ||
Value::List(v) => visitor.visit_list(v), | ||
Value::Node(v) => visitor.visit_node(v), | ||
Value::Error(v) => visitor.visit_error(v), | ||
} | ||
} | ||
|
||
pub fn walk_list<'a, V: Visitor<'a>>(visitor: &mut V, n: &'a List) { | ||
for v in &n.values { | ||
visitor.visit_value(v) | ||
} | ||
} | ||
|
||
pub fn walk_node<'a, V: Visitor<'a>>(visitor: &mut V, n: &'a Node) { | ||
for v in &n.items { | ||
visitor.visit_value(v) | ||
} | ||
} |