Skip to main content

typr_core/utils/
builder.rs

1#![allow(
2    dead_code,
3    unused_variables,
4    unused_imports,
5    unreachable_code,
6    unused_assignments
7)]
8
9use crate::components::error_message::help_data::HelpData;
10use crate::components::error_message::type_error::TypeError;
11use crate::components::language::operators::Op;
12use crate::components::language::var::Var;
13use crate::components::language::Lang;
14use crate::components::r#type::argument_type::ArgumentType;
15use crate::components::r#type::tchar::Tchar;
16use crate::components::r#type::tint::Tint;
17use crate::components::r#type::type_operator::TypeOperator;
18use crate::components::r#type::vector_type::VecType;
19use crate::components::r#type::Type;
20use crate::TypRError;
21use std::collections::HashSet;
22
23pub fn generic_type() -> Type {
24    Type::Generic("T".to_string(), HelpData::default())
25}
26
27pub fn self_generic_type() -> Type {
28    Type::Generic("Self".to_string(), HelpData::default())
29}
30
31pub fn empty_type() -> Type {
32    Type::Empty(HelpData::default())
33}
34
35pub fn empty_lang() -> Lang {
36    Lang::Empty(HelpData::default())
37}
38
39pub fn any_type() -> Type {
40    Type::Any(HelpData::default())
41}
42
43pub fn integer_type(i: i32) -> Type {
44    Type::Integer(Tint::Val(i), HelpData::default())
45}
46
47pub fn integer_type_default() -> Type {
48    Type::Integer(Tint::Unknown, HelpData::default())
49}
50
51pub fn character_type(s: &str) -> Type {
52    Type::Char(Tchar::Val(s.to_string()), HelpData::default())
53}
54
55pub fn character_type_default() -> Type {
56    Type::Char(Tchar::Unknown, HelpData::default())
57}
58
59pub fn number_type() -> Type {
60    Type::Number(crate::components::r#type::tnumber::Tnum::Unknown, HelpData::default())
61}
62
63pub fn boolean_type() -> Type {
64    Type::Boolean(crate::components::r#type::tbool::Tbool::Unknown, HelpData::default())
65}
66
67pub fn record_type(params: &[(String, Type)]) -> Type {
68    let args = params
69        .iter()
70        .map(|param| ArgumentType::from(param.to_owned()))
71        .collect::<HashSet<_>>();
72    Type::Record(args, HelpData::default())
73}
74
75pub fn params_type() -> Type {
76    Type::Params(vec![], HelpData::default())
77}
78
79pub fn generic_function(s: &str) -> Lang {
80    let body = format!("{} <- function(x, ...) {{ UseMethod('{}') }}", s, s);
81    Lang::GenFunc {
82        name: body,
83        help_data: HelpData::default(),
84    }
85}
86
87pub fn tuple_type(types: &[Type]) -> Type {
88    Type::Tuple(types.to_vec(), HelpData::default())
89}
90
91pub fn array_type(i: Type, t: Type) -> Type {
92    Type::Vec(VecType::S3, Box::new(i), Box::new(t), HelpData::default())
93}
94
95pub fn array_type2(i: i32, t: Type) -> Type {
96    let i2 = integer_type(i);
97    Type::Vec(VecType::S3, Box::new(i2), Box::new(t), HelpData::default())
98}
99
100pub fn dataframe_type(i: Type, columns: &[(String, Type)]) -> Type {
101    let fields = columns
102        .iter()
103        .map(|param| ArgumentType::from(param.to_owned()))
104        .collect::<HashSet<_>>();
105    Type::Vec(
106        VecType::DataFrame,
107        Box::new(i),
108        Box::new(Type::Record(fields, HelpData::default())),
109        HelpData::default(),
110    )
111}
112
113pub fn opaque_type(name: &str) -> Type {
114    Type::Opaque(name.to_string(), HelpData::default())
115}
116
117pub fn function_type(args: &[Type], return_type: Type) -> Type {
118    let arg_types: Vec<ArgumentType> = args
119        .iter()
120        .enumerate()
121        .map(|(i, typ)| {
122            let arg_name = crate::components::r#type::generate_arg(i);
123            ArgumentType::new(&arg_name, typ)
124        })
125        .collect();
126    Type::Function(arg_types, Box::new(return_type), HelpData::default())
127}
128
129pub fn interface_type(signatures: &[(&str, Type)]) -> Type {
130    let args = signatures
131        .iter()
132        .cloned()
133        .map(|(name, typ)| ArgumentType::from((name, typ)))
134        .collect::<HashSet<_>>();
135    Type::Interface(args, HelpData::default())
136}
137
138pub fn interface_type2(signatures: &[(String, Type)]) -> Type {
139    let args = signatures
140        .iter()
141        .cloned()
142        .map(|(name, typ)| ArgumentType::from((name, typ)))
143        .collect::<HashSet<_>>();
144    Type::Interface(args, HelpData::default())
145}
146
147pub fn intersection_type(types: &[Type]) -> Type {
148    let type_set = types.iter().cloned().collect::<HashSet<_>>();
149    Type::Intersection(type_set, HelpData::default())
150}
151
152pub fn union_type(types: &[Type]) -> Type {
153    types
154        .iter()
155        .cloned()
156        .reduce(|acc, t| {
157            Type::Operator(
158                TypeOperator::Union,
159                Box::new(acc),
160                Box::new(t),
161                HelpData::default(),
162            )
163        })
164        .unwrap_or(Type::Empty(HelpData::default()))
165}
166
167pub fn unknown_function_type() -> Type {
168    Type::UnknownFunction(HelpData::default())
169}
170
171pub fn operation(operator: Op, left: Lang, right: Lang) -> Lang {
172    Lang::Operator {
173        operator,
174        rhs: Box::new(left),
175        lhs: Box::new(right),
176        help_data: HelpData::default(),
177    }
178}
179
180pub fn let_var(name: &str, typ: Type) -> (Var, Type) {
181    (Var::from(name).set_type(typ.clone()), typ)
182}
183
184pub fn null_type() -> Type {
185    Type::Null(HelpData::default())
186}
187
188pub fn na_type() -> Type {
189    Type::NA(HelpData::default())
190}
191
192pub fn null_lang() -> Lang {
193    Lang::Null(HelpData::default())
194}
195
196pub fn unmatching_return_type(typ1: &Type, typ2: &Type) -> TypRError {
197    TypRError::Type(TypeError::UnmatchingReturnType(typ1.clone(), typ2.clone()))
198}