use unitype_core::check::CheckedWorkspace;
use unitype_core::ir::{Type, TypeDef};
use unitype_core::lang::LanguageProfile;
use unitype_core::value::Value;
pub struct Constructors {
pub record: fn(&str, &[(String, String)]) -> String,
pub variant: fn(&str, &str, Option<&str>) -> String,
pub plain: fn(&Value) -> String,
pub tuple: fn(&[String]) -> String,
pub typed: Option<fn(&CheckedWorkspace, &Value, &Type) -> String>,
}
pub(crate) fn parenthesised(parts: &[String]) -> String {
match parts {
[only] => format!("({only},)"),
_ => format!("({})", parts.join(", ")),
}
}
pub fn typed(
ws: &CheckedWorkspace,
profile: &LanguageProfile,
how: &Constructors,
value: &Value,
ty: &Type,
) -> String {
if let Some(own) = how.typed {
return own(ws, value, ty);
}
let ty = ws.expand_alias(ty);
match (&ty, value) {
(_, Value::Null) => (how.plain)(value),
(Type::Optional { of }, present) => typed(ws, profile, how, present, of),
(Type::Bounded { of, .. }, _) => typed(ws, profile, how, value, of),
(Type::List { of }, Value::List(items)) => {
let parts: Vec<String> = items
.iter()
.map(|i| typed(ws, profile, how, i, of))
.collect();
wrap_list(how, &parts)
}
(Type::Tuple { items: types }, Value::Tuple(items)) if types.len() == items.len() => {
let parts: Vec<String> = types
.iter()
.zip(items)
.map(|(t, i)| typed(ws, profile, how, i, t))
.collect();
(how.tuple)(&parts)
}
(Type::Union { members }, _) => {
for member in members {
if ws.validate(value, member).is_ok() {
return typed(ws, profile, how, value, member);
}
}
(how.plain)(value)
}
(Type::Record { fields }, Value::Record(entries)) => {
let parts: Vec<String> = entries
.iter()
.map(|(name, entry)| {
let declared = fields.iter().find(|f| f.name.as_str() == name);
let written = match declared {
Some(field) => typed(ws, profile, how, entry, &field.ty),
None => (how.plain)(entry),
};
format!("{}: {written}", (how.plain)(&Value::Str(name.clone())))
})
.collect();
format!("{{{}}}", parts.join(", "))
}
(Type::Ref(r), _) => {
let Some(def) = ws.ref_target_of(r).and_then(|q| ws.def(&q).cloned()) else {
return (how.plain)(value);
};
match (&def, value) {
(TypeDef::Struct(_) | TypeDef::Interface(_), Value::Record(entries)) => {
let declared = ws.fields_of(r).unwrap_or_default();
let parts: Vec<(String, String)> = declared
.iter()
.filter_map(|field| {
let entry = entries.get(field.name.as_str())?;
if matches!(entry, Value::Null) {
return None;
}
Some((
profile.name(&field.name),
typed(ws, profile, how, entry, &field.ty),
))
})
.collect();
(how.record)(&profile.name(&r.name), &parts)
}
(
TypeDef::Enum(d),
Value::Variant {
tag,
value: payload,
},
) => {
let carried = d
.variants
.iter()
.find(|v| v.name.as_str() == tag)
.filter(|v| !matches!(v.payload, Type::Unit))
.map(|v| typed(ws, profile, how, payload, &v.payload));
(how.variant)(&profile.name(&r.name), tag, carried.as_deref())
}
_ => (how.plain)(value),
}
}
_ => (how.plain)(value),
}
}
fn wrap_list(how: &Constructors, parts: &[String]) -> String {
let shape = (how.plain)(&Value::List(parts.iter().map(|_| Value::Null).collect()));
let mut out = String::with_capacity(shape.len() + parts.iter().map(String::len).sum::<usize>());
let mut parts = parts.iter();
let mut rest = shape.as_str();
let nothing = (how.plain)(&Value::Null);
while let Some(at) = rest.find(¬hing) {
out.push_str(&rest[..at]);
match parts.next() {
Some(part) => out.push_str(part),
None => out.push_str(¬hing),
}
rest = &rest[at + nothing.len()..];
}
out.push_str(rest);
out
}
#[cfg(test)]
mod tests {
use super::*;
use unitype_core::Workspace;
use unitype_core::check::check_workspace;
use unitype_core::ir::TypeRef;
use unitype_core::lang::profile;
use unitype_core::{Ident, QualifiedName};
const WS: &str = r#"{"universes":[{"name":"demo","types":[
{"k":"struct","name":"Shape","fields":[{"name":"id","ty":"str"}]},
{"k":"struct","name":"Circle","extends":"Shape","fields":[
{"name":"kind","ty":{"k":"literal","value":{"str":"circle"}}},
{"name":"radius","ty":"float"},
{"name":"note","ty":"str?"}]},
{"k":"enum","name":"Colour","variants":[
{"name":"Red"},{"name":"Custom","payload":"str"}]},
{"k":"struct","name":"Bag","fields":[{"name":"items","ty":"[Circle]"}]},
{"k":"struct","name":"Pairs","fields":[
{"name":"named","ty":{"k":"tuple","items":["i64","str"]}},
{"name":"point","ty":{"k":"tuple","items":["i64","i64"]}}]}]}]}"#;
fn ws() -> CheckedWorkspace {
check_workspace(Workspace::from_json(WS).expect("reads")).expect("checks")
}
fn named(name: &str) -> Type {
Type::Ref(TypeRef::qualified(
Ident::new("demo").unwrap(),
Ident::new(name).unwrap(),
))
}
fn write(lang: &str, json: &str, ty: &Type) -> String {
let ws = ws();
let value = ws.read_json(json, ty).expect("a real value");
let profile = profile(lang).expect("ships");
let how = crate::constructors(lang).expect("has constructors");
typed(&ws, &profile, &how, &value, ty)
}
#[test]
fn a_struct_becomes_the_way_that_language_builds_one() {
let json = r#"{"id":"c-1","kind":"circle","radius":2.5}"#;
assert_eq!(
write("python", json, &named("Circle")),
r#"Circle(id="c-1", kind="circle", radius=2.5)"#
);
assert_eq!(
write("rust", json, &named("Circle")),
r#"Circle { id: "c-1".to_string(), kind: "circle".to_string(), radius: 2.5 }"#
);
assert_eq!(
write("typescript", json, &named("Circle")),
r#"{ id: "c-1", kind: "circle", radius: 2.5 }"#
);
}
#[test]
fn fields_come_out_in_the_order_they_were_declared() {
let scrambled = r#"{"radius":2.5,"kind":"circle","id":"c-1"}"#;
assert_eq!(
write("python", scrambled, &named("Circle")),
r#"Circle(id="c-1", kind="circle", radius=2.5)"#
);
}
#[test]
fn a_field_that_is_absent_is_left_out_of_the_literal_too() {
let with_null = r#"{"id":"c-1","kind":"circle","radius":2.5,"note":null}"#;
let written = write("python", with_null, &named("Circle"));
assert!(!written.contains("note"), "{written}");
}
#[test]
fn a_nested_struct_is_built_rather_than_flattened_into_a_map() {
let json = r#"{"items":[{"id":"c-1","kind":"circle","radius":1.0}]}"#;
let written = write("rust", json, &named("Bag"));
assert!(
written.contains("Circle {"),
"the element is a Circle, so it should be built as one: {written}"
);
assert!(written.starts_with("Bag {"), "{written}");
}
#[test]
fn a_variant_is_named_the_way_that_language_names_one() {
assert_eq!(write("rust", r#""Red""#, &named("Colour")), "Colour::Red");
assert_eq!(
write(
"rust",
r#"{"$tag":"Custom","value":"teal"}"#,
&named("Colour")
),
r#"Colour::Custom("teal".to_string())"#
);
assert_eq!(
write("python", r#""Red""#, &named("Colour")),
r#"{"$tag": "Red"}"#
);
}
#[test]
fn a_list_gets_that_language_brackets_with_the_parts_filled_in() {
let json = r#"{"items":[{"id":"a","kind":"circle","radius":1.0},
{"id":"b","kind":"circle","radius":2.0}]}"#;
let written = write("python", json, &named("Bag"));
assert!(written.contains("items=[Circle("), "{written}");
assert_eq!(written.matches("Circle(").count(), 2, "{written}");
}
#[test]
fn a_tuple_with_a_string_in_it_keeps_the_quotes_around_the_string() {
let json = r#"{"named":[1,"a"],"point":[1,2]}"#;
assert_eq!(
write("python", json, &named("Pairs")),
r#"Pairs(named=(1, "a"), point=(1, 2))"#
);
assert_eq!(
write("typescript", json, &named("Pairs")),
r#"{ named: [1, "a"], point: [1, 2] }"#
);
assert_eq!(
write("rust", json, &named("Pairs")),
r#"Pairs { named: (1, "a".to_string()), point: (1, 2) }"#
);
}
#[test]
fn a_tuple_of_numbers_is_not_rewritten_as_strings() {
let json = r#"{"named":[7,"x"],"point":[3,4]}"#;
let rust = write("rust", json, &named("Pairs"));
assert!(rust.contains("point: (3, 4)"), "{rust}");
assert!(!rust.contains("3.to_string()"), "{rust}");
}
#[test]
fn a_one_element_tuple_keeps_its_trailing_comma_where_it_needs_one() {
let ty = Type::Tuple {
items: vec![Type::String],
};
let value = Value::Tuple(vec![Value::Str("only".into())]);
let ws = ws();
let write = |lang: &str| {
let profile = profile(lang).expect("ships");
let how = crate::constructors(lang).expect("has constructors");
typed(&ws, &profile, &how, &value, &ty)
};
assert_eq!(write("python"), r#"("only",)"#);
assert_eq!(write("rust"), r#"("only".to_string(),)"#);
assert_eq!(write("typescript"), r#"["only"]"#);
}
#[test]
fn without_a_type_to_go_on_a_record_stays_a_map() {
let ws = ws();
let value = ws
.read_json(r#"{"whatever":1}"#, &Type::Any)
.expect("reads");
let profile = profile("python").expect("ships");
let how = crate::constructors("python").expect("has constructors");
assert_eq!(
typed(&ws, &profile, &how, &value, &Type::Any),
r#"{"whatever": 1}"#
);
}
#[test]
fn every_shipped_language_has_constructors() {
for lang in unitype_core::LANGUAGES {
assert!(crate::constructors(lang).is_some(), "{lang}");
}
let _ = QualifiedName::new(Ident::new("a").unwrap(), Ident::new("b").unwrap());
}
}