diff --git a/src/checker.rs b/src/checker.rs index e7c3b6d..03875d5 100644 --- a/src/checker.rs +++ b/src/checker.rs @@ -11,7 +11,7 @@ pub async fn check_all( proxies: Vec, token: tokio_util::sync::CancellationToken, #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender, -) -> color_eyre::Result> { +) -> crate::Result> { if config.checking.check_url.is_none() { return Ok(proxies); } diff --git a/src/config.rs b/src/config.rs index 1b7e79a..8d8afff 100644 --- a/src/config.rs +++ b/src/config.rs @@ -64,7 +64,7 @@ pub struct Config { async fn get_output_path( raw_config: &raw_config::RawConfig, -) -> color_eyre::Result { +) -> crate::Result { let output_path = if is_docker().await { let mut path = tokio::task::spawn_blocking(dirs::data_local_dir) .await @@ -104,7 +104,7 @@ impl Config { pub async fn from_raw_config( raw_config: raw_config::RawConfig, - ) -> color_eyre::Result { + ) -> crate::Result { let output_path = get_output_path(&raw_config).await?; let max_concurrent_checks = @@ -200,7 +200,7 @@ impl From for Source { } } -pub async fn load_config() -> color_eyre::Result> { +pub async fn load_config() -> crate::Result> { let raw_config_path = raw_config::get_config_path(); let raw_config = raw_config::read_config(Path::new(&raw_config_path)) .await diff --git a/src/fs.rs b/src/fs.rs index b7591f6..1d5522a 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -4,11 +4,11 @@ use color_eyre::eyre::{OptionExt as _, WrapErr as _}; use crate::config::APP_DIRECTORY_NAME; -pub async fn get_cache_path() -> color_eyre::Result { +pub async fn get_cache_path() -> crate::Result { static CACHE: tokio::sync::OnceCell = tokio::sync::OnceCell::const_new(); Ok(CACHE - .get_or_try_init(async || -> color_eyre::Result { + .get_or_try_init(async || -> crate::Result { let mut path = tokio::task::spawn_blocking(dirs::cache_dir) .await .wrap_err("failed to spawn task to get user's cache directory")? diff --git a/src/http.rs b/src/http.rs index 2b5658f..5c7981f 100644 --- a/src/http.rs +++ b/src/http.rs @@ -4,8 +4,6 @@ use std::{ time::{Duration, SystemTime}, }; -use color_eyre::Result; - use crate::config::Config; const DEFAULT_MAX_RETRIES: u32 = 2; @@ -76,7 +74,7 @@ pub async fn fetch_text( url: U, basic_auth: Option<&BasicAuth>, headers: Option<&HashMap>, -) -> Result { +) -> crate::Result { let mut attempt: u32 = 0; loop { let mut request = http_client.get(url.clone()); diff --git a/src/ipdb.rs b/src/ipdb.rs index 62c1026..4ede496 100644 --- a/src/ipdb.rs +++ b/src/ipdb.rs @@ -23,12 +23,16 @@ impl DbType { const fn url(self) -> &'static str { match self { - Self::Asn => "https://raw.githubusercontent.com/P3TERX/GeoLite.mmdb/download/GeoLite2-ASN.mmdb", - Self::Geo => "https://raw.githubusercontent.com/P3TERX/GeoLite.mmdb/download/GeoLite2-City.mmdb", + Self::Asn => { + "https://raw.githubusercontent.com/P3TERX/GeoLite.mmdb/download/GeoLite2-ASN.mmdb" + } + Self::Geo => { + "https://raw.githubusercontent.com/P3TERX/GeoLite.mmdb/download/GeoLite2-City.mmdb" + } } } - async fn db_path(self) -> color_eyre::Result { + async fn db_path(self) -> crate::Result { let mut cache_path = get_cache_path().await.wrap_err("failed to get cache path")?; match self { @@ -38,7 +42,7 @@ impl DbType { Ok(cache_path) } - async fn etag_path(self) -> color_eyre::Result { + async fn etag_path(self) -> crate::Result { let mut db_path = self.db_path().await.wrap_err_with(move || { format!("failed to get {} database path", self.name()) })?; @@ -50,7 +54,7 @@ impl DbType { self, mut response: reqwest::Response, #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender, - ) -> color_eyre::Result<()> { + ) -> crate::Result<()> { #[cfg(feature = "tui")] drop(tx.send(Event::App(AppEvent::IpDbTotal( self, @@ -84,7 +88,7 @@ impl DbType { Ok(()) } - async fn save_etag(self, etag: impl AsRef<[u8]>) -> color_eyre::Result<()> { + async fn save_etag(self, etag: impl AsRef<[u8]>) -> crate::Result<()> { let path = self.etag_path().await?; tokio::fs::write(&path, etag).await.wrap_err_with(move || { format!("failed to write to file {}", path.display()) @@ -93,7 +97,7 @@ impl DbType { async fn read_etag( self, - ) -> color_eyre::Result> { + ) -> crate::Result> { let path = self.etag_path().await?; match tokio::fs::read_to_string(&path).await { Ok(text) => Ok(text.parse().ok()), @@ -104,7 +108,7 @@ impl DbType { } } - async fn remove_etag(self) -> color_eyre::Result<()> { + async fn remove_etag(self) -> crate::Result<()> { let path = self.etag_path().await?; match tokio::fs::remove_file(&path).await { Ok(()) => Ok(()), @@ -119,7 +123,7 @@ impl DbType { self, http_client: reqwest::Client, #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender, - ) -> color_eyre::Result<()> { + ) -> crate::Result<()> { let db_path = self.db_path().await?; let mut headers = reqwest::header::HeaderMap::new(); #[expect(clippy::collapsible_if)] @@ -206,7 +210,7 @@ impl DbType { pub async fn open_mmap( self, - ) -> color_eyre::Result> { + ) -> crate::Result> { let path = self.db_path().await?; tokio::task::spawn_blocking(move || maxminddb::Reader::open_mmap(path)) .await diff --git a/src/main.rs b/src/main.rs index 83d0d02..4d53c7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,7 +62,6 @@ mod scraper; #[cfg(feature = "tui")] mod tui; mod utils; - use std::sync::Arc; use color_eyre::eyre::WrapErr as _; @@ -78,6 +77,9 @@ use tracing_subscriber::{ #[global_allocator] static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; +type Error = color_eyre::Report; +type Result = color_eyre::Result; + fn create_logging_filter( config: &config::Config, ) -> tracing_subscriber::filter::Targets { @@ -110,7 +112,7 @@ async fn download_output_dependencies( #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender< event::Event, >, -) -> color_eyre::Result<()> { +) -> crate::Result<()> { let mut output_dependencies_tasks = tokio::task::JoinSet::new(); if config.asn_enabled() { @@ -158,7 +160,7 @@ async fn main_task( #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender< event::Event, >, -) -> color_eyre::Result<()> { +) -> crate::Result<()> { let http_client = http::create_reqwest_client(&config) .wrap_err("failed to create reqwest HTTP client")?; @@ -260,7 +262,7 @@ fn watch_signals( async fn run_with_tui( config: Arc, logging_filter: tracing_subscriber::filter::Targets, -) -> color_eyre::Result<()> { +) -> crate::Result<()> { tui_logger::init_logger(tui_logger::LevelFilter::Debug) .wrap_err("failed to initialize tui_logger")?; tracing_subscriber::registry() @@ -294,7 +296,7 @@ async fn run_with_tui( async fn run_without_tui( config: Arc, logging_filter: tracing_subscriber::filter::Targets, -) -> color_eyre::Result<()> { +) -> crate::Result<()> { tracing_subscriber::registry() .with(logging_filter) .with(tracing_subscriber::fmt::layer()) @@ -309,7 +311,7 @@ async fn run_without_tui( } #[tokio::main] -async fn main() -> color_eyre::Result<()> { +async fn main() -> crate::Result<()> { color_eyre::install().wrap_err("failed to install color_eyre hooks")?; let config = config::load_config().await?; diff --git a/src/output.rs b/src/output.rs index e92491d..765bca8 100644 --- a/src/output.rs +++ b/src/output.rs @@ -61,7 +61,7 @@ fn group_proxies<'a>( pub async fn save_proxies( config: Arc, mut proxies: Vec, -) -> color_eyre::Result<()> { +) -> crate::Result<()> { if config.output.sort_by_speed { proxies.sort_by_key(sort_by_timeout); } else { diff --git a/src/proxy.rs b/src/proxy.rs index 694d2e9..92171c1 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -22,7 +22,7 @@ pub enum ProxyType { } impl FromStr for ProxyType { - type Err = color_eyre::Report; + type Err = crate::Error; fn from_str(s: &str) -> Result { match s.to_ascii_lowercase().as_str() { @@ -60,7 +60,7 @@ pub struct Proxy { } impl TryFrom<&mut Proxy> for reqwest::Proxy { - type Error = color_eyre::Report; + type Error = crate::Error; fn try_from(value: &mut Proxy) -> Result { let proxy = Self::all(format!( @@ -84,7 +84,7 @@ impl Proxy { self.timeout.is_some() } - pub async fn check(&mut self, config: &Config) -> color_eyre::Result<()> { + pub async fn check(&mut self, config: &Config) -> crate::Result<()> { if let Some(check_url) = &config.checking.check_url { let client = reqwest::ClientBuilder::new() .user_agent(&config.checking.user_agent) diff --git a/src/raw_config.rs b/src/raw_config.rs index 2be67e7..7d5ee26 100644 --- a/src/raw_config.rs +++ b/src/raw_config.rs @@ -180,7 +180,7 @@ pub fn get_config_path() -> String { env::var(CONFIG_ENV).unwrap_or_else(|_| "config.toml".to_owned()) } -pub async fn read_config(path: &Path) -> color_eyre::Result { +pub async fn read_config(path: &Path) -> crate::Result { let raw_config = tokio::fs::read_to_string(path).await.wrap_err_with(move || { format!("failed to read {} to string", path.display()) diff --git a/src/scraper.rs b/src/scraper.rs index e0758d1..189a61b 100644 --- a/src/scraper.rs +++ b/src/scraper.rs @@ -19,7 +19,7 @@ async fn scrape_one( proxies: Arc>>, source: Arc, #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender, -) -> color_eyre::Result<()> { +) -> crate::Result<()> { let text_result = if let Ok(u) = url::Url::parse(&source.url) { match u.scheme() { "http" | "https" => { @@ -119,7 +119,7 @@ pub async fn scrape_all( http_client: reqwest::Client, token: tokio_util::sync::CancellationToken, #[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender, -) -> color_eyre::Result> { +) -> crate::Result> { let proxies = Arc::new(parking_lot::Mutex::new(HashSet::new())); let mut join_set = tokio::task::JoinSet::new(); diff --git a/src/tui.rs b/src/tui.rs index 0cfd697..9482720 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -40,7 +40,7 @@ pub async fn run( token: tokio_util::sync::CancellationToken, tx: tokio::sync::mpsc::UnboundedSender, mut rx: tokio::sync::mpsc::UnboundedReceiver, -) -> color_eyre::Result<()> { +) -> crate::Result<()> { tokio::spawn(tick_event_listener(tx.clone())); tokio::spawn(crossterm_event_listener(tx)); diff --git a/src/utils.rs b/src/utils.rs index 4090fdb..6df03d5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,6 +16,6 @@ pub async fn is_docker() -> bool { } } -pub fn pretty_error(e: &color_eyre::Report) -> String { +pub fn pretty_error(e: &crate::Error) -> String { e.chain().map(ToString::to_string).collect::>().join(" \u{2192} ") }