onetaskgraph_plugin_api/
id.rs1use std::fmt;
8
9use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
10use serde::{Deserialize, Serialize};
11
12use crate::SourceError;
13
14#[derive(
20 Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, JsonSchema,
21)]
22#[serde(transparent)]
23pub struct NativeId(pub String);
24
25impl NativeId {
26 #[must_use]
28 pub fn as_str(&self) -> &str {
29 &self.0
30 }
31}
32
33impl From<&str> for NativeId {
34 fn from(value: &str) -> Self {
35 Self(value.to_owned())
36 }
37}
38
39impl From<String> for NativeId {
40 fn from(value: String) -> Self {
41 Self(value)
42 }
43}
44
45impl fmt::Display for NativeId {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 f.write_str(&self.0)
48 }
49}
50
51pub const SOURCE_NAME_PATTERN: &str = "^[a-z0-9][a-z0-9-]*$";
57
58#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
68#[serde(try_from = "String", into = "String")]
69pub struct SourceName(String);
70
71impl SourceName {
72 pub fn new(value: impl Into<String>) -> Result<Self, SourceError> {
79 let value = value.into();
80 if Self::is_valid(&value) {
81 Ok(Self(value))
82 } else {
83 Err(SourceError::Config {
84 message: format!(
85 "source name {value:?} is not usable; names must match {SOURCE_NAME_PATTERN} \
86 (lower-case letters, digits and hyphens, starting with a letter or digit)"
87 ),
88 })
89 }
90 }
91
92 fn is_valid(value: &str) -> bool {
98 let mut chars = value.chars();
99 let Some(first) = chars.next() else {
100 return false;
101 };
102 if !(first.is_ascii_lowercase() || first.is_ascii_digit()) {
103 return false;
104 }
105 chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
106 }
107
108 #[must_use]
110 pub fn as_str(&self) -> &str {
111 &self.0
112 }
113}
114
115impl TryFrom<String> for SourceName {
116 type Error = SourceError;
117
118 fn try_from(value: String) -> Result<Self, Self::Error> {
119 Self::new(value)
120 }
121}
122
123impl From<SourceName> for String {
124 fn from(value: SourceName) -> Self {
125 value.0
126 }
127}
128
129impl fmt::Display for SourceName {
130 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131 f.write_str(&self.0)
132 }
133}
134
135impl JsonSchema for SourceName {
136 fn schema_name() -> std::borrow::Cow<'static, str> {
137 "SourceName".into()
138 }
139
140 fn json_schema(_generator: &mut SchemaGenerator) -> Schema {
141 json_schema!({
142 "type": "string",
143 "pattern": SOURCE_NAME_PATTERN,
144 "description": "The name a configuration document gives one configured source.",
145 })
146 }
147}