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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use std::{
    fmt::{self, Display, Formatter},
    path::Path,
    str::FromStr,
};

use anyhow::{Context, Error};
use heck::{ToPascalCase, ToSnakeCase};
use webc::Container;

#[derive(Debug, Clone)]
pub struct Package {
    metadata: Metadata,
    libraries: Vec<Library>,
    commands: Vec<Command>,
}

impl Package {
    /// Create a new [`Package`].
    ///
    /// # Panics
    ///
    /// This assumes all libraries have a unique [`Library::interface_name()`].
    pub fn new(metadata: Metadata, libraries: Vec<Library>, commands: Vec<Command>) -> Self {
        assert_unique_names("library", libraries.iter().map(|lib| lib.interface_name()));
        assert_unique_names("command", commands.iter().map(|cmd| cmd.name.as_str()));

        Package {
            metadata,
            libraries,
            commands,
        }
    }

    /// Load a [`Package`] from a WEBC binary.
    pub fn from_webc(webc: &Container) -> Result<Self, Error> {
        crate::pirita::load_webc_binary(webc)
    }

    pub fn metadata(&self) -> &Metadata {
        &self.metadata
    }

    pub fn libraries(&self) -> &[Library] {
        &self.libraries
    }

    pub fn requires_wasi(&self) -> bool {
        !self.commands.is_empty() || self.libraries.iter().any(|lib| lib.requires_wasi())
    }

    pub fn commands(&self) -> &[Command] {
        &self.commands
    }
}

fn assert_unique_names<'a>(kind: &str, names: impl IntoIterator<Item = &'a str>) {
    let mut already_seen: Vec<&str> = Vec::new();

    for name in names {
        match already_seen.binary_search(&name) {
            Ok(_) => panic!("Duplicate {kind} name: {name}"),
            Err(index) => already_seen.insert(index, name),
        }
    }
}

/// The name of a package from WAPM (e.g. `wasmer/wasmer-pack`).
///
/// Syntax:
///
/// - A `PackageName` consists of a “name” and an optional “namespace or
///   username”
/// - The “namespace or username” may be an “identifier” or the “_” namespace
///   (used for backwards compatibility)
/// - If a "namespace or username” isn’t provided, it is assumed to be a package
///   alias and will be resolved to a package by the WAPM backend
/// - “Identifiers” can only contain alphanumeric ascii characters, `_`, and `-`
/// - “Identifiers” must also start with an ascii character and be at most 100
///   characters long
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackageName {
    namespace: Namespace,
    name: String,
}

impl PackageName {
    pub fn parse(raw: &str) -> Result<Self, Error> {
        raw.parse()
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn set_name(&mut self, name: impl Into<String>) {
        self.name = name.into();
    }

    pub fn namespace(&self) -> &Namespace {
        &self.namespace
    }

    /// Get the NPM equivalent of this [`PackageName`].
    ///
    /// This should satisfy NPM's
    /// [naming rules](https://github.com/npm/validate-npm-package-name#naming-rules):
    ///
    /// - package name length should be greater than zero
    /// - all the characters in the package name must be lowercase i.e., no uppercase or mixed case names are allowed
    /// - package name can consist of hyphens
    /// - package name must not contain any non-url-safe characters (since name ends up being part of a URL)
    /// - package name should not start with . or _
    /// - package name should not contain any spaces
    /// - package name should not contain any of the following characters: ~)('!*
    /// - package name cannot be the same as a node.js/io.js core module nor a reserved/blacklisted name. For example, the following names are invalid:
    ///   - http
    ///   - stream
    ///   - node_modules
    ///   - favicon.ico
    /// - package name length cannot exceed 214
    pub fn javascript_package(&self) -> String {
        let PackageName { namespace, name } = self;

        match namespace.as_str() {
            Some(ns) => format!("@{ns}/{name}").to_lowercase(),
            None => name.to_string().to_lowercase(),
        }
    }

    /// Get the PyPI equivalent of this [`PackageName`].
    ///
    /// This should satisfy the naming scheme outlined in
    /// [PEP 8](https://peps.python.org/pep-0008/#package-and-module-names):
    ///
    /// > Modules should have short, all-lowercase names. Underscores can be
    /// > used in the module name if it improves readability. Python packages
    /// > should also have short, all-lowercase names, although the use of
    /// > underscores is discouraged.
    pub fn python_name(&self) -> String {
        self.name.to_snake_case()
    }
}

impl FromStr for PackageName {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if !s.contains('/') {
            let name = parse_identifier(s)
                .with_context(|| format!("\"{s}\" is not a valid package name"))?;
            return Ok(PackageName {
                namespace: Namespace::None,
                name,
            });
        }

