harn_parser/builtin_signatures/
types.rs1use crate::ast::{ShapeField, TypeExpr};
11
12pub use harn_builtin_meta::{
13 BuiltinMetadata, BuiltinSignature, Param, ShapeFieldDescriptor, Ty, TY_ANY, TY_BOOL, TY_BYTES,
14 TY_BYTES_OR_NIL, TY_CLOSURE, TY_DECIMAL, TY_DICT, TY_DICT_OR_NIL, TY_DURATION, TY_FLOAT,
15 TY_INT, TY_INT_OR_NIL, TY_LIST, TY_NEVER, TY_NIL, TY_NUMBER, TY_STRING, TY_STRING_OR_NIL,
16};
17
18pub fn ty_to_type_expr(ty: &Ty) -> TypeExpr {
22 match ty {
23 Ty::Named(name) => TypeExpr::Named((*name).into()),
24 Ty::Generic(name) => TypeExpr::Named((*name).into()),
25 Ty::Any => TypeExpr::Named("any".into()),
26 Ty::Optional(inner) => {
27 TypeExpr::Union(vec![ty_to_type_expr(inner), TypeExpr::Named("nil".into())])
28 }
29 Ty::Apply("list", [inner]) => TypeExpr::List(Box::new(ty_to_type_expr(inner))),
35 Ty::Apply("dict", [key, value]) => TypeExpr::DictType(
36 Box::new(ty_to_type_expr(key)),
37 Box::new(ty_to_type_expr(value)),
38 ),
39 Ty::Apply("iter", [inner]) => TypeExpr::Iter(Box::new(ty_to_type_expr(inner))),
40 Ty::Apply("generator" | "Generator", [inner]) => {
41 TypeExpr::Generator(Box::new(ty_to_type_expr(inner)))
42 }
43 Ty::Apply("stream" | "Stream", [inner]) => {
44 TypeExpr::Stream(Box::new(ty_to_type_expr(inner)))
45 }
46 Ty::Apply("owned", [inner]) => TypeExpr::Owned(Box::new(ty_to_type_expr(inner))),
47 Ty::Apply(name, args) => TypeExpr::Applied {
48 name: (*name).into(),
49 args: args.iter().map(ty_to_type_expr).collect(),
50 },
51 Ty::Union(members) => TypeExpr::Union(members.iter().map(ty_to_type_expr).collect()),
52 Ty::Fn(params, return_type) => TypeExpr::FnType {
53 params: params.iter().map(ty_to_type_expr).collect(),
54 return_type: Box::new(ty_to_type_expr(return_type)),
55 },
56 Ty::Shape(fields) => TypeExpr::Shape(
57 fields
58 .iter()
59 .map(|f| ShapeField::synthetic(f.name, ty_to_type_expr(&f.ty), f.optional))
60 .collect(),
61 ),
62 Ty::SchemaOf(name) => TypeExpr::Applied {
63 name: "Schema".into(),
64 args: vec![TypeExpr::Named((*name).into())],
65 },
66 Ty::Never => TypeExpr::Never,
67 Ty::LitInt(v) => TypeExpr::LitInt(*v),
68 Ty::LitString(s) => TypeExpr::LitString((*s).into()),
69 }
70}
71
72pub trait TyExt {
76 fn to_type_expr(&self) -> TypeExpr;
78}
79
80impl TyExt for Ty {
81 fn to_type_expr(&self) -> TypeExpr {
82 ty_to_type_expr(self)
83 }
84}
85
86pub trait BuiltinSignatureExt {
87 fn param_type_exprs(&self) -> Vec<TypeExpr>;
90 fn return_type_expr(&self) -> TypeExpr;
92}
93
94impl BuiltinSignatureExt for BuiltinSignature {
95 fn param_type_exprs(&self) -> Vec<TypeExpr> {
96 self.params.iter().map(|p| ty_to_type_expr(&p.ty)).collect()
97 }
98
99 fn return_type_expr(&self) -> TypeExpr {
100 ty_to_type_expr(&self.returns)
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 const STRING: Ty = Ty::Named("string");
109 const LIST_ARGS: &[Ty] = &[STRING];
110
111 #[test]
112 fn builtin_container_metadata_uses_language_ast_variants() {
113 assert_eq!(
114 ty_to_type_expr(&Ty::Apply("list", LIST_ARGS)),
115 TypeExpr::List(Box::new(TypeExpr::Named("string".into())))
116 );
117 }
118}