grand refactory to multiple list-based control api

This commit is contained in:
yggverse 2026-03-28 04:42:41 +02:00
parent 827cb182f2
commit b93c1e8481
15 changed files with 271 additions and 282 deletions

40
src/rules/list/source.rs Normal file
View file

@ -0,0 +1,40 @@
use anyhow::{Result, bail};
use std::{path::PathBuf, str::FromStr};
use url::Url;
pub enum Source {
Path(PathBuf),
Url(Url),
}
impl Source {
pub fn from_str(source: &str) -> Result<Self> {
Ok(if source.contains("://") {
Self::Url(Url::from_str(source)?)
} else {
Self::Path(PathBuf::from_str(source)?.canonicalize()?)
})
}
pub async fn get(&self) -> Result<String> {
Ok(match self {
Source::Path(path) => tokio::fs::read_to_string(path).await?,
Source::Url(url) => {
let request = url.as_str();
let response = reqwest::get(request).await?;
let status = response.status();
if status.is_success() {
response.text().await?
} else {
bail!("Could not receive remote list `{request}`: `{status}`")
}
}
})
}
}
impl FromStr for Source {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Source::from_str(s)
}
}