lisette_syntax/
display_form.rs1use crate::program::Definition;
2use crate::types::{SimpleKind, Type, TypeKind, is_rune};
3
4pub const UNIT_DISPLAY: &str = "()";
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum DisplayForm {
8 Unit,
9 Function,
10 Char,
11 String,
12 Bool,
13 Integer,
14 Float,
15 Value,
16}
17
18impl DisplayForm {
19 pub fn of<'d>(ty: &Type, lookup: impl Fn(&str) -> Option<&'d Definition>) -> Self {
20 if is_raw_function_type(ty) {
21 return DisplayForm::Function;
22 }
23 if ty.is_unit() {
24 return DisplayForm::Unit;
25 }
26 if is_rune(ty, lookup) {
27 return DisplayForm::Char;
28 }
29 match ty.as_simple() {
30 Some(SimpleKind::String) => DisplayForm::String,
31 Some(SimpleKind::Bool) => DisplayForm::Bool,
32 Some(k) if k.is_integer() => DisplayForm::Integer,
33 Some(k) if k.is_float() => DisplayForm::Float,
34 _ => DisplayForm::Value,
35 }
36 }
37
38 pub fn interpolation_verb(self) -> &'static str {
39 match self {
40 DisplayForm::Unit => UNIT_DISPLAY,
41 DisplayForm::Char => "%c",
42 DisplayForm::String => "%s",
43 DisplayForm::Bool => "%t",
44 DisplayForm::Integer => "%d",
45 DisplayForm::Float => "%g",
46 DisplayForm::Function => "%p",
47 DisplayForm::Value => "%v",
48 }
49 }
50}
51
52fn is_raw_function_type(ty: &Type) -> bool {
53 match ty.kind() {
54 TypeKind::Function(_) => true,
55 TypeKind::Simple(_)
56 | TypeKind::Compound { .. }
57 | TypeKind::Nominal { .. }
58 | TypeKind::ImportNamespace(_)
59 | TypeKind::Var { .. }
60 | TypeKind::Uninferred
61 | TypeKind::Parameter(_)
62 | TypeKind::Never
63 | TypeKind::Tuple(_)
64 | TypeKind::Array { .. }
65 | TypeKind::Error(_)
66 | TypeKind::ReceiverPlaceholder => false,
67 }
68}