mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Some minor cleanup of metadata code
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user