use std::fmt;
use serde::{Deserialize, Serialize};
pub const PLUGIN_ID_PATTERN: &str = r"^[a-z][a-z0-9-]*(\.[a-z][a-z0-9-]*)+$";
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
#[cfg_attr(feature = "codegen", derive(ts_rs::TS))]
#[cfg_attr(feature = "codegen", ts(type = "string"))]
pub struct PluginId(String);
impl<'de> Deserialize<'de> for PluginId {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
String::deserialize(d)?
.try_into()
.map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginIdError(pub String);
impl fmt::Display for PluginIdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"invalid plugin id {:?}: expected reverse-domain form like \"com.example.my-plugin\"",
self.0
)
}
}
impl std::error::Error for PluginIdError {}
impl PluginId {
pub fn as_str(&self) -> &str {
&self.0
}
fn is_valid(s: &str) -> bool {
let mut segments = 0;
for seg in s.split('.') {
let mut chars = seg.chars();
let ok = chars.next().is_some_and(|c| c.is_ascii_lowercase())
&& chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-');
if !ok {
return false;
}
segments += 1;
}
segments >= 2
}
}
impl TryFrom<String> for PluginId {
type Error = PluginIdError;
fn try_from(s: String) -> Result<Self, Self::Error> {
if Self::is_valid(&s) {
Ok(Self(s))
} else {
Err(PluginIdError(s))
}
}
}
impl std::str::FromStr for PluginId {
type Err = PluginIdError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.to_owned().try_into()
}
}
impl From<PluginId> for String {
fn from(id: PluginId) -> Self {
id.0
}
}
impl fmt::Display for PluginId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(feature = "codegen")]
impl schemars::JsonSchema for PluginId {
fn schema_name() -> std::borrow::Cow<'static, str> {
"PluginId".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"pattern": PLUGIN_ID_PATTERN,
"description": "Reverse-domain plugin id, e.g. \"com.example.my-plugin\"."
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn accepts_reverse_domain_ids() {
let ids = ["com.example.fmea-check", "jp.modelith.iso15288-rules"];
let results: Vec<bool> = ids
.iter()
.map(|id| id.parse::<PluginId>().is_ok())
.collect();
assert_eq!(results, [true, true]);
}
#[test]
fn accepts_minimum_two_single_letter_segments() {
let id = "a.b";
let result = id.parse::<PluginId>();
assert!(result.is_ok());
}
#[test]
fn rejects_malformed_ids() {
let ids = [
"",
"single",
"Com.example",
"com..example",
"com.example.",
"1com.x",
"com.ex_ample",
];
let accepted: Vec<&str> = ids
.iter()
.copied()
.filter(|id| id.parse::<PluginId>().is_ok())
.collect();
assert!(accepted.is_empty(), "accepted: {accepted:?}");
}
#[test]
fn deserialize_accepts_valid_id() {
let json = r#""com.example.ok""#;
let result = serde_json::from_str::<PluginId>(json);
assert_eq!(result.unwrap().as_str(), "com.example.ok");
}
#[test]
fn deserialize_rejects_invalid_id() {
let json = r#""NotValid""#;
let result = serde_json::from_str::<PluginId>(json);
assert!(result.is_err());
}
}