use clap::ArgMatches;
use ffsend_api::action::exists::{Error as ExistsError, Exists as ApiExists};
use ffsend_api::file::remote_file::{FileParseError, RemoteFile};
use crate::client::create_config;
use crate::cmd::matcher::main::MainMatcher;
use crate::cmd::matcher::{exists::ExistsMatcher, Matcher};
use crate::error::ActionError;
#[cfg(feature = "history")]
use crate::history_tool;
pub struct Exists<'a> {
cmd_matches: &'a ArgMatches<'a>,
}
impl<'a> Exists<'a> {
pub fn new(cmd_matches: &'a ArgMatches<'a>) -> Self {
Self { cmd_matches }
}
pub fn invoke(&self) -> Result<(), ActionError> {
let matcher_exists = ExistsMatcher::with(self.cmd_matches).unwrap();
let matcher_main = MainMatcher::with(self.cmd_matches).unwrap();
let url = matcher_exists.url();
let client_config = create_config(&matcher_main);
let client = client_config.client(false);
let file = RemoteFile::parse_url(url, None)?;
let exists_response = ApiExists::new(&file).invoke(&client)?;
let exists = exists_response.exists();
println!("Exists: {:?}", exists);
if exists {
println!("Password: {:?}", exists_response.requires_password());
}
#[cfg(feature = "history")]
{
if exists {
history_tool::add(&matcher_main, file, false);
} else {
history_tool::remove(&matcher_main, &file);
}
}
Ok(())
}
}
#[derive(Debug, Fail)]
pub enum Error {
#[fail(display = "invalid share link")]
InvalidUrl(#[cause] FileParseError),
#[fail(display = "failed to check whether the file exists")]
Exists(#[cause] ExistsError),
}
impl From<FileParseError> for Error {
fn from(err: FileParseError) -> Error {
Error::InvalidUrl(err)
}
}
impl From<ExistsError> for Error {
fn from(err: ExistsError) -> Error {
Error::Exists(err)
}
}