1use anyhow::{Context as _, Result};
12use clap::{
13 error::{ContextKind, ContextValue},
14 Parser as _,
15};
16use serde::Deserialize;
17use std::ffi::OsString;
18use std::path::Path;
19
20mod arg;
22mod settings;
24mod subcommand;
26mod version;
28
29use arg::{Cli, Commands};
30
31#[derive(Debug, Clone, Default, Deserialize)]
32struct Options {
36 #[expect(dead_code, reason = "not implemented yet")]
37 val1: String,
39}
40
41#[expect(clippy::single_call_fn, reason = "better readability")]
43fn read_file(path: &Path) -> Result<Options> {
44 let _content =
45 fs_err::read_to_string(path).context(format!("Failed to read file: {}", path.display()))?;
46 let options: Options = Options::default();
47 Ok(options)
48}
49
50#[derive(Debug, Clone)]
51#[expect(dead_code, reason = "not implemented yet")]
54struct FilesystemOptions(Options);
55
56impl FilesystemOptions {
57 #[expect(clippy::single_call_fn, reason = "better readability")]
59 pub fn from_file(file: &Path) -> Result<Self> {
60 let options: Options = read_file(file)?;
61 Ok(Self(options))
62 }
63}
64
65#[inline]
84pub fn run<I, T>(args: I) -> Result<()>
86where
87 I: IntoIterator<Item = T>,
88 T: Into<OsString> + Clone,
89{
90 let cli = match Cli::try_parse_from(args) {
91 Ok(cli) => cli,
92 Err(mut err) => {
93 #[expect(clippy::single_match, reason = "better readability")]
94 #[expect(clippy::pattern_type_mismatch, reason = "I don't know")]
95 match err.get(ContextKind::InvalidSubcommand) {
96 Some(ContextValue::String(subcommand)) => match subcommand.as_str() {
97 "help" => {
98 err.insert(
99 ContextKind::InvalidSubcommand,
100 ContextValue::String("help".to_owned()),
101 );
102 }
103 "module" => {
104 err.insert(
105 ContextKind::InvalidSubcommand,
106 ContextValue::String("module".to_owned()),
107 );
108 }
109 _ => {}
110 },
111 _ => {}
112 }
113 err.exit()
114 }
115 };
116
117 let filesystem = if let Some(config_file) = cli.top_level.config_file.as_ref() {
118 Some(FilesystemOptions::from_file(config_file)?)
119 } else {
120 None
121 };
122
123 #[expect(clippy::todo, reason = "implement later")]
124 if let Some(_filesystem) = filesystem {
125 todo!("TODO: read as global settings");
126 }
127
128 let _global_settings = settings::GlobalSettings::resolve(&cli.top_level.global_args);
130
131 let result = match *cli.command {
132 #[expect(clippy::todo, reason = "implement later")]
133 Commands::Help(_help_args) => {
134 todo!("TODO: implement help command");
135 }
136 Commands::Module(module_args) => subcommand::module::module(module_args),
137 };
138 result.context("Failed to run command")
139}