-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render enums using the datamodel renderer (#3305)
* Render enums using the datamodel renderer * Clippy and formatting * Rogue debugs * Return of some of the old normalization for now * Review fixes * Not doing this now
- Loading branch information
Julius de Bruijn
committed
Oct 20, 2022
1 parent
4333f57
commit 873cfc1
Showing
25 changed files
with
1,027 additions
and
425 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,76 @@ | ||
use std::fmt; | ||
|
||
use crate::Enum; | ||
|
||
/// The PSL data model declaration. | ||
#[derive(Default, Debug)] | ||
pub struct Datamodel<'a> { | ||
enums: Vec<Enum<'a>>, | ||
} | ||
|
||
impl<'a> Datamodel<'a> { | ||
/// Create a new empty data model. | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Add an enum block to the data model. | ||
/// | ||
/// ```ignore | ||
/// enum Foo { // < | ||
/// Bar // < this | ||
/// } // < | ||
/// ``` | ||
pub fn push_enum(&mut self, r#enum: Enum<'a>) { | ||
self.enums.push(r#enum); | ||
} | ||
} | ||
|
||
impl<'a> fmt::Display for Datamodel<'a> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
for r#enum in self.enums.iter() { | ||
writeln!(f, "{enum}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::*; | ||
use expect_test::expect; | ||
|
||
#[test] | ||
fn simple_data_model() { | ||
let mut traffic_light = Enum::new("TrafficLight"); | ||
|
||
traffic_light.push_variant("Red"); | ||
traffic_light.push_variant("Yellow"); | ||
traffic_light.push_variant("Green"); | ||
|
||
let mut cat = Enum::new("Cat"); | ||
cat.push_variant("Asleep"); | ||
cat.push_variant("Hungry"); | ||
|
||
let mut data_model = Datamodel::new(); | ||
data_model.push_enum(traffic_light); | ||
data_model.push_enum(cat); | ||
|
||
let expected = expect![[r#" | ||
enum TrafficLight { | ||
Red | ||
Yellow | ||
Green | ||
} | ||
enum Cat { | ||
Asleep | ||
Hungry | ||
} | ||
"#]]; | ||
|
||
let rendered = psl::reformat(&format!("{data_model}"), 2).unwrap(); | ||
expected.assert_eq(&rendered); | ||
} | ||
} |
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
Oops, something went wrong.