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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::intermediate_representation::TargetLanguage;
use crate::util::loaders::LoaderConfig;
use anyhow::{bail, Error, Result};
use std::path::Path;
use std::path::PathBuf;

pub(crate) enum CliCmd {
    Generate(GenerateStructCmd),
    GenerateExperimenter(GenerateExperimenterManifestCmd),
    GenerateSingleFileManifest(GenerateSingleFileManifestCmd),
    FetchFile(LoaderConfig, String),
    Validate(ValidateCmd),
    PrintChannels(PrintChannelsCmd),
    PrintInfo(PrintInfoCmd),
}

#[derive(Clone)]
pub(crate) struct GenerateStructCmd {
    pub(crate) manifest: String,
    pub(crate) output: PathBuf,
    pub(crate) language: TargetLanguage,
    pub(crate) load_from_ir: bool,
    pub(crate) channel: String,
    pub(crate) loader: LoaderConfig,
}

pub(crate) struct GenerateExperimenterManifestCmd {
    pub(crate) manifest: String,
    pub(crate) output: PathBuf,
    pub(crate) language: TargetLanguage,
    pub(crate) load_from_ir: bool,
    pub(crate) loader: LoaderConfig,
}

pub(crate) struct GenerateSingleFileManifestCmd {
    pub(crate) manifest: String,
    pub(crate) output: PathBuf,
    pub(crate) channel: String,
    pub(crate) loader: LoaderConfig,
}

pub(crate) struct ValidateCmd {
    pub(crate) manifest: String,
    pub(crate) loader: LoaderConfig,
}

pub(crate) struct PrintChannelsCmd {
    pub(crate) manifest: String,
    pub(crate) loader: LoaderConfig,
    pub(crate) as_json: bool,
}

pub(crate) struct PrintInfoCmd {
    pub(crate) manifest: String,
    pub(crate) loader: LoaderConfig,
    pub(crate) channel: Option<String>,
    pub(crate) as_json: bool,
    pub(crate) feature: Option<String>,
}

impl TryFrom<&std::ffi::OsStr> for TargetLanguage {
    type Error = Error;
    fn try_from(value: &std::ffi::OsStr) -> Result<Self> {
        if let Some(s) = value.to_str() {
            TryFrom::try_from(s)
        } else {
            bail!("Unreadable target language")
        }
    }
}

impl TryFrom<&Path> for TargetLanguage {
    type Error = Error;
    fn try_from(value: &Path) -> Result<Self> {
        TryFrom::try_from(
            value
                .extension()
                .ok_or_else(|| anyhow::anyhow!("No extension available to determine language"))?,
        )
    }
}

impl TryFrom<String> for TargetLanguage {
    type Error = Error;
    fn try_from(value: String) -> Result<Self> {
        TryFrom::try_from(value.as_str())
    }
}