use std::path::PathBuf;
use clap::ArgMatches;
use ffsend_api::url::Url;
use super::Matcher;
use crate::cmd::arg::{ArgPassword, ArgUrl, CmdArgOption};
#[cfg(feature = "archive")]
use crate::util::env_var_present;
pub struct DownloadMatcher<'a> {
matches: &'a ArgMatches<'a>,
}
impl<'a: 'b, 'b> DownloadMatcher<'a> {
pub fn url(&'a self) -> Url {
ArgUrl::value(self.matches)
}
pub fn guess_host(&'a self, url: Option<Url>) -> Url {
let mut url = url.unwrap_or(self.url());
url.set_path("");
url.set_query(None);
url.set_fragment(None);
url
}
pub fn password(&'a self) -> Option<String> {
ArgPassword::value(self.matches)
}
pub fn output(&'a self) -> PathBuf {
self.matches
.value_of("output")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("./"))
}
#[cfg(feature = "archive")]
pub fn extract(&self) -> bool {
self.matches.is_present("extract") || env_var_present("SNDR_EXTRACT")
}
}
impl<'a> Matcher<'a> for DownloadMatcher<'a> {
fn with(matches: &'a ArgMatches) -> Option<Self> {
matches
.subcommand_matches("download")
.map(|matches| DownloadMatcher { matches })
}
}