use std::collections::BTreeMap;
use std::path::PathBuf;
use std::time::Duration;
use crate::error::KitResult;
mod paths;
pub use paths::KitPaths;
pub const DEFAULT_LOCK_TIMEOUT: Duration = Duration::from_secs(30);
#[derive(Debug, Clone)]
pub struct KitConfig {
pub id: String,
pub manifest_name: String,
pub crate_prefix: String,
pub entry_symbol: Vec<u8>,
pub lib_stem_prefix: String,
pub contract_crate: String,
pub contract_version: String,
pub wrapper_edition: String,
pub wrapper_body: String,
pub data_dir: Option<PathBuf>,
pub lock_timeout: Duration,
pub prefer_prebuilt: bool,
pub target_triple: Option<String>,
pub registry: String,
pub local_overrides: BTreeMap<String, PathBuf>,
}
impl KitConfig {
pub fn new(id: impl Into<String>) -> Self {
let id = id.into();
let snake = id.replace('-', "_");
Self {
manifest_name: format!("{id}-plugin.toml"),
crate_prefix: format!("{id}-plugin-"),
entry_symbol: format!("{snake}_plugin_entry_v1").into_bytes(),
lib_stem_prefix: format!("{snake}_plugin_"),
contract_crate: format!("{id}-plugin"),
contract_version: "0.1".to_string(),
wrapper_edition: "2021".to_string(),
wrapper_body: format!("{snake}_plugin::export!({{crate_ident}}::create);\n"),
data_dir: None,
lock_timeout: DEFAULT_LOCK_TIMEOUT,
prefer_prebuilt: true,
target_triple: None,
registry: "https://crates.io".to_string(),
local_overrides: BTreeMap::new(),
id,
}
}
pub fn with_data_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.data_dir = Some(dir.into());
self
}
pub fn with_lock_timeout(mut self, t: Duration) -> Self {
self.lock_timeout = t;
self
}
pub fn effective_target(&self) -> &str {
self.target_triple
.as_deref()
.unwrap_or(crate::TARGET_TRIPLE)
}
pub fn normalize_crate_name(&self, name: &str) -> String {
if name.starts_with(&self.crate_prefix) {
name.to_string()
} else {
format!("{}{}", self.crate_prefix, name)
}
}
pub fn lib_stem(&self, crate_name: &str) -> String {
let tail = crate_name
.strip_prefix(&self.crate_prefix)
.unwrap_or(crate_name);
format!("{}{}", self.lib_stem_prefix, tail.replace('-', "_"))
}
pub fn paths(&self) -> KitResult<KitPaths> {
KitPaths::resolve(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn cfg() -> KitConfig {
KitConfig::new("myapp")
}
#[test]
fn new_derives_names_from_the_id() {
let c = cfg();
assert_eq!(c.id, "myapp");
assert_eq!(c.manifest_name, "myapp-plugin.toml");
assert_eq!(c.crate_prefix, "myapp-plugin-");
assert_eq!(c.lib_stem_prefix, "myapp_plugin_");
assert_eq!(c.contract_crate, "myapp-plugin");
}
#[test]
fn normalizes_short_and_full_crate_names() {
let c = cfg();
assert_eq!(c.normalize_crate_name("foo"), "myapp-plugin-foo");
assert_eq!(
c.normalize_crate_name("myapp-plugin-foo"),
"myapp-plugin-foo"
);
}
#[test]
fn derives_lib_stem_from_crate_name() {
let c = cfg();
assert_eq!(c.lib_stem("myapp-plugin-foo"), "myapp_plugin_foo");
assert_eq!(c.lib_stem("myapp-plugin-a-b"), "myapp_plugin_a_b");
}
#[test]
fn target_falls_back_to_compile_time_triple() {
let c = cfg();
assert_eq!(c.effective_target(), crate::TARGET_TRIPLE);
let mut overridden = cfg();
overridden.target_triple = Some("aarch64-apple-darwin".to_string());
assert_eq!(overridden.effective_target(), "aarch64-apple-darwin");
}
#[test]
fn with_lock_timeout_replaces_the_default() {
let c = cfg().with_lock_timeout(Duration::from_millis(5));
assert_eq!(c.lock_timeout, Duration::from_millis(5));
}
}