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 to_base_path(self) -> String {
16        self.to_string()
17    }
18
19    /// Check if this environment supports file I/O
20    pub fn supports_file_io(self) -> bool {
21        match self {
22            Environment::StandAlone | Environment::Project | Environment::Repl => true,
23            Environment::Wasm => false,
24        }
25    }
26
27    /// Check if external files should be inlined
28    pub fn should_inline_files(self) -> bool {
29        match self {
30            Environment::Wasm => true,
31            _ => false,
32        }
33    }
34}
35
36impl fmt::Display for Environment {
37    fn fmt(self: &Self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        let res = match self {
39            Environment::Project => "R/",
40            Environment::StandAlone | Environment::Repl | Environment::Wasm => "",
41        };
42        write!(f, "{}", res)
43    }
44}
45
46#[derive(Debug, Clone, PartialEq, Copy, Serialize, Deserialize)]
47pub enum TargetLanguage {
48    R,
49    JS,
50}
51
52impl Default for TargetLanguage {
53    fn default() -> Self {
54        TargetLanguage::R
55    }
56}
57
58#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
59pub enum FileType {
60    Main,
61    Module,
62}
63
64#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
65pub struct Config {
66    pub environment: Environment,
67    pub file_type: FileType,
68    pub target_language: TargetLanguage,
69}
70
71//main
72impl Config {
73    pub fn set_environment(&self, e: Environment) -> Config {
74        Config {
75            environment: e,
76            ..self.clone()
77        }
78    }
79
80    pub fn set_as_module(self) -> Self {
81        Self {
82            file_type: FileType::Module,
83            ..self
84        }
85    }
86
87    pub fn set_target_language(self, language: TargetLanguage) -> Self {
88        Self {
89            target_language: language,
90            ..self
91        }
92    }
93
94    pub fn get_target_language(&self) -> TargetLanguage {
95        self.target_language
96    }
97
98    pub fn to_context(self) -> Context {
99        Context::default().set_config(self)
100    }
101}
102
103impl Default for Config {
104    fn default() -> Config {
105        Config {
106            target_language: TargetLanguage::R,
107            environment: Environment::StandAlone,
108            file_type: FileType::Main,
109        }
110    }
111}