Skip to content

Commit

Permalink
feat: add config file support
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Oct 24, 2024
1 parent f0b242e commit 94f3eed
Show file tree
Hide file tree
Showing 51 changed files with 1,432 additions and 868 deletions.
2 changes: 2 additions & 0 deletions duvet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ http = ["dep:http", "reqwest"]
testing = ["tracing-subscriber"]

[dependencies]
anyhow = "1"
blake3 = "1"
bytes = "1"
duvet-macros = { version = "0.1", path = "../duvet-macros" }
Expand All @@ -24,6 +25,7 @@ miette = { version = "7", features = ["fancy"] }
once_cell = "1"
reqwest = { version = "0.12", optional = true }
serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
tokio = { version = "1", features = ["fs", "sync"] }
tokio-util = "0.7"
toml_edit = { version = "0.22", features = ["parse", "serde"] }
Expand Down
29 changes: 19 additions & 10 deletions duvet-core/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ impl Diagnostic for Error {
}
}

impl From<anyhow::Error> for Error {
fn from(value: anyhow::Error) -> Self {
Report::msg(value).into()
}
}

impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Report::msg(value).into()
}
}

impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Report::msg(value).into()
}
}

impl From<Report> for Error {
fn from(err: Report) -> Self {
Self(Arc::new(err))
Expand Down Expand Up @@ -210,7 +228,7 @@ impl From<Vec<Error>> for Set {
impl fmt::Display for Set {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for error in self.errors.iter() {
writeln!(f, "{}", error)?;
writeln!(f, "{:?}", error)?;
}
Ok(())
}
Expand All @@ -227,12 +245,3 @@ impl StdError for Set {
Some(&self.main)
}
}

/*
impl Diagnostic for Set {
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
let iter = self.errors.iter().map(|e| e as &dyn Diagnostic);
Some(Box::new(iter))
}
}
*/
29 changes: 29 additions & 0 deletions duvet-core/src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{diagnostic::IntoDiagnostic, path::Path, Result};
use core::cell::RefCell;
use once_cell::sync::Lazy;
use std::sync::Arc;

static GLOBAL_ARGS: Lazy<Arc<[String]>> = Lazy::new(|| std::env::args().collect());
static GLOBAL_DIR: Lazy<Result<Path>> =
Lazy::new(|| std::env::current_dir().map(|v| v.into()).into_diagnostic());

thread_local! {
static ARGS: RefCell<Arc<[String]>> = RefCell::new(GLOBAL_ARGS.clone());
static DIR: RefCell<Result<Path>> = RefCell::new(GLOBAL_DIR.clone());
}

pub fn args() -> Arc<[String]> {
ARGS.with(|current| current.borrow().clone())
}

pub fn set_args(args: Arc<[String]>) {
ARGS.with(|current| *current.borrow_mut() = args);
}

pub fn current_dir() -> Result<Path> {
DIR.with(|current| current.borrow().clone())
}

pub fn set_current_dir(dir: Path) {
DIR.with(|current| *current.borrow_mut() = Ok(dir));
}
20 changes: 20 additions & 0 deletions duvet-core/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ impl SourceFile {
.await
}

pub async fn as_json<T>(&self) -> crate::Result<Arc<T>>
where
T: 'static + Send + Sync + serde::de::DeserializeOwned,
{
let path = self.path.clone();
let contents = self.contents.clone();
// TODO can we get better errors by mapping string ranges?
crate::Cache::current()
.get_or_init(*self.hash(), move || {
crate::Query::from(
serde_json::from_slice(contents.data())
.map(Arc::new)
.into_diagnostic()
.wrap_err(path)
.map_err(|err| err.into()),
)
})
.await
}

pub fn substr(&self, v: &str) -> Option<Slice<SourceFile>> {
unsafe {
let beginning = self.as_bytes().as_ptr();
Expand Down
34 changes: 27 additions & 7 deletions duvet-core/src/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,34 @@ use globset as g;
use serde::de;
use std::{str::FromStr, sync::Arc};

#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct Glob {
set: Arc<g::GlobSet>,
set: Arc<(g::GlobSet, Vec<String>)>,
}

impl fmt::Debug for Glob {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Glob").field(&self.set.1).finish()
}
}

impl Glob {
pub fn is_match<P: AsRef<std::path::Path>>(&self, path: &P) -> bool {
self.set.is_match(path)
self.set.0.is_match(path)
}

pub fn try_from_iter<T: IntoIterator<Item = I>, I: AsRef<str>>(
iter: T,
) -> Result<Glob, g::Error> {
let mut builder = g::GlobSetBuilder::new();
let mut display = vec![];
for item in iter {
builder.add(g::Glob::new(item.as_ref())?);
let value = format_value(item.as_ref());
builder.add(g::Glob::new(&value)?);
display.push(value);
}
let set = builder.build()?;
let set = Arc::new(set);
let set = Arc::new((set, display));
Ok(Self { set })
}
}
Expand Down Expand Up @@ -67,12 +76,23 @@ impl<'de> de::Visitor<'de> for StringOrList {
S: de::SeqAccess<'de>,
{
let mut builder = g::GlobSetBuilder::new();
let mut display = vec![];
while let Some(value) = seq.next_element()? {
let item = g::Glob::new(value).map_err(serde::de::Error::custom)?;
let value = format_value(value);
let item = g::Glob::new(&value).map_err(serde::de::Error::custom)?;
builder.add(item);
display.push(value);
}
let set = builder.build().map_err(serde::de::Error::custom)?;
let set = Arc::new(set);
let set = Arc::new((set, display));
Ok(Glob { set })
}
}

fn format_value(v: &str) -> String {
if v.starts_with("**/") || v.starts_with('/') {
v.to_string()
} else {
format!("**/{v}")
}
}
3 changes: 2 additions & 1 deletion duvet-core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ pub struct Hash([u8; HASH_LEN]);

impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x")?;
for byte in &self.0 {
write!(f, "{byte:#02x}")?;
write!(f, "{byte:02x}")?;
}
Ok(())
}
Expand Down
25 changes: 21 additions & 4 deletions duvet-core/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@ use std::sync::Arc;
pub use http::response::Parts;
pub use reqwest::Client;

fn default_headers() -> reqwest::header::HeaderMap {
let mut map = reqwest::header::HeaderMap::new();

map.insert("accept", "text/plain".parse().unwrap());

map
}

pub fn client() -> Query<Client> {
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
struct Q;

// TODO configure the client more
// - User-Agent headers
// - Accept headers?
Cache::current().get_or_init(Q, || Query::from(Client::builder().build().unwrap()))
Cache::current().get_or_init(Q, || {
Query::from(
Client::builder()
.user_agent(concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION")
))
.default_headers(default_headers())
.build()
.unwrap(),
)
})
}

