use crate::program::Definition;
use crate::types::{SimpleKind, Type, TypeKind, is_rune};
pub const UNIT_DISPLAY: &str = "()";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisplayForm {
Unit,
Function,
Char,
String,
Bool,
Integer,
Float,
Value,
}
impl DisplayForm {
pub fn of<'d>(ty: &Type, lookup: impl Fn(&str) -> Option<&'d Definition>) -> Self {
if is_raw_function_type(ty) {
return DisplayForm::Function;
}
if ty.is_unit() {
return DisplayForm::Unit;
}
if is_rune(ty, lookup) {
return DisplayForm::Char;
}
match ty.as_simple() {
Some(SimpleKind::String) => DisplayForm::String,
Some(SimpleKind::Bool) => DisplayForm::Bool,
Some(k) if k.is_integer() => DisplayForm::Integer,
Some(k) if k.is_float() => DisplayForm::Float,
_ => DisplayForm::Value,
}
}
pub fn interpolation_verb(self) -> &'static str {
match self {
DisplayForm::Unit => UNIT_DISPLAY,
DisplayForm::Char => "%c",
DisplayForm::String => "%s",
DisplayForm::Bool => "%t",
DisplayForm::Integer => "%d",
DisplayForm::Float => "%g",
DisplayForm::Function => "%p",
DisplayForm::Value => "%v",
}
}
}
fn is_raw_function_type(ty: &Type) -> bool {
match ty.kind() {
TypeKind::Function(_) => true,
TypeKind::Simple(_)
| TypeKind::Compound { .. }
| TypeKind::Nominal { .. }
| TypeKind::ImportNamespace(_)
| TypeKind::Var { .. }
| TypeKind::Uninferred
| TypeKind::Parameter(_)
| TypeKind::Never
| TypeKind::Tuple(_)
| TypeKind::Array { .. }
| TypeKind::Error(_)
| TypeKind::ReceiverPlaceholder => false,
}
}