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