From 473ef8452bc340be13c872bd821af2b80f173118 Mon Sep 17 00:00:00 2001 From: fromost Date: Thu, 9 Oct 2025 18:28:35 +0800 Subject: [PATCH] Change using Result --- src/app.rs | 13 +++++++------ src/config/mod.rs | 20 +++++++++++--------- src/crawler/mod.rs | 23 +++++++++++++---------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/app.rs b/src/app.rs index 461aeee..8dbc656 100644 --- a/src/app.rs +++ b/src/app.rs @@ -24,13 +24,13 @@ pub(crate) struct App { } impl App { - pub fn new() -> App { + pub fn new() -> Self { let app_conf = - if APP_CONIFG_FILE_PATH.exists() { ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH) } + if APP_CONIFG_FILE_PATH.exists() { ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH).unwrap() } else { ApplicationConfig::new() }; Self::initialize(); let db_conn = Self::establish_db_connection(app_conf.clone()); - App { + Self { running: true, events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)), db_connection: db_conn, @@ -67,7 +67,7 @@ impl App { fn update(&mut self, event: Event) -> Result<()> { if let Event::Key(key) = event && event::KeyEventKind::is_press(&key.kind) { match key.code { - Char('q') => self.quit(), + Char('q') => self.quit()?, _ => {} } } @@ -129,10 +129,11 @@ impl App { // event handlers impl App { - fn quit(&mut self) { + fn quit(&mut self) -> Result<()> { self.running = false; self.app_config .clone() - .write_to_file(APP_CONIFG_FILE_PATH.to_path_buf()); + .write_to_file(APP_CONIFG_FILE_PATH.to_path_buf())?; + Ok(()) } } \ No newline at end of file diff --git a/src/config/mod.rs b/src/config/mod.rs index 59810f0..1740d05 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,21 +1,22 @@ use std::path::{PathBuf}; use ini::Ini; +use color_eyre::Result; use crate::config::types::{ApplicationConfig, BasicConfig}; -use crate::constants::{APP_CONFIG_DIR, APP_CONIFG_FILE_PATH, APP_DATA_DIR}; +use crate::constants::{APP_CONIFG_FILE_PATH, APP_DATA_DIR}; pub mod types; impl ApplicationConfig { - pub fn from_file(path: &PathBuf) -> Self { - let conf = Ini::load_from_file(path).unwrap(); + pub fn from_file(path: &PathBuf) -> Result { + let conf = Ini::load_from_file(path)?; let basic_conf_section = conf.section(Some("Basic")).unwrap(); let basic_conf = BasicConfig { db_path: basic_conf_section.get("DBPath").unwrap().to_string(), - tick_rate: basic_conf_section.get("TickRate").unwrap().parse().unwrap(), + tick_rate: basic_conf_section.get("TickRate").unwrap().parse()?, }; - Self { + Ok(Self { basic_config: basic_conf - } + }) } pub fn new() -> Self { @@ -25,15 +26,16 @@ impl ApplicationConfig { tick_rate: 250 } }; - conf.clone().write_to_file(APP_CONIFG_FILE_PATH.to_path_buf()); + conf.clone().write_to_file(APP_CONIFG_FILE_PATH.to_path_buf()).unwrap(); conf } - pub fn write_to_file(self, path: PathBuf) { + pub fn write_to_file(self, path: PathBuf) -> Result<()> { let mut conf = Ini::new(); conf.with_section(Some("Basic")) .set("DBPath", self.basic_config.db_path) .set("TickRate", self.basic_config.tick_rate.to_string()); - conf.write_to_file(path).unwrap(); + conf.write_to_file(path)?; + Ok(()) } } \ No newline at end of file diff --git a/src/crawler/mod.rs b/src/crawler/mod.rs index aa38899..2ec6086 100644 --- a/src/crawler/mod.rs +++ b/src/crawler/mod.rs @@ -1,7 +1,9 @@ -use reqwest::{Client, ClientBuilder, Url}; -use robotstxt::{DefaultMatcher, RobotsParseHandler}; -use crate::constants::{APP_CACHE_PATH, APP_DATA_DIR}; +use reqwest::{Client, Url}; +use robotstxt::{DefaultMatcher}; +use color_eyre::Result; +use crate::constants::{APP_CACHE_PATH}; +#[derive(Clone)] pub(crate) struct Crawler { id: String, base_url: Url, @@ -14,7 +16,7 @@ impl Crawler { let crawler = Self { id: id.to_string(), client: Client::new(), - robots_txt: Self::get_robots_txt(id, &base_url).await, + robots_txt: Self::get_robots_txt(id, &base_url).await.unwrap(), base_url, }; let mut matcher = DefaultMatcher::default(); @@ -23,7 +25,7 @@ impl Crawler { crawler } - async fn get_robots_txt(id: &str, base_url: &Url) -> String { + async fn get_robots_txt(id: &str, base_url: &Url) -> Result { let local_robots_path = APP_CACHE_PATH.clone() .join(id).join("robots.txt"); if !local_robots_path.exists() { @@ -31,13 +33,14 @@ impl Crawler { robots_url.set_path("/robots.txt"); let response = reqwest::get(robots_url).await .expect(format!("Failed to get robots.txt in `{}/robots.txt`", base_url.as_str()).as_str()); - let content = response.text().await.unwrap(); - tokio::fs::create_dir_all(local_robots_path.parent().unwrap()).await.unwrap(); - tokio::fs::write(&local_robots_path, &content).await.unwrap(); - content + let content = response.text().await?; + tokio::fs::create_dir_all(local_robots_path.parent().unwrap()).await?; + tokio::fs::write(&local_robots_path, &content).await?; + Ok(content) + } else { - tokio::fs::read_to_string(&local_robots_path).await.unwrap() + Ok(tokio::fs::read_to_string(&local_robots_path).await?) } } } \ No newline at end of file