From b17691dbdd6ebb0d4e357aadd5fb73937cccefde Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Thu, 27 Mar 2025 17:40:19 +0000 Subject: [PATCH] Some minor cleanup of metadata code --- src/metadata/file.rs | 4 ++-- src/metadata/message.rs | 10 ++-------- src/metadata/metadata_document.rs | 7 +++---- src/metadata/plugin_metadata.rs | 24 ++++++++++++++++-------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/metadata/file.rs b/src/metadata/file.rs index 96fdc121..593f98ba 100644 --- a/src/metadata/file.rs +++ b/src/metadata/file.rs @@ -145,7 +145,7 @@ impl TryFrom<&MarkedYaml> for File { fn try_from(value: &MarkedYaml) -> Result { match &value.data { YamlData::String(s) => Ok(File { - name: Filename(UniCase::new(s.to_string())), + name: Filename::new(s.clone()), display_name: None, detail: Vec::new(), condition: None, @@ -168,7 +168,7 @@ impl TryFrom<&MarkedYaml> for File { let condition = parse_condition(h, YamlObjectType::File)?; Ok(File { - name: Filename(UniCase::new(name.to_string())), + name: Filename::new(name.to_string()), display_name: display_name.map(|(_, s)| s.to_string()), detail, condition, diff --git a/src/metadata/message.rs b/src/metadata/message.rs index 1b463495..55d424d5 100644 --- a/src/metadata/message.rs +++ b/src/metadata/message.rs @@ -173,10 +173,7 @@ impl Message { pub fn new(message_type: MessageType, content: String) -> Self { Self { message_type, - content: vec![MessageContent { - text: content, - language: MessageContent::DEFAULT_LANGUAGE.to_string(), - }], + content: vec![MessageContent::new(content)], condition: None, } } @@ -268,10 +265,7 @@ pub(crate) fn parse_message_contents_yaml( ) -> Result, ParseMetadataError> { let contents = match &value.data { YamlData::String(s) => { - vec![MessageContent { - text: s.to_string(), - language: MessageContent::DEFAULT_LANGUAGE.to_string(), - }] + vec![MessageContent::new(s.clone())] } YamlData::Array(a) => a .iter() diff --git a/src/metadata/metadata_document.rs b/src/metadata/metadata_document.rs index 861cb653..8619e0d5 100644 --- a/src/metadata/metadata_document.rs +++ b/src/metadata/metadata_document.rs @@ -124,15 +124,14 @@ impl MetadataDocument { regex_plugins.push(plugin); } else { let filename = Filename::new(plugin.name().to_string()); - if plugins.contains_key(&filename) { + if let Some(old) = plugins.insert(filename, plugin) { return Err(ParseMetadataError::duplicate_entry( plugin_yaml.span.start, - plugin.name().to_string(), + old.name().to_string(), YamlObjectType::PluginMetadata, ) .into()); - } - plugins.insert(filename, plugin); + }; } } diff --git a/src/metadata/plugin_metadata.rs b/src/metadata/plugin_metadata.rs index 5fc3c9f4..880f4b04 100644 --- a/src/metadata/plugin_metadata.rs +++ b/src/metadata/plugin_metadata.rs @@ -190,7 +190,7 @@ impl PluginMetadata { /// Returns `true` if the plugin name contains any of the characters `:\*?|` /// and `false` otherwise. pub fn is_regex_plugin(&self) -> bool { - self.name.regex.is_some() + self.name.is_regex() } /// Check if the given plugin name matches this plugin metadata object's @@ -201,13 +201,7 @@ impl PluginMetadata { /// case-insensitively. The given plugin name must be literal, i.e. not a /// regular expression. pub fn name_matches(&self, other_name: &str) -> bool { - if let Some(regex) = &self.name.regex { - regex.is_match(other_name).inspect_err(|e| { - logging::error!("Encountered an error while trying to match the regex {} to the string {}: {}", regex.as_str(), other_name, e); - }).unwrap_or(false) - } else { - unicase::eq(self.name.string.as_str(), other_name) - } + self.name.matches(other_name) } /// Serialises the plugin metadata as YAML. @@ -257,6 +251,20 @@ impl PluginName { }) } } + + fn matches(&self, other_name: &str) -> bool { + if let Some(regex) = &self.regex { + regex.is_match(other_name).inspect_err(|e| { + logging::error!("Encountered an error while trying to match the regex {} to the string {}: {}", regex.as_str(), other_name, e); + }).unwrap_or(false) + } else { + unicase::eq(self.string.as_str(), other_name) + } + } + + fn is_regex(&self) -> bool { + self.regex.is_some() + } } impl std::cmp::PartialEq for PluginName {