pub fn get_full<U>(url: U) -> Query<Result<(Arc<Parts>, Contents)>>
Expand Down
3 changes: 2 additions & 1 deletion duvet-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod cache;
pub mod contents;
pub mod diagnostic;
pub mod dir;
pub mod env;
pub mod file;
pub mod glob;
pub mod hash;
Expand All @@ -36,4 +37,4 @@ pub use cache::Cache;
pub use duvet_macros::*;
pub use query::Query;

pub type Result<T, E = diagnostic::Error> = core::result::Result<T, E>;
pub type Result<T = (), E = diagnostic::Error> = core::result::Result<T, E>;
10 changes: 6 additions & 4 deletions duvet-core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,16 @@ mod tests {
let query = Query::new(async move { rx.await.unwrap() });

let a = query.clone();
let a = tokio::spawn(async move { *a.get().await });
let a = async move { *a.get().await };

let b = query;
let b = tokio::spawn(async move { *b.get().await });
let b = async move { *b.get().await };

tx.send(123).unwrap();

assert_eq!(a.await.unwrap(), 123);
assert_eq!(b.await.unwrap(), 123);
let (a, b) = tokio::join!(a, b);

assert_eq!(a, 123);
assert_eq!(b, 123);
}
}
4 changes: 4 additions & 0 deletions duvet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version = "1"

[source]
pattern = "src/**/*.rs"
5 changes: 4 additions & 1 deletion duvet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ anyhow = "1"
clap = { version = "4", features = ["derive"] }
duvet-core = { version = "0.1", path = "../duvet-core" }
fnv = { version = "1", default-features = false }
futures = { version = "0.3" }
glob = "0.3"
lazy_static = "1"
once_cell = "1"
Expand All @@ -27,7 +28,9 @@ reqwest = { version = "0.12", features = ["blocking", "native-tls"] }
serde = { version = "1", features = ["derive"] }
slug = { version = "0.1" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
toml = "0.5"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
toml = "0.8"
triple_accel = "0.4"
url = "2"
v_jsonescape = "0.7"
Expand Down
Loading

0 comments on commit 94f3eed

Please sign in to comment.