Some minor cleanup of metadata code

This commit is contained in:
Oliver Hamlet
2025-03-28 18:00:16 +00:00
parent e7a5bfd5ab
commit b17691dbdd
4 changed files with 23 additions and 22 deletions
+2 -2
View File
@@ -145,7 +145,7 @@ impl TryFrom<&MarkedYaml> for File {
fn try_from(value: &MarkedYaml) -> Result<Self, Self::Error> {
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,
+2 -8
View File
@@ -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<Vec<MessageContent>, 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()
+3 -4
View File
@@ -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);
};
}
}
+16 -8
View File
@@ -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 {