Skip to main content

openapi_nexus_typescript/config/
module.rs

1//! TypeScript module system definitions
2
3use std::fmt;
4use std::str::FromStr;
5
6use serde::{Deserialize, Serialize};
7
8/// TypeScript module systems
9#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10pub enum TypeScriptModule {
11    CommonJS,
12    ES2020,
13    ES2022,
14    ESNext,
15}
16
17impl FromStr for TypeScriptModule {
18    type Err = String;
19
20    fn from_str(s: &str) -> Result<Self, Self::Err> {
21        match s.to_lowercase().as_str() {
22            "commonjs" | "cjs" => Ok(Self::CommonJS),
23            "esnext" => Ok(Self::ESNext),
24            "es2020" => Ok(Self::ES2020),
25            "es2022" => Ok(Self::ES2022),
26            _ => Err(format!(
27                "Invalid TypeScript module: '{}'. Expected one of: commonjs, esnext, es2020, es2022",
28                s
29            )),
30        }
31    }
32}
33
34impl fmt::Display for TypeScriptModule {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            TypeScriptModule::CommonJS => write!(f, "CommonJS"),
38            TypeScriptModule::ES2020 => write!(f, "ES2020"),
39            TypeScriptModule::ES2022 => write!(f, "ES2022"),
40            TypeScriptModule::ESNext => write!(f, "ESNext"),
41        }
42    }
43}