use hir_lang::Prim;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Discipline {
Dynamic,
Gradual,
StaticExplicit,
StaticInferred,
Hm,
}
impl Discipline {
#[must_use]
pub const fn is_implemented(self) -> bool {
matches!(self, Self::Dynamic | Self::StaticExplicit)
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Dynamic => "dynamic",
Self::Gradual => "gradual",
Self::StaticExplicit => "static-explicit",
Self::StaticInferred => "static-inferred",
Self::Hm => "hm",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RecordTyping {
Nominal,
Structural,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Polymorphism {
Traits,
Interfaces,
Typeclasses,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Profile {
pub discipline: Discipline,
pub subtyping: bool,
pub records: RecordTyping,
pub numeric_widening: bool,
pub null_safety: bool,
pub exhaustive_match: bool,
pub polymorphism: Polymorphism,
pub default_int: Prim,
pub default_float: Prim,
pub string_add: bool,
pub certain_errors: bool,
}
impl Default for Profile {
fn default() -> Self {
Self::static_explicit()
}
}
impl Profile {
#[must_use]
pub const fn dynamic() -> Self {
Self {
discipline: Discipline::Dynamic,
subtyping: true,
records: RecordTyping::Structural,
numeric_widening: true,
null_safety: false,
exhaustive_match: false,
polymorphism: Polymorphism::Interfaces,
default_int: Prim::I64,
default_float: Prim::F64,
string_add: true,
certain_errors: true,
}
}
#[must_use]
pub const fn gradual() -> Self {
Self {
discipline: Discipline::Gradual,
null_safety: true,
..Self::dynamic()
}
}
#[must_use]
pub const fn static_explicit() -> Self {
Self {
discipline: Discipline::StaticExplicit,
subtyping: false,
records: RecordTyping::Nominal,
numeric_widening: false,
null_safety: true,
exhaustive_match: true,
polymorphism: Polymorphism::Interfaces,
default_int: Prim::I64,
default_float: Prim::F64,
string_add: false,
certain_errors: true,
}
}
#[must_use]
pub const fn static_inferred() -> Self {
Self {
discipline: Discipline::StaticInferred,
polymorphism: Polymorphism::Traits,
..Self::static_explicit()
}
}
#[must_use]
pub const fn hm() -> Self {
Self {
discipline: Discipline::Hm,
polymorphism: Polymorphism::Typeclasses,
..Self::static_explicit()
}
}
#[must_use]
pub const fn with_subtyping(mut self, on: bool) -> Self {
self.subtyping = on;
self
}
#[must_use]
pub const fn with_records(mut self, records: RecordTyping) -> Self {
self.records = records;
self
}
#[must_use]
pub const fn with_numeric_widening(mut self, on: bool) -> Self {
self.numeric_widening = on;
self
}
#[must_use]
pub const fn with_null_safety(mut self, on: bool) -> Self {
self.null_safety = on;
self
}
#[must_use]
pub const fn with_exhaustive_match(mut self, on: bool) -> Self {
self.exhaustive_match = on;
self
}
#[must_use]
pub const fn with_polymorphism(mut self, polymorphism: Polymorphism) -> Self {
self.polymorphism = polymorphism;
self
}
#[must_use]
pub const fn with_literal_defaults(mut self, int: Prim, float: Prim) -> Self {
self.default_int = int;
self.default_float = float;
self
}
#[must_use]
pub const fn with_string_add(mut self, on: bool) -> Self {
self.string_add = on;
self
}
#[must_use]
pub const fn with_certain_errors(mut self, on: bool) -> Self {
self.certain_errors = on;
self
}
pub(crate) const fn is_static(&self) -> bool {
!matches!(self.discipline, Discipline::Dynamic)
}
}