use std::path::{Path, PathBuf};
use std::sync::Arc;
use thiserror::Error;
mod parse;
pub use parse::parse_config;
#[derive(Debug, Error)]
pub enum DepsError {
#[error("could not read cljrs.edn: {0}")]
Io(#[from] std::io::Error),
#[error("parse error in cljrs.edn: {0}")]
Parse(String),
}
pub type DepsResult<T> = Result<T, DepsError>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitDep {
pub url: Arc<str>,
pub sha: Arc<str>,
pub rust_init: Option<Arc<str>>,
pub rust_crate_dir: Option<Arc<str>>,
pub rust_load_dylib: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Dependency {
Git(GitDep),
Local {
root: PathBuf,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrustedSigner {
Inline(String),
File(PathBuf),
}
#[derive(Debug, Clone, Default)]
pub struct Alias {
pub extra_paths: Vec<PathBuf>,
pub extra_deps: Vec<(Arc<str>, Dependency)>,
}
#[derive(Debug, Clone)]
pub struct RustConfig {
pub crate_dir: PathBuf,
pub init_fn: Option<Arc<str>>,
}
impl RustConfig {
pub fn crate_name(&self) -> Option<&str> {
self.init_fn
.as_deref()
.map(|s| s.split("::").next().unwrap_or(s))
}
}
#[derive(Debug, Clone, Default)]
pub struct DepsConfig {
pub paths: Vec<PathBuf>,
pub deps: Vec<(Arc<str>, Dependency)>,
pub aliases: Vec<(Arc<str>, Alias)>,
pub verify_commit_signatures: bool,
pub trusted_signers: Vec<TrustedSigner>,
pub enforce_native_versions: bool,
pub rust: Option<RustConfig>,
}
impl DepsConfig {
pub fn find_dep(&self, name: &str) -> Option<&Dependency> {
self.deps
.iter()
.find(|(n, _)| n.as_ref() == name)
.map(|(_, d)| d)
}
}
pub fn find_config_file(start: &Path) -> Option<PathBuf> {
let mut dir: &Path = if start.is_file() {
start.parent()?
} else {
start
};
loop {
let candidate = dir.join("cljrs.edn");
if candidate.exists() {
return Some(candidate);
}
dir = dir.parent()?;
}
}
pub fn load_config(start: &Path) -> DepsResult<Option<DepsConfig>> {
match find_config_file(start) {
None => Ok(None),
Some(path) => {
let src = std::fs::read_to_string(&path)?;
let config = parse_config(&src, &path).map_err(DepsError::Parse)?;
Ok(Some(config))
}
}
}