Skip to main content

typr_core/components/context/
config.rs

1use crate::components::context::Context;
2use serde::{Deserialize, Serialize};
3use std::fmt;
4
5#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
6pub enum Environment {
7    StandAlone,
8    Project,
9    Repl,
10    /// WebAssembly environment - all code is inlined, no file I/O
11    Wasm,
12}
13
14impl Environment {
15    pub fn is_project(&self) -> bool {
16        matches!(self, Environment::Project)
17    }
18
19    pub fn to_base_path(self) -> String {
20        self.to_string()
21    }
22
23    /// Check if this environment supports file I/O
24    pub fn supports_file_io(self) -> bool {
25        match self {
26            Environment::StandAlone | Environment::Project | Environment::Repl => true,
27            Environment::Wasm => false,
28        }
29    }
30
31    /// Check if external files should be inlined
32    pub fn should_inline_files(self) -> bool {
33        matches!(self, Environment::Wasm)
34    }
35}
36
37impl fmt::Display for Environment {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        let res = match self {
40            Environment::Project => "R/",
41            Environment::StandAlone | Environment::Repl | Environment::Wasm => "",
42        };
43        write!(f, "{}", res)
44    }
45}
46
47#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize, Default)]
48pub enum TargetLanguage {
49    R,
50    #[default]
51    JS,
52}
53
54#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize, Default)]
55pub enum FileType {
56    Main,
57    #[default]
58    Module,
59}
60
61#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
62pub struct Config {
63    pub environment: Environment,
64    pub file_type: FileType,
65    pub target_language: TargetLanguage,
66    /// When true, opaque alias declarations are registered as transparent.
67    /// Set to true when type-checking a module body so internal functions
68    /// can see through opaque types they declare.
69    pub in_module_body: bool,
70    /// When true, this is a test build (`--test`): `@testable` private members
71    /// are additionally exposed as `M$.test_<name>` (see RFC-TR-031).
72    #[serde(default)]
73    pub test_mode: bool,
74    /// When true, the expression currently being type-checked is nested
75    /// inside a loop body (`Lang::Loop`/`WhileLoop`/`ForLoop`) — lets
76    /// `break`/`next` be rejected outside of one (audit_type_checking.md C2).
77    #[serde(default)]
78    pub in_loop: bool,
79    /// When true (`typr build --checked` / `typr run --checked`), the
80    /// transpiler wraps typed boundaries (`let` annotations, function
81    /// params/return, constructor calls) in `typr_assert_type(...)` runtime
82    /// checks (soundness_transpilation.md Phase A). Test-only oracle, never
83    /// a production mode.
84    #[serde(default)]
85    pub checked_mode: bool,
86}
87
88//main
89impl Config {
90    pub fn set_environment(&self, e: Environment) -> Config {
91        Config {
92            environment: e,
93            ..self.clone()
94        }
95    }
96
97    pub fn set_as_module(self) -> Self {
98        Self {
99            file_type: FileType::Module,
100            ..self
101        }
102    }
103
104    pub fn set_target_language(self, language: TargetLanguage) -> Self {
105        Self {
106            target_language: language,
107            ..self
108        }
109    }
110
111    pub fn set_in_module_body(self, val: bool) -> Self {
112        Self {
113            in_module_body: val,
114            ..self
115        }
116    }
117
118    pub fn set_test_mode(self, val: bool) -> Self {
119        Self { test_mode: val, ..self }
120    }
121
122    pub fn set_in_loop(self, val: bool) -> Self {
123        Self { in_loop: val, ..self }
124    }
125
126    pub fn set_checked_mode(self, val: bool) -> Self {
127        Self {
128            checked_mode: val,
129            ..self
130        }
131    }
132
133    pub fn get_target_language(&self) -> TargetLanguage {
134        self.target_language
135    }
136
137    pub fn to_context(self) -> Context {
138        Context::default().set_config(self)
139    }
140}
141
142impl Default for Config {
143    fn default() -> Config {
144        Config {
145            target_language: TargetLanguage::R,
146            environment: Environment::StandAlone,
147            file_type: FileType::Main,
148            in_module_body: false,
149            test_mode: false,
150            in_loop: false,
151            checked_mode: false,
152        }
153    }
154}