Skip to main content

pmpx_plugin/
family.rs

1//! The ecosystem family a plugin belongs to.
2//!
3//! An open type rather than a closed enum: the family decides how `plugin ls` groups plugins and
4//! what the keys of `[plugin] <family> = "..."` are called, and a third-party plugin supporting a
5//! new ecosystem must not have to wait for a pmpx release.
6
7use std::fmt;
8
9/// Ecosystem family. Decides the grouping headers of the host's `plugin ls`, the scope of
10/// `plugin set`, and the key names of `[plugin] <family> = "..."` in a project's `.pmpx.toml`.
11/// An open type rather than a closed enum: known ecosystems have constants, unknown ones extend
12/// via [`Family::new`], and comparison and ordering go by string -- a third-party plugin
13/// supporting a new ecosystem needs neither a change to this crate nor a host release.
14#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
15pub struct Family(std::borrow::Cow<'static, str>);
16
17impl Family {
18    /// Node / frontend ecosystem.
19    pub const NODE: Family = Family(std::borrow::Cow::Borrowed("node"));
20    /// Rust ecosystem.
21    pub const RUST: Family = Family(std::borrow::Cow::Borrowed("rust"));
22    /// Python ecosystem.
23    pub const PYTHON: Family = Family(std::borrow::Cow::Borrowed("python"));
24    /// Go ecosystem.
25    pub const GO: Family = Family(std::borrow::Cow::Borrowed("go"));
26    /// JVM ecosystem.
27    pub const JVM: Family = Family(std::borrow::Cow::Borrowed("jvm"));
28    /// .NET ecosystem.
29    pub const DOTNET: Family = Family(std::borrow::Cow::Borrowed("dotnet"));
30    /// PHP ecosystem.
31    pub const PHP: Family = Family(std::borrow::Cow::Borrowed("php"));
32    /// Ruby ecosystem.
33    pub const RUBY: Family = Family(std::borrow::Cow::Borrowed("ruby"));
34
35    /// Construct from a custom name.
36    pub fn new(name: impl Into<std::borrow::Cow<'static, str>>) -> Self {
37        Family(name.into())
38    }
39
40    /// Key form. Used for the `plugin ls` grouping and for `.pmpx.toml` keys.
41    pub fn as_str(&self) -> &str {
42        &self.0
43    }
44
45    /// Human-readable grouping header; unknown ecosystems are returned as-is.
46    pub fn display(&self) -> &str {
47        match self.as_str() {
48            "node" => "Node / frontend",
49            "rust" => "Rust",
50            "python" => "Python",
51            "go" => "Go",
52            "jvm" => "JVM",
53            "dotnet" => ".NET",
54            "php" => "PHP",
55            "ruby" => "Ruby",
56            other => other,
57        }
58    }
59}
60
61impl fmt::Display for Family {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        f.write_str(self.as_str())
64    }
65}
66
67impl From<&'static str> for Family {
68    fn from(s: &'static str) -> Self {
69        Family::new(s)
70    }
71}
72
73impl AsRef<str> for Family {
74    fn as_ref(&self) -> &str {
75        self.as_str()
76    }
77}