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 {
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(())
}
}

View File

@@ -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<Self> {
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(())
}
}

View File

@@ -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<String> {
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?)
}
}
}