wapm_to_webc/
lib.rs

1#![allow(warnings)]
2
3#[cfg(test)]
4#[macro_use]
5extern crate pretty_assertions;
6
7mod convert;
8mod get_dependencies;
9mod init;
10mod inspect;
11mod merge;
12mod pack;
13mod unpack;
14mod utils;
15
16pub(crate) use crate::utils::*;
17
18pub use crate::{
19    convert::ConvertOptions, get_dependencies::GetDependenciesOptions, init::InitOptions,
20    inspect::InspectOptions, merge::MergeOptions, pack::PackOptions, unpack::UnpackOptions,
21};
22
23use serde::Serialize;
24use std::{collections::BTreeMap, fmt::Debug, io::Write};
25use webc::v1::DirOrFile;
26
27pub type FileMap = BTreeMap<DirOrFile, Vec<u8>>;
28
29/// The format to use when printing something to the screen.
30#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, clap::ValueEnum)]
31pub enum Format {
32    /// The format generated by Rust's `#[derive(Debug)]` machinery.
33    Debug,
34    #[default]
35    Json,
36}
37
38impl Format {
39    fn print<T>(self, value: &T) -> Result<(), anyhow::Error>
40    where
41        T: Debug + Serialize + ?Sized,
42    {
43        match self {
44            Format::Debug => {
45                println!("{value:?}");
46                Ok(())
47            }
48            Format::Json => {
49                let mut stdout = std::io::stdout();
50                serde_json::to_writer_pretty(stdout.lock(), value)?;
51                writeln!(stdout)?;
52                Ok(())
53            }
54        }
55    }
56}