Skip to content

Commit

Permalink
chore(cli): Simplify --fmt=json export
Browse files Browse the repository at this point in the history
By swapping from
```rust
struct Foo {
   #[serde(flatten)]
   bar: Bar,
}
```
to
```rust
#[serde(transparent)]
struct Foo(Bar);
```

we can accomodate non-object/map values to be directly serialized instead of
wrapping them into a struct with one field.
  • Loading branch information
tingerrr committed Aug 28, 2024
1 parent d5a717f commit eedc317
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 52 deletions.
12 changes: 4 additions & 8 deletions crates/typst-test-cli/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ pub struct Args {
}

#[derive(Debug, Serialize)]
pub struct AddedReport<'t> {
#[serde(flatten)]
inner: TestJson<'t>,
}
#[serde(transparent)]
pub struct AddedReport<'t>(TestJson<'t>);

impl Report for AddedReport<'_> {
fn report<W: WriteColor>(&self, mut writer: W, _verbosity: Verbosity) -> anyhow::Result<()> {
write!(writer, "Added ")?;
ui::write_colored(&mut writer, Color::Cyan, |w| write!(w, "{}", self.inner.id))?;
ui::write_colored(&mut writer, Color::Cyan, |w| write!(w, "{}", self.0.id))?;
writeln!(writer)?;

Ok(())
Expand Down Expand Up @@ -75,9 +73,7 @@ pub fn run(ctx: &mut Context, args: &Args) -> anyhow::Result<()> {

project.create_test(args.test.clone(), kind, !args.no_template)?;
let test = &project.matched()[&args.test];
ctx.reporter.report(&AddedReport {
inner: TestJson::new(test),
})?;
ctx.reporter.report(&AddedReport(TestJson::new(test)))?;

Ok(())
}
13 changes: 5 additions & 8 deletions crates/typst-test-cli/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ pub enum Vcs {
}

#[derive(Debug, Serialize)]
pub struct InitReport<'p> {
#[serde(flatten)]
inner: ProjectJson<'p>,
}
#[serde(transparent)]
pub struct InitReport<'p>(ProjectJson<'p>);

impl Report for InitReport<'_> {
fn report<W: WriteColor>(&self, mut writer: W, _verbosity: Verbosity) -> anyhow::Result<()> {
write!(writer, "Initialized project ")?;
let (color, name) = match &self.inner.package {
let (color, name) = match &self.0.package {
Some(package) => (Color::Cyan, package.name),
None => (Color::Yellow, "<unnamed>"),
};
Expand All @@ -65,9 +63,8 @@ pub fn run(ctx: &mut Context, args: &Args) -> anyhow::Result<()> {

project.init(args.no_example, args.vcs)?;

ctx.reporter.report(&InitReport {
inner: ProjectJson::new(&project),
})?;
ctx.reporter
.report(&InitReport(ProjectJson::new(&project)))?;

Ok(())
}
15 changes: 7 additions & 8 deletions crates/typst-test-cli/src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,24 @@ pub struct Args {
}

#[derive(Debug, Serialize)]
pub struct ListReport<'p> {
tests: Vec<TestJson<'p>>,
}
#[serde(transparent)]
pub struct ListReport<'p>(Vec<TestJson<'p>>);

