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
46impl fmt::Display for Family {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(self.as_str())
49    }
50}
51
52impl From<&'static str> for Family {
53    fn from(s: &'static str) -> Self {
54        Family::new(s)
55    }
56}
57
58impl AsRef<str> for Family {
59    fn as_ref(&self) -> &str {
60        self.as_str()
61    }
62}