idm_rs/
lib.rs

1//! # idm-rs
2//!
3//! `idm-rs` is a crate to download files with Internet Download Manager.
4//! Example usage:
5//! ```rust
6//! use idm_rs::idman;
7//! use std::env;
8//! 
9
10//!let mut downloader = idman::new();
11//!downloader.set_download_file_url("https://www.tonec.com/download/idman317.exe");
12//!downloader.set_download_file_name("Internet Download Manager.exe");
13//!downloader.set_download_file_path(&env::current_dir().unwrap().as_path());
14//!downloader.set_mode(idm_rs::Mode::Silent);
15//!downloader.run();
16//! ```
17
18use std::{
19    error::Error,
20    path::{Path, PathBuf},
21    process::Command,
22};
23
24pub enum Mode {
25    Default,
26    /// Turns on the silent mode when IDM doesn't ask any questions.
27    Silent,
28}
29
30#[allow(non_camel_case_types)]
31pub struct idman {
32    idm_path: PathBuf,
33    mode: Mode,
34    download_file_url: String,
35    download_file_path: Option<PathBuf>,
36    download_file_name: Option<String>,
37}
38
39impl idman {
40    /// Create a new builder.
41    pub fn new() -> Self {
42        idman {
43            idm_path: PathBuf::from(
44                "C:\\Program Files (x86)\\Internet Download Manager\\IDMan.exe",
45            ),
46            mode: Mode::Default,
47            download_file_url: String::new(),
48            download_file_path: None,
49            download_file_name: None,
50        }
51    }
52
53    /// Set the path to the IDMan executable.
54    /// Give this func a full path, with the .exe at the end.
55    /// This function is optional, the default IDMan.exe path is the default install location.
56    pub fn set_idm_path(&mut self, idm_path: &Path) {
57        self.idm_path = idm_path.to_path_buf();
58    }
59
60    /// Set the download mode.
61    /// This function is optional, the default mode is default.
62    pub fn set_mode(&mut self, mode: Mode) {
63        self.mode = mode;
64    }
65
66    /// Set the url to the file.
67    pub fn set_download_file_url(&mut self, url: &str) {
68        self.download_file_url = url.to_string();
69    }
70
71    /// Set the path where the file will be downloaded.
72    /// This function is optional, the default download path is handled by IDM.
73    pub fn set_download_file_path(&mut self, file_path: &Path) {
74        self.download_file_path = Some(file_path.to_path_buf());
75    }
76
77    /// Set the file name, don't forget to add extension at the end.
78    /// If not set, IDM will detect it automatically.
79    /// This function is optional, the default file name is handled by IDM.
80    pub fn set_download_file_name(&mut self, file_name: &str) {
81        self.download_file_name = Some(file_name.to_string());
82    }
83
84    fn process_args(&self) -> Vec<&str> {
85        let mut args: Vec<&str> = Vec::new();
86
87        match &self.mode {
88            Mode::Default => {}
89            Mode::Silent => args.push("/n"),
90        }
91
92        args.push("/d");
93        args.push(&self.download_file_url);
94
95        match &self.download_file_path {
96            Some(x) => {
97                args.push("/p");
98                args.push(x.to_str().unwrap());
99            }
100            None => {}
101        }
102
103        match &self.download_file_name {
104            Some(x) => {
105                args.push("/f");
106                args.push(x);
107            }
108            None => {}
109        }
110
111        args
112    }
113
114    /// Executes the settings and download the file.
115    pub fn run(&mut self) -> Result<(), Box<dyn Error>> {
116        Command::new(self.idm_path.to_str().unwrap())
117            .args(self.process_args())
118            .output()?;
119        Ok(())
120    }
121}