use std::fmt::{Display, Formatter};
use crate::cli::ArgumentsParser;
use clap::Args;
use di::{injectable, Ref};
use serde::{Deserialize, Serialize};
use crate::cli::CommandArguments::{Spectrogram, Transcode, Upload, Verify};
use crate::options::{NotSet, OptionRule, Options, OptionsProvider};
#[derive(Args, Clone, Debug, Default, Deserialize, Serialize)]
pub struct SourceArg {
#[arg(value_name = "SOURCE")]
pub source: Option<String>,
}
#[injectable]
impl SourceArg {
fn new(provider: Ref<OptionsProvider>) -> Self {
provider.get()
}
}
impl Options for SourceArg {
fn get_name() -> String {
"Source Argument".to_owned()
}
fn merge(&mut self, alternative: &Self) {
if self.source.is_none() {
self.source.clone_from(&alternative.source);
}
}
fn apply_defaults(&mut self) {}
#[must_use]
fn validate(&self) -> bool {
let mut errors: Vec<OptionRule> = Vec::new();
if self.source.is_none() {
errors.push(NotSet("Source".to_owned()));
}
OptionRule::show(&errors);
errors.is_empty()
}
fn from_args() -> Option<Self> {
match ArgumentsParser::get() {
Some(
Spectrogram { source, .. }
| Transcode { source, .. }
| Verify { source, .. }
| Upload { source, .. },
) => Some(source),
_ => None,
}
}
#[allow(clippy::absolute_paths)]
fn from_json(json: &str) -> Result<Self, serde_json::error::Error> {
serde_json::from_str(json)
}
fn from_yaml(yaml: &str) -> Result<Self, serde_yaml::Error> {
serde_yaml::from_str(yaml)
}
}
impl Display for SourceArg {
#[allow(clippy::absolute_paths)]
fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
let output = if let Ok(yaml) = serde_yaml::to_string(self) {
yaml
} else {
format!("{self:?}")
};
output.fmt(formatter)
}
}