refactor: replace color_eyre::Result with crate::Result

This commit is contained in:
monosans
2025-08-10 22:00:30 +03:00
parent a34acf92ca
commit ea7eeea869
12 changed files with 38 additions and 34 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ pub async fn check_all(
proxies: Vec<Proxy>,
token: tokio_util::sync::CancellationToken,
#[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender<Event>,
) -> color_eyre::Result<Vec<Proxy>> {
) -> crate::Result<Vec<Proxy>> {
if config.checking.check_url.is_none() {
return Ok(proxies);
}
+3 -3
View File
@@ -64,7 +64,7 @@ pub struct Config {
async fn get_output_path(
raw_config: &raw_config::RawConfig,
) -> color_eyre::Result<PathBuf> {
) -> crate::Result<PathBuf> {
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<Self> {
) -> crate::Result<Self> {
let output_path = get_output_path(&raw_config).await?;
let max_concurrent_checks =
@@ -200,7 +200,7 @@ impl From<raw_config::SourceConfig> for Source {
}
}
pub async fn load_config() -> color_eyre::Result<Arc<Config>> {
pub async fn load_config() -> crate::Result<Arc<Config>> {
let raw_config_path = raw_config::get_config_path();
let raw_config = raw_config::read_config(Path::new(&raw_config_path))
.await
+2 -2
View File
@@ -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<PathBuf> {
pub async fn get_cache_path() -> crate::Result<PathBuf> {
static CACHE: tokio::sync::OnceCell<PathBuf> =
tokio::sync::OnceCell::const_new();
Ok(CACHE
.get_or_try_init(async || -> color_eyre::Result<PathBuf> {
.get_or_try_init(async || -> crate::Result<PathBuf> {
let mut path = tokio::task::spawn_blocking(dirs::cache_dir)
.await
.wrap_err("failed to spawn task to get user's cache directory")?
+1 -3
View File
@@ -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<U: reqwest::IntoUrl + Clone + Display>(
url: U,
basic_auth: Option<&BasicAuth>,
headers: Option<&HashMap<String, String>>,
) -> Result<String> {
) -> crate::Result<String> {
let mut attempt: u32 = 0;
loop {
let mut request = http_client.get(url.clone());
+14 -10
View File
@@ -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<PathBuf> {
async fn db_path(self) -> crate::Result<PathBuf> {
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<PathBuf> {
async fn etag_path(self) -> crate::Result<PathBuf> {
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<Event>,
) -> 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<Option<reqwest::header::HeaderValue>> {
) -> crate::Result<Option<reqwest::header::HeaderValue>> {
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<Event>,
) -> 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<maxminddb::Reader<maxminddb::Mmap>> {
) -> crate::Result<maxminddb::Reader<maxminddb::Mmap>> {
let path = self.db_path().await?;
tokio::task::spawn_blocking(move || maxminddb::Reader::open_mmap(path))
.await
+8 -6
View File
@@ -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<T> = color_eyre::Result<T>;
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<config::Config>,
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<config::Config>,
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?;
+1 -1
View File
@@ -61,7 +61,7 @@ fn group_proxies<'a>(
pub async fn save_proxies(
config: Arc<Config>,
mut proxies: Vec<Proxy>,
) -> color_eyre::Result<()> {
) -> crate::Result<()> {
if config.output.sort_by_speed {
proxies.sort_by_key(sort_by_timeout);
} else {
+3 -3
View File
@@ -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<Self, Self::Err> {
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<Self, Self::Error> {
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)
+1 -1
View File
@@ -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<RawConfig> {
pub async fn read_config(path: &Path) -> crate::Result<RawConfig> {
let raw_config =
tokio::fs::read_to_string(path).await.wrap_err_with(move || {
format!("failed to read {} to string", path.display())
+2 -2
View File
@@ -19,7 +19,7 @@ async fn scrape_one(
proxies: Arc<parking_lot::Mutex<HashSet<Proxy>>>,
source: Arc<Source>,
#[cfg(feature = "tui")] tx: tokio::sync::mpsc::UnboundedSender<Event>,
) -> 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<Event>,
) -> color_eyre::Result<Vec<Proxy>> {
) -> crate::Result<Vec<Proxy>> {
let proxies = Arc::new(parking_lot::Mutex::new(HashSet::new()));
let mut join_set = tokio::task::JoinSet::new();
+1 -1
View File
@@ -40,7 +40,7 @@ pub async fn run(
token: tokio_util::sync::CancellationToken,
tx: tokio::sync::mpsc::UnboundedSender<Event>,
mut rx: tokio::sync::mpsc::UnboundedReceiver<Event>,
) -> color_eyre::Result<()> {
) -> crate::Result<()> {
tokio::spawn(tick_event_listener(tx.clone()));
tokio::spawn(crossterm_event_listener(tx));
+1 -1
View File
@@ -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::<Vec<_>>().join(" \u{2192} ")
}