diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5afd867 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,55 @@ +use std::{io, env, fmt, error::Error}; +use toml::{self, de}; + +#[derive(Debug)] +pub enum WsError { + Reqwest(reqwest::Error), + Url(url::ParseError), + Io(io::Error), + Toml(de::Error), + Var(env::VarError), +} + +impl From for WsError { + fn from(value: url::ParseError) -> Self { + Self::Url(value) + } +} + +impl From for WsError { + fn from(value: reqwest::Error) -> Self { + Self::Reqwest(value) + } +} + +impl From for WsError { + fn from(value: io::Error) -> Self { + Self::Io(value) + } +} + +impl From for WsError { + fn from(value: toml::de::Error) -> Self { + Self::Toml(value) + } +} + +impl From for WsError { + fn from(value: env::VarError) -> Self { + Self::Var(value) + } +} + +impl Error for WsError {} + +impl fmt::Display for WsError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + WsError::Reqwest(e) => write!(f, "reqwest error: {}", e), + WsError::Url(e) => write!(f, "url error: {}", e), + WsError::Io(e) => write!(f, "io error: {}", e), + WsError::Toml(e) => write!(f, "toml error: {}", e), + WsError::Var(e) => write!(f, "var error: {}", e), + } + } +} diff --git a/src/main.rs b/src/main.rs index bcfc123..b7e5fb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,10 +14,10 @@ use prometheus::{ }; use reqwest::header::USER_AGENT; use serde::Deserialize; +use vrchat_prometheus_adapter::WsError; use std::collections::HashMap; -use std::error::Error; use std::sync::OnceLock; -use std::{env, fmt, fs, io}; +use std::{env, fs}; use url::Url; static PLAYER_COUNT: OnceLock = OnceLock::new(); @@ -26,59 +26,6 @@ static WORLD_VISITS: OnceLock = OnceLock::new(); static WORLD_OCCUPANTS: OnceLock = OnceLock::new(); static WORLD_FAVORITES: OnceLock = OnceLock::new(); -#[derive(Debug)] -enum WsError { - Reqwest(reqwest::Error), - Url(url::ParseError), - Io(io::Error), - Toml(toml::de::Error), - Var(env::VarError), -} - -impl From for WsError { - fn from(value: url::ParseError) -> Self { - Self::Url(value) - } -} - -impl From for WsError { - fn from(value: reqwest::Error) -> Self { - Self::Reqwest(value) - } -} - -impl From for WsError { - fn from(value: io::Error) -> Self { - Self::Io(value) - } -} - -impl From for WsError { - fn from(value: toml::de::Error) -> Self { - Self::Toml(value) - } -} - -impl From for WsError { - fn from(value: env::VarError) -> Self { - Self::Var(value) - } -} - -impl Error for WsError {} - -impl fmt::Display for WsError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - WsError::Reqwest(e) => write!(f, "reqwest error: {}", e), - WsError::Url(e) => write!(f, "url error: {}", e), - WsError::Io(e) => write!(f, "io error: {}", e), - WsError::Toml(e) => write!(f, "toml error: {}", e), - WsError::Var(e) => write!(f, "var error: {}", e), - } - } -} - #[derive(Clone, Debug, Deserialize)] struct Config { /// Groups we want to track.