diff --git a/kbinxml/src/byte_buffer.rs b/kbinxml/src/byte_buffer.rs index 5b039ab..e07d4e2 100644 --- a/kbinxml/src/byte_buffer.rs +++ b/kbinxml/src/byte_buffer.rs @@ -71,7 +71,7 @@ pub enum ByteBufferError { } /// Remove trailing null bytes, used for the `String` type -pub(crate) fn strip_trailing_null_bytes<'a>(data: &'a [u8]) -> &'a [u8] { +pub(crate) fn strip_trailing_null_bytes(data: &[u8]) -> &[u8] { let len = data.len(); if len == 0 { diff --git a/kbinxml/src/encoding_type.rs b/kbinxml/src/encoding_type.rs index 507c84f..01f823c 100644 --- a/kbinxml/src/encoding_type.rs +++ b/kbinxml/src/encoding_type.rs @@ -85,7 +85,7 @@ impl EncodingType { e if e == EUC_JP => Ok(EncodingType::EUC_JP), e if e == SHIFT_JIS => Ok(EncodingType::SHIFT_JIS), e if e == UTF_8 => Ok(EncodingType::UTF_8), - _ => return Err(EncodingError::UnknownEncoding), + _ => Err(EncodingError::UnknownEncoding), } } diff --git a/kbinxml/src/lib.rs b/kbinxml/src/lib.rs index d947a59..b0d0136 100644 --- a/kbinxml/src/lib.rs +++ b/kbinxml/src/lib.rs @@ -54,9 +54,9 @@ pub fn is_binary_xml(input: &[u8]) -> bool { } pub fn from_binary(input: Bytes) -> Result<(NodeCollection, EncodingType)> { - let mut reader = Reader::new(input)?; - let collection = NodeCollection::from_iter(&mut reader).ok_or(KbinError::NoNodeCollection)?; + let reader = Reader::new(input)?; let encoding = reader.encoding(); + let collection = reader.collect::>().ok_or(KbinError::NoNodeCollection)?; Ok((collection, encoding)) } @@ -105,5 +105,6 @@ where T: ToTextXml, { let writer = TextXmlWriter::new(); - writer.to_text_xml(input) + + writer.into_text_xml(input) } diff --git a/kbinxml/src/node/collection.rs b/kbinxml/src/node/collection.rs index 9376db7..a16381f 100644 --- a/kbinxml/src/node/collection.rs +++ b/kbinxml/src/node/collection.rs @@ -1,6 +1,6 @@ use std::collections::VecDeque; use std::fmt; -use std::iter::Iterator; +use std::iter::{FromIterator, Iterator}; use crate::error::KbinError; use crate::node::{Node, NodeDefinition}; @@ -39,19 +39,6 @@ impl NodeCollection { } } - pub fn from_iter(iter: &mut I) -> Option - where - I: Iterator, - { - let base = if let Some(def) = iter.next() { - def - } else { - return None; - }; - - NodeCollection::from_iter_base(base, iter) - } - fn from_iter_base(base: NodeDefinition, iter: &mut I) -> Option where I: Iterator, @@ -59,18 +46,14 @@ impl NodeCollection { let mut attributes = VecDeque::new(); let mut children = VecDeque::new(); - loop { - if let Some(def) = iter.next() { - match def.node_type { - StandardType::Attribute => attributes.push_back(def), - StandardType::NodeEnd | StandardType::FileEnd => break, - _ => match NodeCollection::from_iter_base(def, iter) { - Some(child) => children.push_back(child), - None => return None, - }, - } - } else { - break; + while let Some(def) = iter.next() { + match def.node_type { + StandardType::Attribute => attributes.push_back(def), + StandardType::NodeEnd | StandardType::FileEnd => break, + _ => match NodeCollection::from_iter_base(def, iter) { + Some(child) => children.push_back(child), + None => return None, + }, } } @@ -120,7 +103,7 @@ impl NodeCollection { if let Value::Attribute(value) = attr.value()? { node.set_attr(key, value); } else { - return Err(KbinError::InvalidState.into()); + return Err(KbinError::InvalidState); } } @@ -141,7 +124,7 @@ impl NodeCollection { let target_opt = if let Some(index) = parse_index(token) { target.children().get(index) } else { - target.children().iter().find(|ref child| { + target.children().iter().find(|child| { child .base() .key() @@ -162,6 +145,18 @@ impl NodeCollection { } } +impl FromIterator for Option { + fn from_iter(iter: T) -> Self + where + T: IntoIterator, + { + let mut iter = iter.into_iter(); + let base = iter.next()?; + + NodeCollection::from_iter_base(base, &mut iter) + } +} + struct DisplayDebugWrapper<'a, T: fmt::Display + 'a>(&'a T, bool); impl<'a, T> fmt::Debug for DisplayDebugWrapper<'a, T> where diff --git a/kbinxml/src/node/definition.rs b/kbinxml/src/node/definition.rs index ab73ae0..6363b36 100644 --- a/kbinxml/src/node/definition.rs +++ b/kbinxml/src/node/definition.rs @@ -79,12 +79,12 @@ impl NodeDefinition { } #[inline] - pub fn data<'a>(&'a self) -> &'a NodeData { + pub fn data(&self) -> &NodeData { &self.data } #[inline] - pub fn data_mut<'a>(&'a mut self) -> &'a mut NodeData { + pub fn data_mut(&mut self) -> &mut NodeData { &mut self.data } @@ -118,7 +118,7 @@ impl NodeDefinition { } } - pub fn value_bytes<'a>(&'a self) -> Option<&'a [u8]> { + pub fn value_bytes(&self) -> Option<&[u8]> { match self.data { NodeData::Some { ref value_data, .. } => Some(value_data), NodeData::None => None, diff --git a/kbinxml/src/printer.rs b/kbinxml/src/printer.rs index a867131..c24151a 100644 --- a/kbinxml/src/printer.rs +++ b/kbinxml/src/printer.rs @@ -53,9 +53,10 @@ impl Printer { }; } - let collection = NodeCollection::from_iter(&mut definitions.into_iter()); + let collection: Option = definitions.into_iter().collect(); + match collection { - Some(ref collection) => eprintln!("collection: {:#}", collection), + Some(collection) => eprintln!("collection: {:#}", collection), None => eprintln!("collection: {:?}", collection), }; diff --git a/kbinxml/src/sixbit.rs b/kbinxml/src/sixbit.rs index c326726..fcc1682 100644 --- a/kbinxml/src/sixbit.rs +++ b/kbinxml/src/sixbit.rs @@ -4,8 +4,7 @@ use std::io::{self, Read, Write}; use byteorder::{ReadBytesExt, WriteBytesExt}; use snafu::{ResultExt, Snafu}; -static CHAR_MAP: &'static [u8] = - b"0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; +const CHAR_MAP: &[u8] = b"0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"; lazy_static! { static ref BYTE_MAP: HashMap = { diff --git a/kbinxml/src/to_text_xml/mod.rs b/kbinxml/src/to_text_xml/mod.rs index e069495..bfa6b49 100644 --- a/kbinxml/src/to_text_xml/mod.rs +++ b/kbinxml/src/to_text_xml/mod.rs @@ -26,7 +26,7 @@ impl TextXmlWriter { Self { xml_writer } } - pub fn to_text_xml(mut self, value: &T) -> Result, KbinError> + pub fn into_text_xml(mut self, value: &T) -> Result, KbinError> where T: ToTextXml, { diff --git a/kbinxml/src/to_text_xml/node.rs b/kbinxml/src/to_text_xml/node.rs index 4122ba1..d992a38 100644 --- a/kbinxml/src/to_text_xml/node.rs +++ b/kbinxml/src/to_text_xml/node.rs @@ -52,7 +52,7 @@ impl ToTextXml for Node { } for (key, value) in self.attributes() { - let value = BytesText::from_plain_str(&value); + let value = BytesText::from_plain_str(value); elem.push_attribute(Attribute { key: key.as_bytes(), diff --git a/kbinxml/src/types/string.rs b/kbinxml/src/types/string.rs index b413daf..e4f7edb 100644 --- a/kbinxml/src/types/string.rs +++ b/kbinxml/src/types/string.rs @@ -13,7 +13,7 @@ pub trait FromKbinString: Sized { fn space_check(input: &str) -> Result<()> { // check for space character if input.find(' ').is_some() { - return Err(KbinError::InvalidState.into()); + return Err(KbinError::InvalidState); } Ok(()) @@ -87,8 +87,8 @@ macro_rules! basic_int_parse { fn from_kbin_string(input: &str) -> Result { space_check(input)?; - if input.starts_with("0x") { - <$type>::from_str_radix(&input[2..], 16) + if let Some(input) = input.strip_prefix("0x") { + <$type>::from_str_radix(input, 16) .context(StringParseIntSnafu { node_type: stringify!($type) }) } else { input diff --git a/kbinxml/src/value/array.rs b/kbinxml/src/value/array.rs index 28ebbc5..0820e11 100644 --- a/kbinxml/src/value/array.rs +++ b/kbinxml/src/value/array.rs @@ -174,6 +174,14 @@ macro_rules! type_impl { } } + pub fn is_empty(&self) -> bool { + match self { + $( + ValueArray::$konst(values) => values.is_empty(), + )* + } + } + pub fn len(&self) -> usize { match self { $( @@ -186,25 +194,25 @@ macro_rules! type_impl { impl ValueArray { type_impl! { - S8, U8, - S16, U16, - S32, U32, - S64, U64, - Ip4, - Float, - Double, - Boolean, - S8_2, S8_3, S8_4, Vs8, - U8_2, U8_3, U8_4, Vu8, - Boolean2, Boolean3, Boolean4, Vb, - S16_2, S16_3, S16_4, Vs16, - S32_2, S32_3, S32_4, - S64_2, S64_3, S64_4, - U16_2, U16_3, U16_4, Vu16, - U32_2, U32_3, U32_4, - U64_2, U64_3, U64_4, - Float2, Float3, Float4, - Double2, Double3, Double4, + S8, U8, + S16, U16, + S32, U32, + S64, U64, + Ip4, + Float, + Double, + Boolean, + S8_2, S8_3, S8_4, Vs8, + U8_2, U8_3, U8_4, Vu8, + Boolean2, Boolean3, Boolean4, Vb, + S16_2, S16_3, S16_4, Vs16, + S32_2, S32_3, S32_4, + S64_2, S64_3, S64_4, + U16_2, U16_3, U16_4, Vu16, + U32_2, U32_3, U32_4, + U64_2, U64_3, U64_4, + Float2, Float3, Float4, + Double2, Double3, Double4, } } @@ -221,7 +229,7 @@ fn write_values(f: &mut fmt::Formatter, values: &[T]) -> fmt::R macro_rules! write_array { ($method:ident, $num:expr) => { fn $method(f: &mut fmt::Formatter, values: &[[T; $num]]) -> fmt::Result { - for (i, v) in values.iter().flat_map(|v| v.into_iter()).enumerate() { + for (i, v) in values.iter().flat_map(|v| v.iter()).enumerate() { if i > 0 { f.write_str(" ")?; } @@ -296,7 +304,7 @@ impl fmt::Display for ValueArray { Ok(()) }, ValueArray::Boolean2(values) => { - for (i, v) in values.iter().flat_map(|v| v.into_iter()).enumerate() { + for (i, v) in values.iter().flat_map(|v| v.iter()).enumerate() { if i > 0 { f.write_str(" ")?; } @@ -305,7 +313,7 @@ impl fmt::Display for ValueArray { Ok(()) }, ValueArray::Boolean3(values) => { - for (i, v) in values.iter().flat_map(|v| v.into_iter()).enumerate() { + for (i, v) in values.iter().flat_map(|v| v.iter()).enumerate() { if i > 0 { f.write_str(" ")?; } @@ -314,7 +322,7 @@ impl fmt::Display for ValueArray { Ok(()) }, ValueArray::Boolean4(values) => { - for (i, v) in values.iter().flat_map(|v| v.into_iter()).enumerate() { + for (i, v) in values.iter().flat_map(|v| v.iter()).enumerate() { if i > 0 { f.write_str(" ")?; } diff --git a/kbinxml/src/writer.rs b/kbinxml/src/writer.rs index ccb9b84..e5c8543 100644 --- a/kbinxml/src/writer.rs +++ b/kbinxml/src/writer.rs @@ -132,7 +132,7 @@ fn write_value( .write_u32::(size) .context(NodeSizeSnafu { node_type, size })?; data_buf - .write_all(&data) + .write_all(data) .context(DataWriteSnafu { node_type })?; data_buf .realign_writes(None) @@ -140,7 +140,7 @@ fn write_value( }, Value::String(text) => { data_buf - .write_str(options.encoding, &text) + .write_str(options.encoding, text) .context(DataBufferSnafu { node_type })?; }, Value::Array(values) => { @@ -312,8 +312,8 @@ impl Writeable for Node { data_buf: &mut ByteBufferWrite, ) -> Result<(), WriterError> { let (node_type, is_array) = match self.value() { - Some(Value::Array(ref values)) => (values.standard_type(), true), - Some(ref value) => (value.standard_type(), false), + Some(Value::Array(values)) => (values.standard_type(), true), + Some(value) => (value.standard_type(), false), None => (StandardType::NodeStart, false), }; let array_mask = if is_array { ARRAY_MASK } else { 0 }; @@ -329,15 +329,13 @@ impl Writeable for Node { node_buf .write_u8(node_type as u8 | array_mask) - .context(DataWriteSnafu { - node_type: node_type, - })?; + .context(DataWriteSnafu { node_type })?; match options.compression { CompressionType::Compressed => { - Sixbit::pack(&mut **node_buf, &self.key()).context(NodeSixbitNameSnafu)? + Sixbit::pack(&mut **node_buf, self.key()).context(NodeSixbitNameSnafu)? }, CompressionType::Uncompressed => { - let data = options.encoding.encode_bytes(&self.key()).context( + let data = options.encoding.encode_bytes(self.key()).context( NodeUncompressedNameEncodeSnafu { encoding: options.encoding, }, @@ -371,10 +369,10 @@ impl Writeable for Node { match options.compression { CompressionType::Compressed => { - Sixbit::pack(&mut **node_buf, &key).context(NodeSixbitNameSnafu)? + Sixbit::pack(&mut **node_buf, key).context(NodeSixbitNameSnafu)? }, CompressionType::Uncompressed => { - let data = options.encoding.encode_bytes(&key).context( + let data = options.encoding.encode_bytes(key).context( NodeUncompressedNameEncodeSnafu { encoding: options.encoding, }, @@ -475,3 +473,10 @@ impl Writer { Ok(output) } } + +impl Default for Writer { + #[inline] + fn default() -> Self { + Self::new() + } +} diff --git a/kbinxml_cli/src/main.rs b/kbinxml_cli/src/main.rs index 5385852..290fc89 100644 --- a/kbinxml_cli/src/main.rs +++ b/kbinxml_cli/src/main.rs @@ -8,7 +8,7 @@ use encoding_rs::Encoding; use kbinxml::{EncodingType, Options, Printer}; fn display_buf(buf: &[u8]) -> Result<(), IoError> { - io::stdout().write_all(&buf)?; + io::stdout().write_all(buf)?; println!(); Ok(()) diff --git a/psmap_derive/src/lib.rs b/psmap_derive/src/lib.rs index 790ea73..eb70611 100644 --- a/psmap_derive/src/lib.rs +++ b/psmap_derive/src/lib.rs @@ -495,8 +495,9 @@ pub fn psmap(input: TokenStream) -> TokenStream { }; let mut loops = TokenStream2::new(); + for InputBlock { name, mappings } in blocks.iter() { - loops.append_all(output.create_input_loop(&name, mappings.iter())); + loops.append_all(output.create_input_loop(name, mappings.iter())); } let definitions = output.definitions;