init Preload with validate related argument options

This commit is contained in:
yggverse 2025-07-07 13:11:01 +03:00
parent 43d94ee9b6
commit a112b49eba
2 changed files with 45 additions and 15 deletions

View file

@ -9,9 +9,10 @@ mod torrent;
mod trackers; mod trackers;
use anyhow::Result; use anyhow::Result;
use config::Config;
use debug::Debug; use debug::Debug;
use index::Index; use index::Index;
use preload::Preload; use peers::Peers;
use rss::Rss; use rss::Rss;
use std::{collections::HashSet, num::NonZero, path::PathBuf, time::Duration}; use std::{collections::HashSet, num::NonZero, path::PathBuf, time::Duration};
use torrent::Torrent; use torrent::Torrent;
@ -28,20 +29,17 @@ async fn main() -> Result<()> {
use tokio::time; use tokio::time;
// init components // init components
let config = config::Config::parse(); let config = Config::parse();
let debug = Debug::init(&config.debug)?; let debug = Debug::init(&config.debug)?;
let peers = peers::Peers::init(&config.initial_peer)?; let peers = Peers::init(&config.initial_peer)?;
let preload = config.preload.map(|ref p| { let preload = preload::init(
Preload::init( config.preload,
p,
config.preload_regex, config.preload_regex,
config.preload_max_filecount, config.preload_max_filecount,
config.preload_max_filesize, config.preload_max_filesize,
config.preload_total_size, config.preload_total_size,
config.clear, config.clear,
) )?;
.unwrap()
});
let trackers = Trackers::init(&config.tracker)?; let trackers = Trackers::init(&config.tracker)?;
let torrent = config.export_torrents.map(|p| Torrent::init(&p).unwrap()); let torrent = config.export_torrents.map(|p| Torrent::init(&p).unwrap());
let session = librqbit::Session::new_with_opts( let session = librqbit::Session::new_with_opts(

View file

@ -11,7 +11,7 @@ pub struct Preload {
} }
impl Preload { impl Preload {
pub fn init( fn init(
directory: &str, directory: &str,
regex: Option<String>, regex: Option<String>,
max_filecount: Option<usize>, max_filecount: Option<usize>,
@ -89,3 +89,35 @@ impl Preload {
self.regex.as_ref().is_some_and(|r| r.is_match(pattern)) self.regex.as_ref().is_some_and(|r| r.is_match(pattern))
} }
} }
/// Init `Preload` with validate related argument options
pub fn init(
path: Option<String>,
regex: Option<String>,
max_filecount: Option<usize>,
max_filesize: Option<u64>,
total_size: Option<u64>,
clear: bool,
) -> Result<Option<Preload>> {
Ok(match path {
Some(ref p) => Some(Preload::init(
p,
regex,
max_filecount,
max_filesize,
total_size,
clear,
)?),
None => {
if regex.is_some()
|| max_filecount.is_some()
|| max_filesize.is_some()
|| total_size.is_some()
|| clear
{
bail!("`--preload` directory is required for this configuration!")
}
None
}
})
}