pint_cli/
new.rs

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
//! `pint new` implementation.

use clap::{builder::styling::Style, Parser};
use pint_pkg::{
    manifest::{ManifestFile, PackageKind},
    new::new_pkg,
};
use std::path::PathBuf;

/// Create a new package.
#[derive(Parser, Debug)]
pub struct Args {
    /// Specify the "contract" package kind.
    ///
    /// This is the default behaviour.
    #[arg(long)]
    contract: bool,
    /// Specify the "library" package kind.
    ///
    /// By default, new packages are created with the "contract" kind.
    #[arg(long)]
    lib: bool,
    /// Optionally provide a name.
    ///
    /// By default, the package name is the last directory in the canonicalized
    /// representation of the given path.
    #[arg(long)]
    name: Option<String>,
    /// The directory path in which the package should be created.
    path: PathBuf,
}

fn kind_from_bools(contract: bool, lib: bool) -> anyhow::Result<Option<PackageKind>> {
    let opt = match (contract, lib) {
        (false, false) => None,
        (true, false) => Some(PackageKind::Contract),
        (false, true) => Some(PackageKind::Library),
        (true, true) => anyhow::bail!("must choose either `contract` or `lib`, not both"),
    };
    Ok(opt)
}

pub(crate) fn cmd(args: Args) -> anyhow::Result<()> {
    let name = args.name;
    let kind = kind_from_bools(args.contract, args.lib)?;
    let opts = pint_pkg::new::Options { name, kind };
    let manifest_path = new_pkg(&args.path, opts)?;
    let manifest = ManifestFile::from_path(&manifest_path)?;
    let bold = Style::new().bold();
    println!(
        "     {}Created{} {} [{}] ({})",
        bold.render(),
        bold.render_reset(),
        manifest.pkg.name,
        manifest.pkg.kind,
        manifest.dir().display(),
    );
    Ok(())
}