        let (namespace, name) = s.split_once('/').context(
            "All packages must have a namespace (i.e. the \"wasmer\" in \"wasmer/wasmer-pack\")",
        )?;

        let namespace = if namespace == "_" {
            Namespace::Underscore
        } else {
            let ns = parse_identifier(namespace)
                .with_context(|| format!("\"{namespace}\" is not a valid namespace"))?;
            Namespace::Some(ns)
        };

        let name = parse_identifier(name)
            .with_context(|| format!("\"{name}\" is not a valid package name"))?;

        Ok(PackageName { namespace, name })
    }
}

impl Display for PackageName {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let PackageName { namespace, name } = self;

        if let Some(ns) = namespace.as_str() {
            write!(f, "{ns}/")?;
        }

        write!(f, "{name}")?;

        Ok(())
    }
}

/// The username or organisation a [`Package`] may be associated with.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Namespace {
    /// The namespace was present.
    Some(String),
    /// The `_` namespace - typically used for global packages or backwards
    /// compatibility with the time before WAPM had namespaces.
    Underscore,
    /// No namespace was provided. Typically this means the backend will resolve
    /// this package to an alias.
    None,
}

impl Namespace {
    /// Get the namespace as a string, if one is present.
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Namespace::Some(s) => Some(s),
            Namespace::Underscore | Namespace::None => None,
        }
    }
}

fn parse_identifier(s: &str) -> Result<String, Error> {
    anyhow::ensure!(!s.is_empty(), "Identifiers can't be empty");
    anyhow::ensure!(
        s.starts_with(|c: char| c.is_ascii_alphabetic()),
        "Identifiers must start with an ascii letter",
    );
    anyhow::ensure!(
        s.chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.')),
        "Identifiers can only contain '-', '_', '.', ascii numbers, and letters"
    );
    Ok(s.replace('.', "-"))
}

/// Information about the [`Package`] being generated.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Metadata {
    /// The package's name.
    pub package_name: PackageName,
    /// A semver-compliant version number.
    pub version: String,
    /// Extended information about the package.
    pub description: Option<String>,
}

impl Metadata {
    /// Create a new [`Metadata`] object with all required fields.
    pub fn new(package_name: PackageName, version: impl Into<String>) -> Self {
        Metadata {
            package_name,
            version: version.into(),
            description: None,
        }
    }

    /// Set the [`Metadata::description`] field.
    pub fn with_description(self, description: impl Into<String>) -> Self {
        Metadata {
            description: Some(description.into()),
            ..self
        }
    }
}

#[derive(Debug, Clone)]
pub struct Library {
    pub module: Module,
    pub exports: Interface,
    pub imports: Vec<Interface>,
}

impl Library {
    /// The name of the interface being generated.
    ///
    /// If coming from a WIT file, this will be the `wasmer-pack` in
    /// `wasmer-pack.exports.wit`.
    pub fn interface_name(&self) -> &str {
        self.exports.name()
    }

    /// The name of the class generated by `wai-bindgen`.
    ///
    /// For example, if you were generating bindings for `wasmer-pack.exports.wit`,
    /// this would be `WasmerPack`.
    pub fn class_name(&self) -> String {
        self.interface_name().to_pascal_case()
    }

    /// The filename of the [`Module`] this [`Library`] contains.
    ///
    /// For example, if the [`Module`] was loaded from `./path/to/wasmer-pack.wasm`,
    /// this would be `wasmer-pack.wasm`.
    pub fn module_filename(&self) -> &str {
        Path::new(&self.module.name)
            .file_name()
            .expect("We assume module names are non-empty")
            .to_str()
            .expect("The original path came from a Rust string")
    }

    pub fn requires_wasi(&self) -> bool {
        matches!(self.module.abi, Abi::Wasi)
    }
}

