cargo: clippy

This commit is contained in:
Felix
2022-02-02 23:45:14 +00:00
parent 4122e12206
commit c14690f225
14 changed files with 91 additions and 81 deletions

View File

@@ -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 {

View File

@@ -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),
}
}

View File

@@ -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::<Option<_>>().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)
}

View File

@@ -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<I>(iter: &mut I) -> Option<NodeCollection>
where
I: Iterator<Item = NodeDefinition>,
{
let base = if let Some(def) = iter.next() {
def
} else {
return None;
};
NodeCollection::from_iter_base(base, iter)
}
fn from_iter_base<I>(base: NodeDefinition, iter: &mut I) -> Option<NodeCollection>
where
I: Iterator<Item = NodeDefinition>,
@@ -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<NodeDefinition> for Option<NodeCollection> {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = NodeDefinition>,
{
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

View File

@@ -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,

View File

@@ -53,9 +53,10 @@ impl Printer {
};
}
let collection = NodeCollection::from_iter(&mut definitions.into_iter());
let collection: Option<NodeCollection> = definitions.into_iter().collect();
match collection {
Some(ref collection) => eprintln!("collection: {:#}", collection),
Some(collection) => eprintln!("collection: {:#}", collection),
None => eprintln!("collection: {:?}", collection),
};

View File

@@ -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<u8, u8> = {

View File

@@ -26,7 +26,7 @@ impl TextXmlWriter {
Self { xml_writer }
}
pub fn to_text_xml<T>(mut self, value: &T) -> Result<Vec<u8>, KbinError>
pub fn into_text_xml<T>(mut self, value: &T) -> Result<Vec<u8>, KbinError>
where
T: ToTextXml,
{

View File

@@ -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(),

View File

@@ -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<Self> {
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

View File

@@ -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<T: fmt::Display>(f: &mut fmt::Formatter, values: &[T]) -> fmt::R
macro_rules! write_array {
($method:ident, $num:expr) => {
fn $method<T: fmt::Display>(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(" ")?;
}

View File

@@ -132,7 +132,7 @@ fn write_value(
.write_u32::<BigEndian>(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()
}
}

View File

@@ -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(())

View File

@@ -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;