1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#![allow(warnings)]

#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;

mod convert;
mod get_dependencies;
mod init;
mod inspect;
mod merge;
mod pack;
mod unpack;
mod utils;

pub(crate) use crate::utils::*;

pub use crate::{
    convert::ConvertOptions, get_dependencies::GetDependenciesOptions, init::InitOptions,
    inspect::InspectOptions, merge::MergeOptions, pack::PackOptions, unpack::UnpackOptions,
};

use serde::Serialize;
use std::{collections::BTreeMap, fmt::Debug, io::Write};
use webc::v1::DirOrFile;

pub type FileMap = BTreeMap<DirOrFile, Vec<u8>>;

/// The format to use when printing something to the screen.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, clap::ValueEnum)]
pub enum Format {
    /// The format generated by Rust's `#[derive(Debug)]` machinery.
    Debug,
    #[default]
    Json,
}

impl Format {
    fn print<T>(self, value: &T) -> Result<(), anyhow::Error>
    where
        T: Debug + Serialize + ?Sized,
    {
        match self {
            Format::Debug => {
                println!("{value:?}");
                Ok(())
            }
            Format::Json => {
                let mut stdout = std::io::stdout();
                serde_json::to_writer_pretty(stdout.lock(), value)?;
                writeln!(stdout)?;
                Ok(())
            }
        }
    }
}