mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Tidy up getting YAML mapping values
This commit is contained in:
@@ -8,8 +8,8 @@ use super::{
|
||||
validate_message_contents,
|
||||
},
|
||||
yaml::{
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, as_string_node,
|
||||
get_required_string_value, get_string_value, parse_condition,
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, get_required_string_value,
|
||||
get_string_value, get_value, parse_condition,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -169,7 +169,7 @@ impl TryFromYaml for File {
|
||||
|
||||
let display_name = get_string_value(h, "display", YamlObjectType::File)?;
|
||||
|
||||
let detail = match h.get(&as_string_node("detail")) {
|
||||
let detail = match get_value(h, "detail") {
|
||||
Some(n) => parse_message_contents_yaml(
|
||||
n,
|
||||
"detail",
|
||||
|
||||
@@ -8,8 +8,8 @@ use super::{
|
||||
ParseMetadataError,
|
||||
},
|
||||
yaml::{
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, as_mapping, as_string_node,
|
||||
get_required_string_value, get_strings_vec_value, parse_condition,
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, as_mapping, get_required_string_value,
|
||||
get_strings_vec_value, get_value, parse_condition,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -282,7 +282,7 @@ impl TryFromYaml for Message {
|
||||
_ => MessageType::Say,
|
||||
};
|
||||
|
||||
let mut content = match mapping.get(&as_string_node("content")) {
|
||||
let mut content = match get_value(mapping, "content") {
|
||||
Some(n) => parse_message_contents_yaml(n, "content", YamlObjectType::Message)?,
|
||||
None => {
|
||||
return Err(ParseMetadataError::missing_key(
|
||||
|
||||
@@ -16,7 +16,9 @@ use super::{
|
||||
group::Group,
|
||||
message::Message,
|
||||
plugin_metadata::PluginMetadata,
|
||||
yaml::{EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, get_as_slice, process_merge_keys},
|
||||
yaml::{
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, get_slice_value, process_merge_keys,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -115,7 +117,7 @@ impl MetadataDocument {
|
||||
|
||||
let mut plugins: HashMap<Filename, PluginMetadata> = HashMap::new();
|
||||
let mut regex_plugins: Vec<PluginMetadata> = Vec::new();
|
||||
for plugin_yaml in get_as_slice(&doc, "plugins", YamlObjectType::MetadataDocument)? {
|
||||
for plugin_yaml in get_slice_value(&doc, "plugins", YamlObjectType::MetadataDocument)? {
|
||||
let plugin = PluginMetadata::try_from_yaml(plugin_yaml)?;
|
||||
if plugin.is_regex_plugin() {
|
||||
regex_plugins.push(plugin);
|
||||
@@ -132,14 +134,14 @@ impl MetadataDocument {
|
||||
}
|
||||
}
|
||||
|
||||
let messages = get_as_slice(&doc, "globals", YamlObjectType::MetadataDocument)?
|
||||
let messages = get_slice_value(&doc, "globals", YamlObjectType::MetadataDocument)?
|
||||
.iter()
|
||||
.map(Message::try_from_yaml)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
let mut bash_tags = Vec::new();
|
||||
let mut str_set = HashSet::new();
|
||||
for bash_tag_yaml in get_as_slice(&doc, "bash_tags", YamlObjectType::MetadataDocument)? {
|
||||
for bash_tag_yaml in get_slice_value(&doc, "bash_tags", YamlObjectType::MetadataDocument)? {
|
||||
let bash_tag: &str = match bash_tag_yaml.data.as_str() {
|
||||
Some(b) => b,
|
||||
None => {
|
||||
@@ -167,7 +169,7 @@ impl MetadataDocument {
|
||||
|
||||
let mut group_names = HashSet::new();
|
||||
let mut groups = Vec::new();
|
||||
for group_yaml in get_as_slice(&doc, "groups", YamlObjectType::MetadataDocument)? {
|
||||
for group_yaml in get_slice_value(&doc, "groups", YamlObjectType::MetadataDocument)? {
|
||||
let group = Group::try_from_yaml(group_yaml)?;
|
||||
|
||||
let name = group.name().to_owned();
|
||||
|
||||
@@ -7,8 +7,8 @@ use super::{
|
||||
validate_message_contents,
|
||||
},
|
||||
yaml::{
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, as_mapping, as_string_node,
|
||||
get_required_string_value, get_u32_value,
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, as_mapping, get_required_string_value,
|
||||
get_u32_value, get_value,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -130,7 +130,7 @@ impl TryFromYaml for PluginCleaningData {
|
||||
let udr = get_u32_value(mapping, "udr", YamlObjectType::PluginCleaningData)?.unwrap_or(0);
|
||||
let nav = get_u32_value(mapping, "nav", YamlObjectType::PluginCleaningData)?.unwrap_or(0);
|
||||
|
||||
let detail = match mapping.get(&as_string_node("detail")) {
|
||||
let detail = match get_value(mapping, "detail") {
|
||||
Some(n) => {
|
||||
parse_message_contents_yaml(n, "detail", YamlObjectType::PluginCleaningData)?
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ use super::{
|
||||
plugin_cleaning_data::PluginCleaningData,
|
||||
tag::Tag,
|
||||
yaml::{
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, as_mapping, get_as_slice,
|
||||
get_required_string_value, get_string_value,
|
||||
EmitYaml, TryFromYaml, YamlEmitter, YamlObjectType, as_mapping, get_required_string_value,
|
||||
get_slice_value, get_string_value,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -444,7 +444,7 @@ fn get_boxed_slice<T: TryFromYaml>(
|
||||
mapping: &saphyr::AnnotatedMapping<MarkedYaml>,
|
||||
key: &'static str,
|
||||
) -> Result<Box<[T]>, ParseMetadataError> {
|
||||
get_as_slice(mapping, key, YamlObjectType::PluginMetadata)?
|
||||
get_slice_value(mapping, key, YamlObjectType::PluginMetadata)?
|
||||
.iter()
|
||||
.map(|e| T::try_from_yaml(e))
|
||||
.collect()
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
use std::sync::LazyLock;
|
||||
|
||||
use saphyr::{MarkedYaml, YamlData};
|
||||
|
||||
use crate::metadata::error::YamlMergeKeyError;
|
||||
|
||||
use super::as_string_node;
|
||||
|
||||
static MERGE_KEY: LazyLock<MarkedYaml> = LazyLock::new(|| as_string_node("<<"));
|
||||
|
||||
pub fn process_merge_keys(mut yaml: MarkedYaml) -> Result<MarkedYaml, YamlMergeKeyError> {
|
||||
match yaml.data {
|
||||
YamlData::Sequence(a) => {
|
||||
@@ -39,7 +33,7 @@ fn merge_mapping_keys<'a, 'b>(
|
||||
})
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
if let Some(value) = mapping.remove(&MERGE_KEY) {
|
||||
if let Some(value) = mapping.remove(&MarkedYaml::value_from_str("<<")) {
|
||||
merge_into_mapping(mapping, value)
|
||||
} else {
|
||||
Ok(mapping)
|
||||
|
||||
@@ -5,7 +5,7 @@ mod parse;
|
||||
pub use emit::{EmitYaml, YamlEmitter};
|
||||
pub use merge::process_merge_keys;
|
||||
pub use parse::{
|
||||
TryFromYaml, YamlObjectType, as_mapping, as_string_node, get_as_slice,
|
||||
get_required_string_value, get_string_value, get_strings_vec_value, get_u32_value,
|
||||
parse_condition, to_unmarked_yaml,
|
||||
TryFromYaml, YamlObjectType, as_mapping, get_required_string_value, get_slice_value,
|
||||
get_string_value, get_strings_vec_value, get_u32_value, get_value, parse_condition,
|
||||
to_unmarked_yaml,
|
||||
};
|
||||
|
||||
@@ -55,8 +55,11 @@ pub fn to_unmarked_yaml<'a>(yaml: &MarkedYaml<'a>) -> Yaml<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_string_node(value: &str) -> MarkedYaml {
|
||||
MarkedYaml::value_from_str(value)
|
||||
pub fn get_value<'a, 'b>(
|
||||
mapping: &'a AnnotatedMapping<'b, MarkedYaml<'b>>,
|
||||
key: &'static str,
|
||||
) -> Option<&'a MarkedYaml<'b>> {
|
||||
mapping.get(&MarkedYaml::value_from_str(key))
|
||||
}
|
||||
|
||||
pub fn get_string_value<'a>(
|
||||
@@ -64,7 +67,7 @@ pub fn get_string_value<'a>(
|
||||
key: &'static str,
|
||||
yaml_type: YamlObjectType,
|
||||
) -> Result<Option<(Marker, &'a str)>, ParseMetadataError> {
|
||||
match mapping.get(&as_string_node(key)) {
|
||||
match get_value(mapping, key) {
|
||||
Some(n) => match n.data.as_str() {
|
||||
Some(s) => Ok(Some((n.span.start, s))),
|
||||
None => Err(ParseMetadataError::unexpected_value_type(
|
||||
@@ -95,7 +98,7 @@ pub fn get_strings_vec_value<'a>(
|
||||
key: &'static str,
|
||||
yaml_type: YamlObjectType,
|
||||
) -> Result<Vec<&'a str>, ParseMetadataError> {
|
||||
match mapping.get(&as_string_node(key)) {
|
||||
match get_value(mapping, key) {
|
||||
Some(n) => match n.data.as_vec() {
|
||||
Some(n) => n
|
||||
.iter()
|
||||
@@ -139,7 +142,7 @@ pub fn get_u32_value(
|
||||
key: &'static str,
|
||||
yaml_type: YamlObjectType,
|
||||
) -> Result<Option<u32>, ParseMetadataError> {
|
||||
match mapping.get(&as_string_node(key)) {
|
||||
match get_value(mapping, key) {
|
||||
Some(n) => match n.data.as_integer() {
|
||||
Some(i) => i.try_into().map(Some).map_err(|_e| {
|
||||
ParseMetadataError::new(n.span.start, MetadataParsingErrorReason::NonU32Number(i))
|
||||
@@ -155,12 +158,12 @@ pub fn get_u32_value(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_as_slice<'a>(
|
||||
pub fn get_slice_value<'a>(
|
||||
mapping: &'a saphyr::AnnotatedMapping<MarkedYaml>,
|
||||
key: &'static str,
|
||||
yaml_type: YamlObjectType,
|
||||
) -> Result<&'a [MarkedYaml<'a>], ParseMetadataError> {
|
||||
if let Some(value) = mapping.get(&as_string_node(key)) {
|
||||
if let Some(value) = get_value(mapping, key) {
|
||||
match value.data.as_vec() {
|
||||
Some(n) => Ok(n.as_slice()),
|
||||
None => Err(ParseMetadataError::unexpected_value_type(
|
||||
|
||||
Reference in New Issue
Block a user