impl Report for ListReport<'_> {
fn report<W: WriteColor>(&self, writer: W, _verbosity: Verbosity) -> anyhow::Result<()> {
let mut w = Heading::new(writer, "Tests");

// NOTE: max pading of 50 should be enough for most cases
let pad = Ord::min(
self.tests
self.0
.iter()
.map(|test| test.id.len())
.max()
.unwrap_or(usize::MAX),
50,
);

for test in &self.tests {
for test in &self.0 {
write!(w, "{: <pad$} ", test.id)?;
let color = match test.kind {
"ephemeral" => Color::Yellow,
Expand All @@ -53,9 +52,9 @@ impl Report for ListReport<'_> {

pub fn run(ctx: &mut Context, args: &Args) -> anyhow::Result<()> {
let project = ctx.collect_tests(&args.op_args, None)?;
ctx.reporter.report(&ListReport {
tests: project.matched().values().map(TestJson::new).collect(),
})?;
ctx.reporter.report(&ListReport(
project.matched().values().map(TestJson::new).collect(),
))?;

Ok(())
}
21 changes: 9 additions & 12 deletions crates/typst-test-cli/src/cli/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ use crate::report::{Report, Verbosity};
use crate::ui;

#[derive(Debug, Serialize)]
pub struct StatusReport<'p> {
#[serde(flatten)]
inner: ProjectJson<'p>,
}
#[serde(transparent)]
pub struct StatusReport<'p>(ProjectJson<'p>);

impl Report for StatusReport<'_> {
fn report<W: WriteColor>(&self, mut writer: W, _verbosity: Verbosity) -> anyhow::Result<()> {
Expand All @@ -33,7 +31,7 @@ impl Report for StatusReport<'_> {
.max()
.unwrap();

if let Some(package) = &self.inner.package {
if let Some(package) = &self.0.package {
write!(writer, "{:>align$}{}", "Project", delims.open)?;
ui::write_bold_colored(&mut writer, Color::Cyan, |w| write!(w, "{}", &package.name))?;
write!(writer, ":")?;
Expand All @@ -47,7 +45,7 @@ impl Report for StatusReport<'_> {
writeln!(writer)?;
}

if let Some(vcs) = &self.inner.vcs {
if let Some(vcs) = &self.0.vcs {
write!(writer, "{:>align$}{}", "Vcs", delims.middle)?;
ui::write_bold_colored(&mut writer, Color::Green, |w| write!(w, "{vcs}"))?;
writeln!(writer)?;
Expand All @@ -57,15 +55,15 @@ impl Report for StatusReport<'_> {
writeln!(writer)?;
}

if self.inner.tests.is_empty() {
if self.0.tests.is_empty() {
write!(writer, "{:>align$}{}", "Tests", delims.middle)?;
ui::write_bold_colored(&mut writer, Color::Cyan, |w| writeln!(w, "none"))?;
} else {
let mut persistent = 0;
let mut ephemeral = 0;
let mut compile_only = 0;

for test in &self.inner.tests {
for test in &self.0.tests {
match test.kind {
"persistent" => persistent += 1,
"ephemeral" => ephemeral += 1,
Expand All @@ -88,7 +86,7 @@ impl Report for StatusReport<'_> {
}

write!(writer, "{:>align$}{}", "Template", delims.close)?;
if let Some(path) = &self.inner.template_path {
if let Some(path) = &self.0.template_path {
ui::write_bold_colored(&mut writer, Color::Cyan, |w| write!(w, "{path}"))?;
} else {
ui::write_bold_colored(&mut writer, Color::Green, |w| write!(w, "none"))?;
Expand All @@ -104,9 +102,8 @@ pub fn run(ctx: &mut Context) -> anyhow::Result<()> {
project.collect_tests(test_set::builtin::all())?;
project.load_template()?;

ctx.reporter.report(&StatusReport {
inner: ProjectJson::new(&project),
})?;
ctx.reporter
.report(&StatusReport(ProjectJson::new(&project)))?;

Ok(())
}
15 changes: 6 additions & 9 deletions crates/typst-test-cli/src/cli/uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ pub struct Args {
}

#[derive(Debug, Serialize)]
pub struct InitReport<'p> {
#[serde(flatten)]
inner: ProjectJson<'p>,
}
#[serde(transparent)]
pub struct InitReport<'p>(ProjectJson<'p>);

impl Report for InitReport<'_> {
fn report<W: WriteColor>(&self, mut writer: W, _verbosity: Verbosity) -> anyhow::Result<()> {
write!(writer, "Uninitalized project ")?;
let (color, name) = match &self.inner.package {
let (color, name) = match &self.0.package {
Some(package) => (Color::Cyan, package.name),
None => (Color::Yellow, "<unnamed>"),
};
ui::write_colored(&mut writer, color, |w| write!(w, "{}", name))?;
let count = self.inner.tests.len();
let count = self.0.tests.len();
writeln!(
writer,
", removed {} {}",
Expand Down Expand Up @@ -66,9 +64,8 @@ pub fn run(ctx: &mut Context, args: &Args) -> anyhow::Result<()> {

project.uninit()?;

ctx.reporter.report(&InitReport {
inner: ProjectJson::new(&project),
})?;
ctx.reporter
.report(&InitReport(ProjectJson::new(&project)))?;

Ok(())
}
13 changes: 6 additions & 7 deletions crates/typst-test-cli/src/cli/util/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ struct FontJson<'f> {
}

#[derive(Debug, Serialize)]
pub struct FontsReport<'f> {
fonts: Vec<FontJson<'f>>,
}
#[serde(transparent)]
pub struct FontsReport<'f>(Vec<FontJson<'f>>);

impl Report for FontsReport<'_> {
fn report<W: WriteColor>(&self, writer: W, _verbosity: Verbosity) -> anyhow::Result<()> {
let mut w = Heading::new(writer, "Fonts");
for font in &self.fonts {
for font in &self.0 {
ui::write_ident(&mut w, |w| writeln!(w, "{}", font.name))?;

let w = &mut Indented::new(&mut w, 2);
Expand All @@ -57,8 +56,8 @@ impl Report for FontsReport<'_> {
pub fn run(ctx: &mut Context, args: &Args) -> anyhow::Result<()> {
let fonts = ctx.args.global.fonts.searcher();

ctx.reporter.report(&FontsReport {
fonts: fonts
ctx.reporter.report(&FontsReport(
fonts
.book
.families()
.map(|(name, info)| FontJson {
Expand All @@ -79,7 +78,7 @@ pub fn run(ctx: &mut Context, args: &Args) -> anyhow::Result<()> {
},
})
.collect(),
})?;
))?;

Ok(())
}

0 comments on commit eedc317

Please sign in to comment.