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}
71
72//main
73impl Config {
74    pub fn set_environment(&self, e: Environment) -> Config {
75        Config {
76            environment: e,
77            ..self.clone()
78        }
79    }
80
81    pub fn set_as_module(self) -> Self {
82        Self {
83            file_type: FileType::Module,
84            ..self
85        }
86    }
87
88    pub fn set_target_language(self, language: TargetLanguage) -> Self {
89        Self {
90            target_language: language,
91            ..self
92        }
93    }
94
95    pub fn set_in_module_body(self, val: bool) -> Self {
96        Self {
97            in_module_body: val,
98            ..self
99        }
100    }
101
102    pub fn get_target_language(&self) -> TargetLanguage {
103        self.target_language
104    }
105
106    pub fn to_context(self) -> Context {
107        Context::default().set_config(self)
108    }
109}
110
111impl Default for Config {
112    fn default() -> Config {
113        Config {
114            target_language: TargetLanguage::R,
115            environment: Environment::StandAlone,
116            file_type: FileType::Main,
117            in_module_body: false,
118        }
119    }
120}