use std::{
error::Error,
path::{Path, PathBuf},
process::Command,
};
pub enum Mode {
Default,
Silent,
}
#[allow(non_camel_case_types)]
pub struct idman {
idm_path: PathBuf,
mode: Mode,
download_file_url: String,
download_file_path: Option<PathBuf>,
download_file_name: Option<String>,
}
impl idman {
pub fn new() -> Self {
idman {
idm_path: PathBuf::from(
"C:\\Program Files (x86)\\Internet Download Manager\\IDMan.exe",
),
mode: Mode::Default,
download_file_url: String::new(),
download_file_path: None,
download_file_name: None,
}
}
pub fn set_idm_path(&mut self, idm_path: &Path) {
self.idm_path = idm_path.to_path_buf();
}
pub fn set_mode(&mut self, mode: Mode) {
self.mode = mode;
}
pub fn set_download_file_url(&mut self, url: &str) {
self.download_file_url = url.to_string();
}
pub fn set_download_file_path(&mut self, file_path: &Path) {
self.download_file_path = Some(file_path.to_path_buf());
}
pub fn set_download_file_name(&mut self, file_name: &str) {
self.download_file_name = Some(file_name.to_string());
}
fn process_args(&self) -> Vec<&str> {
let mut args: Vec<&str> = Vec::new();
match &self.mode {
Mode::Default => {}
Mode::Silent => args.push("/n"),
}
args.push("/d");
args.push(&self.download_file_url);
match &self.download_file_path {
Some(x) => {
args.push("/p");
args.push(x.to_str().unwrap());
}
None => {}
}
match &self.download_file_name {
Some(x) => {
args.push("/f");
args.push(x);
}
None => {}
}
args
}
pub fn run(&mut self) -> Result<(), Box<dyn Error>> {
Command::new(self.idm_path.to_str().unwrap())
.args(self.process_args())
.output()?;
Ok(())
}
}