nimbus_fml/command_line/
commands.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::intermediate_representation::TargetLanguage;
6use crate::util::loaders::LoaderConfig;
7use anyhow::{bail, Error, Result};
8use std::path::Path;
9use std::path::PathBuf;
10
11pub(crate) enum CliCmd {
12    Generate(GenerateStructCmd),
13    GenerateExperimenter(GenerateExperimenterManifestCmd),
14    GenerateSingleFileManifest(GenerateSingleFileManifestCmd),
15    FetchFile(LoaderConfig, String),
16    Validate(ValidateCmd),
17    PrintChannels(PrintChannelsCmd),
18    PrintInfo(PrintInfoCmd),
19}
20
21#[derive(Clone)]
22pub(crate) struct GenerateStructCmd {
23    pub(crate) manifest: String,
24    pub(crate) output: PathBuf,
25    pub(crate) language: TargetLanguage,
26    pub(crate) load_from_ir: bool,
27    pub(crate) channel: String,
28    pub(crate) loader: LoaderConfig,
29}
30
31pub(crate) struct GenerateExperimenterManifestCmd {
32    pub(crate) manifest: String,
33    pub(crate) output: PathBuf,
34    pub(crate) language: TargetLanguage,
35    pub(crate) load_from_ir: bool,
36    pub(crate) loader: LoaderConfig,
37}
38
39pub(crate) struct GenerateSingleFileManifestCmd {
40    pub(crate) manifest: String,
41    pub(crate) output: PathBuf,
42    pub(crate) channel: String,
43    pub(crate) loader: LoaderConfig,
44}
45
46pub(crate) struct ValidateCmd {
47    pub(crate) manifest: String,
48    pub(crate) loader: LoaderConfig,
49}
50
51pub(crate) struct PrintChannelsCmd {
52    pub(crate) manifest: String,
53    pub(crate) loader: LoaderConfig,
54    pub(crate) as_json: bool,
55}
56
57pub(crate) struct PrintInfoCmd {
58    pub(crate) manifest: String,
59    pub(crate) loader: LoaderConfig,
60    pub(crate) channel: Option<String>,
61    pub(crate) as_json: bool,
62    pub(crate) feature: Option<String>,
63}
64
65impl TryFrom<&std::ffi::OsStr> for TargetLanguage {
66    type Error = Error;
67    fn try_from(value: &std::ffi::OsStr) -> Result<Self> {
68        if let Some(s) = value.to_str() {
69            TryFrom::try_from(s)
70        } else {
71            bail!("Unreadable target language")
72        }
73    }
74}
75
76impl TryFrom<&Path> for TargetLanguage {
77    type Error = Error;
78    fn try_from(value: &Path) -> Result<Self> {
79        TryFrom::try_from(
80            value
81                .extension()
82                .ok_or_else(|| anyhow::anyhow!("No extension available to determine language"))?,
83        )
84    }
85}
86
87impl TryFrom<String> for TargetLanguage {
88    type Error = Error;
89    fn try_from(value: String) -> Result<Self> {
90        TryFrom::try_from(value.as_str())
91    }
92}