Skip to main content

changepacks_core/
language.rs

1use colored::Colorize;
2use std::fmt::Display;
3
4/// Supported programming languages and their corresponding package manager ecosystems.
5///
6/// Each variant represents a language that changepacks can manage versions for.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
8pub enum Language {
9    /// Python projects using pyproject.toml (pip, uv)
10    Python,
11    /// Node.js projects using package.json (npm, pnpm, yarn, bun)
12    Node,
13    /// Rust projects using Cargo.toml (cargo)
14    Rust,
15    /// Dart projects using pubspec.yaml (pub)
16    Dart,
17    /// C# projects using .csproj (`NuGet`, `dotnet`)
18    CSharp,
19    /// Java projects using build.gradle or build.gradle.kts (Gradle)
20    Java,
21}
22
23impl Language {
24    /// Returns the config key used for publish command lookup
25    #[must_use]
26    pub const fn publish_key(&self) -> &'static str {
27        match self {
28            Self::Node => "node",
29            Self::Python => "python",
30            Self::Rust => "rust",
31            Self::Dart => "dart",
32            Self::CSharp => "csharp",
33            Self::Java => "java",
34        }
35    }
36}
37
38impl Display for Language {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        write!(
41            f,
42            "{}",
43            match self {
44                Self::Python => "Python".yellow().bold(),
45                Self::Node => "Node.js".green().bold(),
46                Self::Rust => "Rust".truecolor(139, 69, 19).bold(),
47                Self::Dart => "Dart".blue().bold(),
48                Self::CSharp => "C#".magenta().bold(),
49                Self::Java => "Java".red().bold(),
50            }
51        )
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use rstest::rstest;
59
60    #[rstest]
61    #[case(Language::Python, "Python")]
62    #[case(Language::Node, "Node")]
63    #[case(Language::Rust, "Rust")]
64    #[case(Language::Dart, "Dart")]
65    #[case(Language::CSharp, "C#")]
66    #[case(Language::Java, "Java")]
67    fn test_language_display(#[case] language: Language, #[case] expected: &str) {
68        let display = format!("{}", language);
69        assert!(display.contains(expected));
70    }
71
72    #[rstest]
73    #[case(Language::Python, "python")]
74    #[case(Language::Node, "node")]
75    #[case(Language::Rust, "rust")]
76    #[case(Language::Dart, "dart")]
77    #[case(Language::CSharp, "csharp")]
78    #[case(Language::Java, "java")]
79    fn test_publish_key(#[case] language: Language, #[case] expected: &str) {
80        assert_eq!(language.publish_key(), expected);
81    }
82}