use alef_core::config::{ResolvedCrateConfig, resolve_output_dir};
use std::path::PathBuf;
pub struct CConsumerContext<'a> {
pub config: &'a ResolvedCrateConfig,
pub header: String,
pub lib_name: String,
pub prefix: String,
}
impl<'a> CConsumerContext<'a> {
pub fn from_config(config: &'a ResolvedCrateConfig) -> Self {
Self {
config,
header: config.ffi_header_name(),
lib_name: config.ffi_lib_name(),
prefix: config.ffi_prefix(),
}
}
}
pub fn free_string_symbol(prefix: &str) -> String {
format!("{prefix}_free_string")
}
pub fn last_error_code_symbol(prefix: &str) -> String {
format!("{prefix}_last_error_code")
}
pub fn last_error_context_symbol(prefix: &str) -> String {
format!("{prefix}_last_error_context")
}
pub fn default_output_dir(config: &ResolvedCrateConfig, default: &str) -> PathBuf {
let resolved = resolve_output_dir(None, &config.name, default);
PathBuf::from(resolved)
}
#[cfg(test)]
mod tests {
use super::*;
use alef_core::config::NewAlefConfig;
fn make_config() -> ResolvedCrateConfig {
let cfg: NewAlefConfig = toml::from_str(
r#"
[workspace]
languages = ["python"]
[[crates]]
name = "my-lib"
sources = ["src/lib.rs"]
"#,
)
.unwrap();
cfg.resolve().unwrap().remove(0)
}
#[test]
fn free_string_symbol_produces_expected_format() {
assert_eq!(free_string_symbol("htm"), "htm_free_string");
}
#[test]
fn last_error_code_symbol_produces_expected_format() {
assert_eq!(last_error_code_symbol("krz"), "krz_last_error_code");
}
#[test]
fn last_error_context_symbol_produces_expected_format() {
assert_eq!(last_error_context_symbol("krz"), "krz_last_error_context");
}
#[test]
fn from_config_reads_ffi_fields() {
let config = make_config();
let ctx = CConsumerContext::from_config(&config);
assert!(!ctx.header.is_empty());
assert!(!ctx.lib_name.is_empty());
assert!(!ctx.prefix.is_empty());
}
#[test]
fn default_output_dir_uses_provided_default() {
let config = make_config();
let dir = default_output_dir(&config, "packages/go/");
assert!(dir.to_string_lossy().contains("go"));
}
}