pub struct PathTo<T> {
pub path: PathBuf,
pub data: T,
}
Expand description
An adapter for automatically loading the contents of a file path
Fields§
§path: PathBuf
The path given as an argument by the user
data: T
The data extracted from the file at the path
Implementations§
Source§impl<T> PathTo<JsonOf<T>>
impl<T> PathTo<JsonOf<T>>
Sourcepub fn data(&self) -> &T
pub fn data(&self) -> &T
Returns reference to the inner JSON datatype
§Example
use clap::Parser;
use clap_adapters::prelude::*;
#[derive(Debug, Parser)]
struct Cli {
#[clap(long)]
config: PathTo<JsonOf<serde_json::Value>>,
}
// Create a config file in a temporary directory
let config_dir = tempfile::tempdir()?;
let config_path = config_dir.path().join("config.json");
let config_path_string = config_path.display().to_string();
// Write a test config of {"hello":"world"} to the config file
let config = serde_json::json!({"hello": "world"});
let config_string = serde_json::to_string(&config)?;
std::fs::write(&config_path, &config_string)?;
// Parse our CLI, passing our config file path to --config
let cli = Cli::parse_from(["app", "--config", &config_path_string]);
let data = cli.config.data();
// We should expect the value we get to match what we wrote to the config
assert_eq!(data, &serde_json::json!({"hello":"world"}));
Sourcepub fn into_data(self) -> T
pub fn into_data(self) -> T
Returns reference to the inner JSON datatype
§Example
use clap::Parser;
use clap_adapters::prelude::*;
#[derive(Debug, Parser)]
struct Cli {
#[clap(long)]
config: PathTo<JsonOf<serde_json::Value>>,
}
// Create a config file in a temporary directory
let config_dir = tempfile::tempdir()?;
let config_path = config_dir.path().join("config.json");
let config_path_string = config_path.display().to_string();
// Write a test config of {"hello":"world"} to the config file
let config = serde_json::json!({"hello": "world"});
let config_string = serde_json::to_string(&config)?;
std::fs::write(&config_path, &config_string)?;
// Parse our CLI, passing our config file path to --config
let cli = Cli::parse_from(["app", "--config", &config_path_string]);
let data = cli.config.into_data();
// We should expect the value we get to match what we wrote to the config
assert_eq!(data, serde_json::json!({"hello":"world"}));
Source§impl<T> PathTo<TomlOf<T>>
impl<T> PathTo<TomlOf<T>>
Sourcepub fn data(&self) -> &T
pub fn data(&self) -> &T
Returns a reference to the inner Toml datatype
§Example
use clap::Parser;
use clap_adapters::prelude::*;
#[derive(Debug, Parser)]
struct Cli {
#[clap(long)]
config: PathTo<TomlOf<serde_json::Value>>,
}
// Create a config file in a temporary directory
let config_dir = tempfile::tempdir()?;
let config_path = config_dir.path().join("config.json");
let config_path_string = config_path.display().to_string();
// Write a test config to the config file
let config_string = r#"hello = "world""#;
std::fs::write(&config_path, &config_string)?;
// Parse our CLI, passing our config file path to --config
let cli = Cli::parse_from(["app", "--config", &config_path_string]);
let value = cli.config.data();
// We should expect the value we get to match what we wrote to the config
assert_eq!(value, &serde_json::json!({"hello":"world"}));
Sourcepub fn into_data(self) -> T
pub fn into_data(self) -> T
Returns the owned inner datatype parsed from Toml
§Example
use clap::Parser;
use clap_adapters::prelude::*;
#[derive(Debug, Parser)]
struct Cli {
#[clap(long)]
config: PathTo<TomlOf<serde_json::Value>>,
}
// Create a config file in a temporary directory
let config_dir = tempfile::tempdir()?;
let config_path = config_dir.path().join("config.json");
let config_path_string = config_path.display().to_string();
// Write a test config to the config file
let config_string = r#"hello = "world""#;
std::fs::write(&config_path, &config_string)?;
// Parse our CLI, passing our config file path to --config
let cli = Cli::parse_from(["app", "--config", &config_path_string]);
let data = cli.config.into_data();
// We should expect the value we get to match what we wrote to the config
assert_eq!(data, serde_json::json!({"hello":"world"}));
Source§impl<T> PathTo<YamlOf<T>>
impl<T> PathTo<YamlOf<T>>
Sourcepub fn data(&self) -> &T
pub fn data(&self) -> &T
Returns reference to the inner Yaml datatype
§Example
use clap::Parser;
use clap_adapters::prelude::*;
#[derive(Debug, Parser)]
struct Cli {
#[clap(long)]
config: PathTo<YamlOf<serde_json::Value>>,
}
// Create a config file in a temporary directory
let config_dir = tempfile::tempdir()?;
let config_path = config_dir.path().join("config.json");
let config_path_string = config_path.display().to_string();
// Write a test config to the config file
let config_string = r#"hello: "world""#;
std::fs::write(&config_path, &config_string)?;
// Parse our CLI, passing our config file path to --config
let cli = Cli::parse_from(["app", "--config", &config_path_string]);
let data = cli.config.data();
// We should expect the value we get to match what we wrote to the config
assert_eq!(data, &serde_json::json!({"hello":"world"}));
Sourcepub fn into_data(self) -> T
pub fn into_data(self) -> T
Returns the owned inner datatype parsed from Yaml
§Example
use clap::Parser;
use clap_adapters::prelude::*;
#[derive(Debug, Parser)]
struct Cli {
#[clap(long)]
config: PathTo<YamlOf<serde_json::Value>>,
}
// Create a config file in a temporary directory
let config_dir = tempfile::tempdir()?;
let config_path = config_dir.path().join("config.json");
let config_path_string = config_path.display().to_string();
// Write a test config to the config file
let config_string = r#"hello: "world""#;
std::fs::write(&config_path, &config_string)?;
// Parse our CLI, passing our config file path to --config
let cli = Cli::parse_from(["app", "--config", &config_path_string]);
let data = cli.config.into_data();
// We should expect the value we get to match what we wrote to the config
assert_eq!(data, serde_json::json!({"hello":"world"}));
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for PathTo<T>where
T: Freeze,
impl<T> RefUnwindSafe for PathTo<T>where
T: RefUnwindSafe,
impl<T> Send for PathTo<T>where
T: Send,
impl<T> Sync for PathTo<T>where
T: Sync,
impl<T> Unpin for PathTo<T>where
T: Unpin,
impl<T> UnwindSafe for PathTo<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more