typr_core/components/context/
config.rs1use 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 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 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 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 pub in_module_body: bool,
70 #[serde(default)]
73 pub test_mode: bool,
74}
75
76impl Config {
78 pub fn set_environment(&self, e: Environment) -> Config {
79 Config {
80 environment: e,
81 ..self.clone()
82 }
83 }
84
85 pub fn set_as_module(self) -> Self {
86 Self {
87 file_type: FileType::Module,
88 ..self
89 }
90 }
91
92 pub fn set_target_language(self, language: TargetLanguage) -> Self {
93 Self {
94 target_language: language,
95 ..self
96 }
97 }
98
99 pub fn set_in_module_body(self, val: bool) -> Self {
100 Self {
101 in_module_body: val,
102 ..self
103 }
104 }
105
106 pub fn set_test_mode(self, val: bool) -> Self {
107 Self {
108 test_mode: val,
109 ..self
110 }
111 }
112
113 pub fn get_target_language(&self) -> TargetLanguage {
114 self.target_language
115 }
116
117 pub fn to_context(self) -> Context {
118 Context::default().set_config(self)
119 }
120}
121
122impl Default for Config {
123 fn default() -> Config {
124 Config {
125 target_language: TargetLanguage::R,
126 environment: Environment::StandAlone,
127 file_type: FileType::Main,
128 in_module_body: false,
129 test_mode: false,
130 }
131 }
132}