This is an automated email from the ASF dual-hosted git repository. Kriskras99 pushed a commit to branch fix/optimize_alias_new in repository https://gitbox.apache.org/repos/asf/avro-rs.git
commit f3b98e69c882010f84d65966a6c0fc79d72eaff8 Author: Kriskras99 <[email protected]> AuthorDate: Wed Jun 17 20:48:16 2026 +0200 fix: Optimize `Alias::new()` and `Parser::fix_aliases_namespace` --- avro/src/schema/name.rs | 11 ++++++++++- avro/src/schema/parser.rs | 11 ++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/avro/src/schema/name.rs b/avro/src/schema/name.rs index 31a5f1e..aa22cee 100644 --- a/avro/src/schema/name.rs +++ b/avro/src/schema/name.rs @@ -257,15 +257,24 @@ impl<'de> Deserialize<'de> for Name { } /// Newtype pattern for `Name` to better control the `serde_json::Value` representation. +/// /// Aliases are serialized as an array of plain strings in the JSON representation. #[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Alias(Name); impl Alias { - pub fn new(name: &str) -> AvroResult<Self> { + pub fn new(name: impl Into<String> + AsRef<str>) -> AvroResult<Self> { Name::new(name).map(Self) } + /// Create a new `Alias` using the namespace from `enclosing_namespace` if absent. + pub fn new_with_enclosing_namespace( + name: impl Into<String> + AsRef<str>, + enclosing_namespace: NamespaceRef, + ) -> AvroResult<Self> { + Name::new_with_enclosing_namespace(name, enclosing_namespace).map(Self) + } + pub fn name(&self) -> &str { self.0.name() } diff --git a/avro/src/schema/parser.rs b/avro/src/schema/parser.rs index 57ef412..678d646 100644 --- a/avro/src/schema/parser.rs +++ b/avro/src/schema/parser.rs @@ -803,19 +803,12 @@ impl Parser { aliases .map(|aliases| { aliases - .iter() + .into_iter() .map(|alias| { // An alias that is not a valid Avro name is a schema // error — propagate it instead of unwrapping (which // would panic on attacker-controlled schema JSON). - if alias.contains('.') { - Alias::new(alias.as_str()) - } else { - match namespace { - Some(ns) => Alias::new(&format!("{ns}.{alias}")), - None => Alias::new(alias.as_str()), - } - } + Alias::new_with_enclosing_namespace(alias, namespace) }) .collect::<AvroResult<Vec<_>>>() })