/// A WebAssembly module.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Module {
    /// A name used to refer to this module (e.g. `wasmer_pack_wasm`).
    pub name: String,
    /// The ABI used by the module.
    pub abi: Abi,
    /// The WebAssembly code, itself.
    pub wasm: Vec<u8>,
}

impl Module {
    /// Load a [`Module`] from a file on disk.
    ///
    /// # Note
    ///
    /// The [`Module::from_path()`] constructor explicitly **doesn't** perform
    /// any validation on the module's file. It is up to the caller to ensure
    /// they pass in the correct [`Abi`].
    pub fn from_path(path: impl AsRef<Path>, abi: Abi) -> Result<Self, Error> {
        let path = path.as_ref();
        let name = path
            .file_name()
            .context("Empty filename")?
            .to_string_lossy()
            .into_owned();

        let wasm = std::fs::read(path)
            .with_context(|| format!("Unable to read \"{}\"", path.display()))?;

        Ok(Module { name, abi, wasm })
    }
}

/// The [*Application Binary Interface*][abi] used by a [`Module`].
///
/// [abi]: https://www.webassembly.guide/webassembly-guide/webassembly/wasm-abis
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Abi {
    None,
    Wasi,
}

impl FromStr for Abi {
    type Err = Error;

    fn from_str(s: &str) -> Result<Abi, Error> {
        match s {
            "none" => Ok(Abi::None),
            "wasi" => Ok(Abi::Wasi),
            _ => Err(Error::msg("Expected either \"none\" or \"wasi\"")),
        }
    }
}

/// The interface exported by the WebAssembly module.
#[derive(Debug, Clone)]
pub struct Interface(pub(crate) wai_parser::Interface);

impl Interface {
    /// Parse an interface definition in the WIT format.
    ///
    /// This will **not** attempt to parse any other files the interface
    /// definition depends on.
    pub fn from_wit(name: &str, src: &str) -> Result<Self, Error> {
        let wit =
            wai_parser::Interface::parse(name, src).context("Unable to parse the WIT file")?;
        Ok(Interface(wit))
    }

    /// Parse an [`Interface`] from its interface definition on disk,
    /// potentially recursively parsing any files it depends on.
    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
        let path = path.as_ref();
        let wit = wai_parser::Interface::parse_file(path)
            .with_context(|| format!("Unable to parse \"{}\"", path.display()))?;
        Ok(Interface(wit))
    }

    /// The name of the interface being generated.
    ///
    /// If coming from a WIT file, this will be the `wasmer-pack` in
    /// `wasmer-pack.exports.wit`.
    pub fn name(&self) -> &str {
        &self.0.name
    }
}

#[derive(Debug, Clone)]
pub struct Command {
    pub name: String,
    pub wasm: Vec<u8>,
}

impl Command {
    pub fn new(name: impl Into<String>, wasm: impl Into<Vec<u8>>) -> Self {
        Command {
            name: name.into(),
            wasm: wasm.into(),
        }
    }
}

/// A set of extra options passed to the bindings generator function
#[derive(Default)]
pub struct BindingsOptions {
    /// User defined name for the generated bindings
    pub name: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sanitize_package_names() {
        let inputs = vec![
            ("package", true),
            ("namespace/package_name", true),
            ("_/package_name", true),
            ("name-space/package-name", true),
            ("n9/p21", true),
            ("wasmer/package", true),
            (
                "abcdefghijklmopqrstuvwxyz_ABCDEFGHIJKLMOPQRSTUVWXYZ0123456789/abcdefghijklmopqrstuvwxyz-ABCDEFGHIJKLMOPQRSTUVWXYZ0123456789",
                true,
            ),
            ("_wasmer/package", false),
            ("wasmer/_package", false),
            ("लाज/तोब", false),
            ("test/package with spaces", false),
            ("-wasmer/package", false),
            ("wasmer/-package", false),
            ("wasmer/-", false),
            ("wasmer/597d361e-f431-4960-9b2a-7e78ec0dbfeb", false),
            ("name space/name", false),
            ("@wasmer/package-name", false),
            ("", false),
        ];

        for (original, is_okay) in inputs {
            let got = PackageName::parse(original);
            assert_eq!(got.is_ok(), is_okay, "{original}");
        }
    }
}