use alef_backend_zig::ZigBackend;
use alef_core::backend::Backend;
use alef_core::config::{AlefConfig, CrateConfig};
use alef_core::ir::{
ApiSurface, CoreWrapper, EnumDef, EnumVariant, ErrorDef, ErrorVariant, FieldDef, FunctionDef, ParamDef,
PrimitiveType, TypeDef, TypeRef,
};
fn make_field(name: &str, ty: TypeRef, optional: bool) -> FieldDef {
FieldDef {
name: name.to_string(),
ty,
optional,
default: None,
doc: String::new(),
sanitized: false,
is_boxed: false,
type_rust_path: None,
cfg: None,
typed_default: None,
core_wrapper: CoreWrapper::None,
vec_inner_core_wrapper: CoreWrapper::None,
newtype_wrapper: None,
}
}
fn make_param(name: &str, ty: TypeRef) -> ParamDef {
ParamDef {
name: name.to_string(),
ty,
optional: false,
default: None,
sanitized: false,
typed_default: None,
is_ref: false,
is_mut: false,
newtype_wrapper: None,
original_type: None,
}
}
fn make_basic_api() -> ApiSurface {
ApiSurface {
crate_name: "demo".into(),
version: "0.1.0".into(),
types: vec![TypeDef {
name: "Config".to_string(),
rust_path: "demo::Config".to_string(),
original_rust_path: String::new(),
fields: vec![
make_field("value", TypeRef::Primitive(PrimitiveType::I32), false),
make_field("label", TypeRef::String, false),
make_field("tag", TypeRef::Optional(Box::new(TypeRef::String)), true),
],
methods: vec![],
is_opaque: false,
is_clone: true,
is_copy: false,
doc: "A demo configuration struct.".to_string(),
cfg: None,
is_trait: false,
has_default: false,
has_stripped_cfg_fields: false,
is_return_type: false,
serde_rename_all: None,
has_serde: false,
super_traits: vec![],
}],
functions: vec![FunctionDef {
name: "process".into(),
rust_path: "demo::process".into(),
original_rust_path: String::new(),
params: vec![
make_param("input", TypeRef::String),
make_param("count", TypeRef::Primitive(PrimitiveType::U32)),
],
return_type: TypeRef::String,
is_async: false,
error_type: Some("DemoError".to_string()),
doc: "Process input and return a result.".to_string(),
cfg: None,
sanitized: false,
return_sanitized: false,
returns_ref: false,
returns_cow: false,
return_newtype_wrapper: None,
}],
enums: vec![EnumDef {
name: "Status".to_string(),
rust_path: "demo::Status".to_string(),
original_rust_path: String::new(),
variants: vec![
EnumVariant {
name: "Active".to_string(),
fields: vec![],
is_tuple: false,
doc: "Active state.".to_string(),
is_default: false,
serde_rename: None,
},
EnumVariant {
name: "Inactive".to_string(),
fields: vec![],
is_tuple: false,
doc: "Inactive state.".to_string(),
is_default: false,
serde_rename: None,
},
],
doc: "Processing status.".to_string(),
cfg: None,
is_copy: false,
has_serde: false,
serde_tag: None,
serde_rename_all: None,
}],
errors: vec![ErrorDef {
name: "DemoError".to_string(),
rust_path: "demo::DemoError".to_string(),
original_rust_path: String::new(),
variants: vec![
ErrorVariant {
name: "InvalidInput".to_string(),
message_template: Some("invalid input provided".to_string()),
fields: vec![],
has_source: false,
has_from: false,
is_unit: true,
doc: "Input validation failed.".to_string(),
},
ErrorVariant {
name: "ProcessingFailed".to_string(),
message_template: Some("processing failed".to_string()),
fields: vec![],
has_source: false,
has_from: false,
is_unit: true,
doc: "Processing encountered an error.".to_string(),
},
],
doc: "Errors emitted by demo operations.".to_string(),
}],
}
}
fn make_basic_config() -> AlefConfig {
AlefConfig {
version: None,
crate_config: CrateConfig {
name: "demo".to_string(),
sources: vec![],
version_from: "Cargo.toml".to_string(),
core_import: None,
workspace_root: None,
skip_core_import: false,
features: vec![],
path_mappings: std::collections::HashMap::new(),
auto_path_mappings: Default::default(),
extra_dependencies: Default::default(),
source_crates: vec![],
error_type: None,
error_constructor: None,
},
languages: vec![],
exclude: Default::default(),
include: Default::default(),
output: Default::default(),
python: None,
node: None,
ruby: None,
php: None,
elixir: None,
wasm: None,
ffi: None,
gleam: None,
go: None,
java: None,
kotlin: None,
dart: None,
swift: None,
csharp: None,
r: None,
zig: None,
scaffold: None,
readme: None,
lint: None,
update: None,
test: None,
setup: None,
clean: None,
build_commands: None,
publish: None,
custom_files: None,
adapters: vec![],
custom_modules: alef_core::config::CustomModulesConfig::default(),
custom_registrations: alef_core::config::CustomRegistrationsConfig::default(),
opaque_types: std::collections::HashMap::new(),
generate: alef_core::config::GenerateConfig::default(),
generate_overrides: std::collections::HashMap::new(),
dto: Default::default(),
sync: None,
e2e: None,
trait_bridges: vec![],
tools: alef_core::config::ToolsConfig::default(),
format: alef_core::config::FormatConfig::default(),
format_overrides: std::collections::HashMap::new(),
}
}
#[test]
fn snapshot_basic_struct_function_enum_error() {
let api = make_basic_api();
let config = make_basic_config();
let files = ZigBackend.generate_bindings(&api, &config).unwrap();
for file in &files {
insta::assert_snapshot!(
format!("snapshot_basic__{}", file.path.display().to_string().replace('/', "__")),
&file.content
);
}
}