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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::{fmt::Display, io::Write, path::PathBuf, str::FromStr};

use anyhow::Error;
use clap::Parser;
use wasmer_pack::{Metadata, Package};

#[derive(Debug, Parser)]
pub struct Show {
    /// The format to use when emitting metadata.
    #[clap(short, long, default_value_t = Format::Text)]
    format: Format,
    /// The Pirita file to read.
    input: PathBuf,
}

impl Show {
    pub fn run(self) -> Result<(), Error> {
        let Show { format, input } = self;

        let pkg = crate::utils::load(&input)?;

        let summary: Summary = summarize(&pkg);

        let mut stdout = std::io::stdout();
        match format {
            Format::Json => {
                summary.write_json(stdout.lock())?;
                writeln!(stdout)?;
            }
            Format::Text => {
                summary.dump(stdout.lock())?;
            }
        }

        Ok(())
    }
}

fn summarize(pkg: &Package) -> Summary {
    let Metadata {
        description,
        package_name,
        version,
        ..
    } = pkg.metadata();

    let bindings = pkg
        .libraries()
        .iter()
        .map(|lib| Library {
            interface_name: lib.interface_name().to_string(),
            wasi: lib.requires_wasi(),
        })
        .collect();

    let commands = pkg
        .commands()
        .iter()
        .map(|cmd| Command {
            name: cmd.name.clone(),
        })
        .collect();

    Summary {
        description: description.clone(),
        name: package_name.to_string(),
        version: version.clone(),
        bindings,
        commands,
    }
}

#[derive(Debug, serde::Serialize)]
struct Summary {
    name: String,
    version: String,
    description: Option<String>,
    bindings: Vec<Library>,
    commands: Vec<Command>,
}

impl Summary {
    fn write_json(&self, writer: impl Write) -> Result<(), Error> {
        serde_json::to_writer_pretty(writer, self)?;
        Ok(())
    }

    fn dump(&self, mut writer: impl Write) -> Result<(), Error> {
        let Summary {
            name,
            version,
            description,
            commands,
            bindings,
        } = self;

        writeln!(writer, "{name} {version}")?;

        if let Some(description) = description {
            writeln!(writer, "{description}")?;
        }

        if !commands.is_empty() {
            writeln!(writer, "Commands:")?;
            for command in commands {
                command.dump(&mut writer)?;
            }
        }

        if !bindings.is_empty() {
            writeln!(writer, "Bindings:")?;
            for lib in bindings {
                lib.dump(&mut writer)?;
            }
        }

        Ok(())
    }
}

#[derive(Debug, serde::Serialize)]
struct Command {
    name: String,
}

impl Command {
    fn dump(&self, mut writer: impl Write) -> Result<(), Error> {
        let Command { name } = self;

        writeln!(writer, "- {name}")?;

        Ok(())
    }
}

#[derive(Debug, serde::Serialize)]
struct Library {
    interface_name: String,
    wasi: bool,
}

impl Library {
    fn dump(&self, mut writer: impl Write) -> Result<(), Error> {
        let Library {
            interface_name,
            wasi,
        } = self;

        write!(writer, "- {interface_name}")?;

        if *wasi {
            write!(writer, " (wasi)")?;
        }

        writeln!(writer)?;

        Ok(())
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, clap::ValueEnum)]
pub enum Format {
    Json,
    Text,
}

impl Display for Format {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Format::Json => f.write_str("json"),
            Format::Text => f.write_str("text"),
        }
    }
}

impl FromStr for Format {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "json" => Ok(Format::Json),
            "text" => Ok(Format::Text),
            other => anyhow::bail!("Expected \"json\" or \"text\", found \"{other}\""),
        }
    }
}