Skip to content

Commit 0726be8

Browse files
committed
functora-cfg ReClap
1 parent 4c882f3 commit 0726be8

File tree

3 files changed

+271
-1
lines changed

3 files changed

+271
-1
lines changed

rust/functora-cfg/Cargo.lock

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/functora-cfg/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repository = "https://github.com/functora/functora.github.io/tree/master/rust/fu
99
description = "A Rust library that merges configuration values from multiple sources into a single typed value."
1010

1111
[dependencies]
12+
clap = { version = "4.5.50", features = ["derive"] }
1213
config = "0.15.18"
1314
serde = "1.0.228"
1415
toml = "0.9.8"

rust/functora-cfg/src/lib.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![doc = include_str!("../README.md")]
2+
use clap::{Args, FromArgMatches, Subcommand};
23
pub use config::ConfigError;
34
use config::{Config, Environment, File, Value};
4-
use serde::{Serialize, de::DeserializeOwned};
5+
use serde::{Deserialize, Serialize, de::DeserializeOwned};
56
use std::path::Path;
67
use toml::Value as TomlValue;
78

@@ -100,3 +101,70 @@ fn toml_to_config(toml: TomlValue) -> Value {
100101
),
101102
}
102103
}
104+
105+
#[derive(
106+
Eq,
107+
PartialEq,
108+
Ord,
109+
PartialOrd,
110+
Debug,
111+
Clone,
112+
Serialize,
113+
Deserialize,
114+
)]
115+
pub struct ReClap<T, U>
116+
where
117+
T: Args,
118+
U: Subcommand,
119+
{
120+
pub prev: T,
121+
pub next: Option<Box<U>>,
122+
}
123+
124+
impl<T, U> Args for ReClap<T, U>
125+
where
126+
T: Args,
127+
U: Subcommand,
128+
{
129+
fn augment_args(cmd: clap::Command) -> clap::Command {
130+
T::augment_args(cmd).defer(|cmd| {
131+
U::augment_subcommands(
132+
cmd.disable_help_subcommand(true),
133+
)
134+
})
135+
}
136+
fn augment_args_for_update(
137+
_: clap::Command,
138+
) -> clap::Command {
139+
unimplemented!()
140+
}
141+
}
142+
143+
impl<T, U> FromArgMatches for ReClap<T, U>
144+
where
145+
T: Args,
146+
U: Subcommand,
147+
{
148+
fn from_arg_matches(
149+
matches: &clap::ArgMatches,
150+
) -> Result<Self, clap::Error> {
151+
let prev = T::from_arg_matches(matches)?;
152+
let next = if let Some((_name, _sub)) =
153+
matches.subcommand()
154+
{
155+
Some(U::from_arg_matches(matches)?)
156+
} else {
157+
None
158+
};
159+
Ok(Self {
160+
prev,
161+
next: next.map(Box::new),
162+
})
163+
}
164+
fn update_from_arg_matches(
165+
&mut self,
166+
_: &clap::ArgMatches,
167+
) -> Result<(), clap::Error> {
168+
unimplemented!()
169+
}
170+
}

0 commit comments

Comments
 (0)