Skip to content

Commit

Permalink
[wit-component] make WitPrinter::print return Result<()>
Browse files Browse the repository at this point in the history
As per PR request, merge `print` with `print_all`.
  • Loading branch information
tomasol committed Dec 18, 2024
1 parent 69b8715 commit 4fc1656
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 30 deletions.
30 changes: 9 additions & 21 deletions crates/wit-component/src/printing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{anyhow, bail, Result};
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Display;
use std::mem;
use std::ops::Deref;
use wit_parser::*;
Expand Down Expand Up @@ -29,19 +30,6 @@ impl Default for WitPrinter {
}
}

impl<O: Output> WitPrinter<O>
where
String: From<O>,
{
/// Convenience wrapper around [`print_all`](WitPrinter::print_all) that prints the specified `pkg` which is located in `resolve` to a string.
///
/// The `nested` list of packages are other packages to include at the end
/// of the output in `package ... { ... }` syntax.
pub fn print(self, resolve: &Resolve, pkg: PackageId, nested: &[PackageId]) -> Result<String> {
self.print_all(resolve, pkg, nested).map(String::from)
}
}

impl<O: Output> WitPrinter<O> {
/// Craete new instance.
pub fn new(output: O) -> Self {
Expand All @@ -60,12 +48,7 @@ impl<O: Output> WitPrinter<O> {
///
/// The `nested` list of packages are other packages to include at the end
/// of the output in `package ... { ... }` syntax.
pub fn print_all(
mut self,
resolve: &Resolve,
pkg: PackageId,
nested: &[PackageId],
) -> Result<O> {
pub fn print(&mut self, resolve: &Resolve, pkg: PackageId, nested: &[PackageId]) -> Result<()> {
self.print_package(resolve, pkg, true)?;
for (i, pkg_id) in nested.iter().enumerate() {
if i > 0 {
Expand All @@ -74,8 +57,7 @@ impl<O: Output> WitPrinter<O> {
}
self.print_package(resolve, *pkg_id, false)?;
}

Ok(self.output)
Ok(())
}

/// Configure whether doc comments will be printed.
Expand Down Expand Up @@ -1332,3 +1314,9 @@ impl From<OutputToString> for String {
output.output
}
}

impl Display for OutputToString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.output.fmt(f)
}
}
4 changes: 3 additions & 1 deletion crates/wit-component/tests/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ fn run_test(path: &Path) -> Result<()> {
DecodedWasm::WitPackage(..) => unreachable!(),
DecodedWasm::Component(resolve, world) => (resolve.worlds[world].package.unwrap(), resolve),
};
let wit = WitPrinter::default()
let mut printer = WitPrinter::default();
printer
.print(&resolve, pkg, &[])
.context("failed to print WIT")?;
let wit = printer.output.to_string();
assert_output(&wit, &component_wit_path)?;

UnresolvedPackageGroup::parse(&component_wit_path, &wit)
Expand Down
4 changes: 3 additions & 1 deletion crates/wit-component/tests/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ fn run_test(path: &Path, is_dir: bool) -> Result<()> {
}

fn assert_print(resolve: &Resolve, pkg_id: PackageId, path: &Path, is_dir: bool) -> Result<()> {
let output = WitPrinter::default().print(resolve, pkg_id, &[])?;
let mut printer = WitPrinter::default();
printer.print(resolve, pkg_id, &[])?;
let output = printer.output.to_string();
let pkg = &resolve.packages[pkg_id];
let expected = if is_dir {
path.join(format!("{}.wit.print", &pkg.name.name))
Expand Down
4 changes: 3 additions & 1 deletion crates/wit-component/tests/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ fn merging() -> Result<()> {
.join("merge")
.join(&pkg.name.name)
.with_extension("wit");
let output = WitPrinter::default().print(&into, id, &[])?;
let mut printer = WitPrinter::default();
printer.print(&into, id, &[])?;
let output = printer.output.to_string();
assert_output(&expected, &output)?;
}
}
Expand Down
6 changes: 3 additions & 3 deletions fuzz/src/roundtrip_wit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ fn roundtrip_through_printing(file: &str, resolve: &Resolve, pkg: PackageId, was
.map(|p| p.0)
.filter(|k| *k != pkg)
.collect::<Vec<_>>();
let doc = WitPrinter::default()
.print(resolve, pkg, &package_deps)
.unwrap();
let mut printer = WitPrinter::default();
printer.print(resolve, pkg, &package_deps).unwrap();
let doc = printer.output.to_string();
let new_pkg = new_resolve
.push_str(&format!("printed-{file}.wit"), &doc)
.unwrap();
Expand Down
4 changes: 3 additions & 1 deletion src/bin/wasm-tools/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,9 @@ impl WitOpts {
let main = decoded.package();
for (id, pkg) in resolve.packages.iter() {
let is_main = id == main;
let output = configure_printer().print(resolve, id, &[])?;
let mut printer = configure_printer();
printer.print(resolve, id, &[])?;
let output = printer.output.to_string();
let out_dir = if is_main {
dir.clone()
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,16 @@ impl OutputArg {
}
Output::Json(s) => self.output_str(s),
#[cfg(feature = "component")]
Output::Wit { wit, printer } => {
Output::Wit { wit, mut printer } => {
let resolve = wit.resolve();
let ids = resolve
.packages
.iter()
.map(|(id, _)| id)
.filter(|id| *id != wit.package())
.collect::<Vec<_>>();
let output = printer.print(resolve, wit.package(), &ids)?;
printer.print(resolve, wit.package(), &ids)?;
let output = printer.output.to_string();
self.output_str(&output)
}
}
Expand Down

0 comments on commit 4fc1656

Please sign in to comment.