Change using Result

This commit is contained in:
fromost
2025-10-09 18:28:35 +08:00
parent 6f8a1eada1
commit 473ef8452b
3 changed files with 31 additions and 25 deletions

View File

@@ -24,13 +24,13 @@ pub(crate) struct App {
} }
impl App { impl App {
pub fn new() -> App { pub fn new() -> Self {
let app_conf = 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() }; else { ApplicationConfig::new() };
Self::initialize(); Self::initialize();
let db_conn = Self::establish_db_connection(app_conf.clone()); let db_conn = Self::establish_db_connection(app_conf.clone());
App { Self {
running: true, running: true,
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)), events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
db_connection: db_conn, db_connection: db_conn,
@@ -67,7 +67,7 @@ impl App {
fn update(&mut self, event: Event) -> Result<()> { fn update(&mut self, event: Event) -> Result<()> {
if let Event::Key(key) = event && event::KeyEventKind::is_press(&key.kind) { if let Event::Key(key) = event && event::KeyEventKind::is_press(&key.kind) {
match key.code { match key.code {
Char('q') => self.quit(), Char('q') => self.quit()?,
_ => {} _ => {}
} }
} }
@@ -129,10 +129,11 @@ impl App {
// event handlers // event handlers
impl App { impl App {
fn quit(&mut self) { fn quit(&mut self) -> Result<()> {
self.running = false; self.running = false;
self.app_config self.app_config
.clone() .clone()
.write_to_file(APP_CONIFG_FILE_PATH.to_path_buf()); .write_to_file(APP_CONIFG_FILE_PATH.to_path_buf())?;
Ok(())
} }
} }

View File

@@ -1,21 +1,22 @@
use std::path::{PathBuf}; use std::path::{PathBuf};
use ini::Ini; use ini::Ini;
use color_eyre::Result;
use crate::config::types::{ApplicationConfig, BasicConfig}; 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; pub mod types;
impl ApplicationConfig { impl ApplicationConfig {
pub fn from_file(path: &PathBuf) -> Self { pub fn from_file(path: &PathBuf) -> Result<Self> {
let conf = Ini::load_from_file(path).unwrap(); let conf = Ini::load_from_file(path)?;
let basic_conf_section = conf.section(Some("Basic")).unwrap(); let basic_conf_section = conf.section(Some("Basic")).unwrap();
let basic_conf = BasicConfig { let basic_conf = BasicConfig {
db_path: basic_conf_section.get("DBPath").unwrap().to_string(), 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 basic_config: basic_conf
} })
} }
pub fn new() -> Self { pub fn new() -> Self {
@@ -25,15 +26,16 @@ impl ApplicationConfig {
tick_rate: 250 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 conf
} }
pub fn write_to_file(self, path: PathBuf) { pub fn write_to_file(self, path: PathBuf) -> Result<()> {
let mut conf = Ini::new(); let mut conf = Ini::new();
conf.with_section(Some("Basic")) conf.with_section(Some("Basic"))
.set("DBPath", self.basic_config.db_path) .set("DBPath", self.basic_config.db_path)
.set("TickRate", self.basic_config.tick_rate.to_string()); .set("TickRate", self.basic_config.tick_rate.to_string());
conf.write_to_file(path).unwrap(); conf.write_to_file(path)?;
Ok(())
} }
} }

View File

@@ -1,7 +1,9 @@
use reqwest::{Client, ClientBuilder, Url}; use reqwest::{Client, Url};
use robotstxt::{DefaultMatcher, RobotsParseHandler}; use robotstxt::{DefaultMatcher};
use crate::constants::{APP_CACHE_PATH, APP_DATA_DIR}; use color_eyre::Result;
use crate::constants::{APP_CACHE_PATH};
#[derive(Clone)]
pub(crate) struct Crawler { pub(crate) struct Crawler {
id: String, id: String,
base_url: Url, base_url: Url,
@@ -14,7 +16,7 @@ impl Crawler {
let crawler = Self { let crawler = Self {
id: id.to_string(), id: id.to_string(),
client: Client::new(), 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, base_url,
}; };
let mut matcher = DefaultMatcher::default(); let mut matcher = DefaultMatcher::default();
@@ -23,7 +25,7 @@ impl Crawler {
crawler crawler
} }
async fn get_robots_txt(id: &str, base_url: &Url) -> String { async fn get_robots_txt(id: &str, base_url: &Url) -> Result<String> {
let local_robots_path = APP_CACHE_PATH.clone() let local_robots_path = APP_CACHE_PATH.clone()
.join(id).join("robots.txt"); .join(id).join("robots.txt");
if !local_robots_path.exists() { if !local_robots_path.exists() {
@@ -31,13 +33,14 @@ impl Crawler {
robots_url.set_path("/robots.txt"); robots_url.set_path("/robots.txt");
let response = reqwest::get(robots_url).await let response = reqwest::get(robots_url).await
.expect(format!("Failed to get robots.txt in `{}/robots.txt`", base_url.as_str()).as_str()); .expect(format!("Failed to get robots.txt in `{}/robots.txt`", base_url.as_str()).as_str());
let content = response.text().await.unwrap(); let content = response.text().await?;
tokio::fs::create_dir_all(local_robots_path.parent().unwrap()).await.unwrap(); tokio::fs::create_dir_all(local_robots_path.parent().unwrap()).await?;
tokio::fs::write(&local_robots_path, &content).await.unwrap(); tokio::fs::write(&local_robots_path, &content).await?;
content Ok(content)
} }
else { else {
tokio::fs::read_to_string(&local_robots_path).await.unwrap() Ok(tokio::fs::read_to_string(&local_robots_path).await?)
} }
} }
} }