Skip to main content

weirdnet_code_spaces/
lang.rs

1//! What a code space is written in.
2use serde::{Deserialize, Serialize};
3
4/// What a code space is written in. Serialized in snake case (`"math"`, `"plugin"`, …).
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum Lang {
8    /// Formulas over columns (`sma20 = sma(close, 20)`), compiled through generated Rust.
9    Math,
10    /// Polars expressions (`ret = col("close").pct_change(lit(1))`), compiled through generated Rust.
11    Polars,
12    /// Rust against the profile's plugin SDK.
13    Rust,
14    /// Typed Python functions, translated to Rust in process (ruff parser, python-ast codegen).
15    Python,
16    /// A library built elsewhere (the source is its path).
17    Plugin,
18}
19
20impl Lang {
21    pub const ALL: [Lang; 5] = [Lang::Math, Lang::Polars, Lang::Rust, Lang::Python, Lang::Plugin];
22    pub fn as_str(self) -> &'static str {
23        match self {
24            Lang::Math => "math",
25            Lang::Polars => "polars",
26            Lang::Rust => "rust",
27            Lang::Python => "python",
28            Lang::Plugin => "plugin",
29        }
30    }
31    pub fn parse(s: &str) -> Option<Lang> {
32        Some(match s.trim().to_ascii_lowercase().as_str() {
33            "math" | "formula" | "formulas" => Lang::Math,
34            "polars" | "pl" => Lang::Polars,
35            "rust" | "rs" => Lang::Rust,
36            "python" | "py" | "rython" => Lang::Python,
37            "plugin" | "prebuilt" | "lib" => Lang::Plugin,
38            _ => return None,
39        })
40    }
41    /// The source is code to compile (not a path to a built library).
42    pub fn compiled(self) -> bool {
43        self != Lang::Plugin
44    }
45    /// File extension for editing the source.
46    pub fn ext(self) -> &'static str {
47        match self {
48            Lang::Math => "math",
49            Lang::Polars => "polars",
50            Lang::Rust => "rs",
51            Lang::Python => "py",
52            Lang::Plugin => "txt",
53        }
54    }
55}