apache_avro/schema/
name.rs1use serde::{Deserialize, Serialize, Serializer};
19use serde_json::{Map, Value};
20use std::borrow::Cow;
21use std::collections::HashMap;
22use std::fmt::{Debug, Display, Formatter};
23use std::str::FromStr;
24
25use crate::{
26 AvroResult, Error, Schema,
27 error::Details,
28 util::MapHelper,
29 validator::{validate_namespace, validate_schema_name},
30};
31
32#[derive(Clone, Hash, PartialEq, Eq)]
43pub struct Name {
44 namespace_and_name: String,
46 index_of_name: usize,
50}
51
52pub type Aliases = Option<Vec<Alias>>;
54pub type Names = HashMap<Name, Schema>;
56pub type NamesRef<'a> = HashMap<Name, &'a Schema>;
58pub type Namespace = Option<String>;
60pub type NamespaceRef<'a> = Option<&'a str>;
62
63impl Name {
64 pub fn new(name: impl Into<String> + AsRef<str>) -> AvroResult<Self> {
68 Self::new_with_enclosing_namespace(name, None)
69 }
70
71 pub fn new_with_enclosing_namespace(
73 name: impl Into<String> + AsRef<str>,
74 enclosing_namespace: NamespaceRef,
75 ) -> AvroResult<Self> {
76 let name_ref = name.as_ref();
83 let index_of_name = validate_schema_name(name_ref)?;
84 if index_of_name > name_ref.len() {
85 return Err(Details::InvalidSchemaNameValidatorImplementation.into());
86 }
87
88 if index_of_name == 0
89 && let Some(namespace) = enclosing_namespace
90 && !namespace.is_empty()
91 {
92 validate_namespace(namespace)?;
93 Ok(Self {
94 namespace_and_name: format!("{namespace}.{name_ref}"),
95 index_of_name: namespace.len() + 1,
96 })
97 } else if index_of_name == 1 {
98 Ok(Self {
100 namespace_and_name: name.as_ref()[1..].into(),
101 index_of_name: 0,
102 })
103 } else {
104 Ok(Self {
105 namespace_and_name: name.into(),
106 index_of_name,
107 })
108 }
109 }
110
111 pub(crate) fn parse(
113 complex: &Map<String, Value>,
114 enclosing_namespace: NamespaceRef,
115 ) -> AvroResult<Self> {
116 let name_field = complex.name().ok_or(Details::GetNameField)?;
117 Self::new_with_enclosing_namespace(
118 name_field,
119 complex.string("namespace").or(enclosing_namespace),
120 )
121 }
122
123 pub fn name(&self) -> &str {
124 &self.namespace_and_name[self.index_of_name..]
125 }
126
127 pub fn namespace(&self) -> NamespaceRef<'_> {
128 if self.index_of_name == 0 {
129 None
130 } else {
131 Some(&self.namespace_and_name[..(self.index_of_name - 1)])
132 }
133 }
134
135 pub fn fullname(&self, enclosing_namespace: NamespaceRef) -> String {
140 if self.index_of_name == 0
141 && let Some(namespace) = enclosing_namespace
142 && !namespace.is_empty()
143 {
144 format!("{namespace}.{}", self.namespace_and_name)
145 } else {
146 self.namespace_and_name.clone()
147 }
148 }
149
150 pub fn fully_qualified_name(&self, enclosing_namespace: NamespaceRef) -> Cow<'_, Name> {
165 if self.index_of_name == 0
166 && let Some(namespace) = enclosing_namespace
167 && !namespace.is_empty()
168 {
169 Cow::Owned(Self {
170 namespace_and_name: format!("{namespace}.{}", self.namespace_and_name),
171 index_of_name: namespace.len() + 1,
172 })
173 } else {
174 Cow::Borrowed(self)
175 }
176 }
177
178 pub(crate) fn invalid_empty_name() -> Self {
185 Self {
186 namespace_and_name: String::new(),
187 index_of_name: usize::MAX,
188 }
189 }
190}
191
192impl TryFrom<&str> for Name {
193 type Error = Error;
194
195 fn try_from(value: &str) -> Result<Self, Self::Error> {
196 Self::new(value)
197 }
198}
199
200impl TryFrom<String> for Name {
201 type Error = Error;
202
203 fn try_from(value: String) -> Result<Self, Self::Error> {
204 Self::new(&value)
205 }
206}
207
208impl FromStr for Name {
209 type Err = Error;
210
211 fn from_str(s: &str) -> Result<Self, Self::Err> {
212 Self::new(s)
213 }
214}
215
216impl Debug for Name {
217 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
218 if self.index_of_name > self.namespace_and_name.len() {
219 f.debug_tuple("Name").field(&"Invalid name!").finish()
220 } else {
221 let mut debug = f.debug_struct("Name");
222 debug.field("name", &self.name());
223 if self.index_of_name != 0 {
224 debug.field("namespace", &self.namespace());
225 debug.finish()
226 } else {
227 debug.finish_non_exhaustive()
228 }
229 }
230 }
231}
232
233impl Display for Name {
234 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
235 assert!(
236 self.index_of_name <= self.namespace_and_name.len(),
237 "Invalid name used"
238 );
239 f.write_str(&self.namespace_and_name)
240 }
241}
242
243impl<'de> Deserialize<'de> for Name {
244 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
245 where
246 D: serde::de::Deserializer<'de>,
247 {
248 Value::deserialize(deserializer).and_then(|value| {
249 use serde::de::Error;
250 if let Value::Object(json) = value {
251 Name::parse(&json, None).map_err(Error::custom)
252 } else {
253 Err(Error::custom(format!("Expected a JSON object: {value:?}")))
254 }
255 })
256 }
257}
258
259#[derive(Clone, Debug, Hash, PartialEq, Eq)]
262pub struct Alias(Name);
263
264impl Alias {
265 pub fn new(name: &str) -> AvroResult<Self> {
266 Name::new(name).map(Self)
267 }
268
269 pub fn name(&self) -> &str {
270 self.0.name()
271 }
272
273 pub fn namespace(&self) -> NamespaceRef<'_> {
274 self.0.namespace()
275 }
276
277 pub fn fullname(&self, enclosing_namespace: NamespaceRef) -> String {
278 self.0.fullname(enclosing_namespace)
279 }
280
281 pub fn fully_qualified_name(&self, default_namespace: NamespaceRef) -> Cow<'_, Name> {
282 self.0.fully_qualified_name(default_namespace)
283 }
284}
285
286impl TryFrom<&str> for Alias {
287 type Error = Error;
288
289 fn try_from(value: &str) -> Result<Self, Self::Error> {
290 Self::new(value)
291 }
292}
293
294impl TryFrom<String> for Alias {
295 type Error = Error;
296
297 fn try_from(value: String) -> Result<Self, Self::Error> {
298 Self::new(&value)
299 }
300}
301
302impl FromStr for Alias {
303 type Err = Error;
304
305 fn from_str(s: &str) -> Result<Self, Self::Err> {
306 Self::new(s)
307 }
308}
309
310impl Serialize for Alias {
311 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
312 where
313 S: Serializer,
314 {
315 serializer.serialize_str(&self.fullname(None))
316 }
317}
318
319#[cfg(test)]
320mod tests {
321 use crate::Error;
322
323 use super::*;
324 use apache_avro_test_helper::TestResult;
325
326 #[test]
327 fn test_namespace_from_name_with_empty_value() -> TestResult {
329 let name = Name::new(".name")?;
330 assert_eq!(name.namespace_and_name, "name");
331 assert_eq!(name.index_of_name, 0);
332
333 Ok(())
334 }
335
336 #[test]
337 fn test_name_with_whitespace_value() {
339 match Name::new(" ").map_err(Error::into_details) {
340 Err(Details::InvalidSchemaName(_, _)) => {}
341 _ => panic!("Expected an Details::InvalidSchemaName!"),
342 }
343 }
344
345 #[test]
346 fn test_name_with_no_name_part() {
348 match Name::new("space.").map_err(Error::into_details) {
349 Err(Details::InvalidSchemaName(_, _)) => {}
350 _ => panic!("Expected an Details::InvalidSchemaName!"),
351 }
352 }
353
354 #[test]
357 fn test_avro_3897_funny_valid_names_and_namespaces() -> TestResult {
358 for funny_name in ["_", "_._", "__._", "_.__", "_._._"] {
359 let name = Name::new(funny_name);
360 assert!(name.is_ok());
361 }
362 Ok(())
363 }
364}