clippy fixes

This commit is contained in:
Robin Appelman 2023-12-15 18:30:16 +01:00
commit cffc2672ab
6 changed files with 40 additions and 35 deletions

View file

@ -2,6 +2,7 @@
name = "vdf-reader"
version = "0.1.0"
edition = "2021"
rust-version = "1.65.0"
[dependencies]
logos = "0.13.0"

View file

@ -12,9 +12,9 @@ impl From<Entry> for Array {
}
}
impl Into<Entry> for Array {
fn into(self) -> Entry {
Entry::Array(self)
impl From<Array> for Entry {
fn from(array: Array) -> Self {
Entry::Array(array)
}
}

View file

@ -44,9 +44,9 @@ impl Entry {
/// Try to get the named entry.
pub fn get<S: AsRef<str>>(&self, name: S) -> Option<&Entry> {
match self {
&Entry::Table(ref value) => value.get(name.as_ref()),
Entry::Table(value) => value.get(name.as_ref()),
&Entry::Array(ref value) => name
Entry::Array(value) => name
.as_ref()
.parse::<usize>()
.ok()
@ -58,7 +58,7 @@ impl Entry {
/// Try to convert the entry to the given type.
pub fn to<T: Parse>(&self) -> Option<T> {
if let &Entry::Value(ref value) = self {
if let Entry::Value(value) = self {
value.to::<T>()
} else {
None
@ -67,7 +67,7 @@ impl Entry {
/// Try to take the entry as a table.
pub fn as_table(&self) -> Option<&Table> {
if let &Entry::Table(ref value) = self {
if let Entry::Table(value) = self {
Some(value)
} else {
None
@ -76,7 +76,7 @@ impl Entry {
/// Try to take the entry as a slice.
pub fn as_slice(&self) -> Option<&[Entry]> {
if let &Entry::Array(ref value) = self {
if let Entry::Array(value) = self {
Some(value.as_slice())
} else {
unsafe { Some(slice::from_raw_parts(self, 1)) }
@ -85,7 +85,7 @@ impl Entry {
/// Try to take the entry as a statement.
pub fn as_statement(&self) -> Option<&Statement> {
if let &Entry::Statement(ref value) = self {
if let Entry::Statement(value) = self {
Some(value)
} else {
None
@ -94,7 +94,7 @@ impl Entry {
/// Try to take the entry as a value.
pub fn as_value(&self) -> Option<&Value> {
if let &Entry::Value(ref value) = self {
if let Entry::Value(value) = self {
Some(value)
} else {
None
@ -104,9 +104,9 @@ impl Entry {
/// Try to take the entry as a string.
pub fn as_str(&self) -> Option<&str> {
match self {
&Entry::Value(ref value) => Some(&*value),
Entry::Value(value) => Some(value),
&Entry::Statement(ref value) => Some(&*value),
Entry::Statement(value) => Some(value),
_ => None,
}

View file

@ -13,9 +13,9 @@ impl From<Cow<'_, str>> for Statement {
}
}
impl Into<Entry> for Statement {
fn into(self) -> Entry {
Entry::Statement(self)
impl From<Statement> for Entry {
fn from(statement: Statement) -> Self {
Entry::Statement(statement)
}
}

View file

@ -2,6 +2,7 @@ use super::{Array, Entry};
use crate::entry::{Statement, Value};
use crate::{Event, Item, Reader, Result};
use serde::{Serialize, Serializer};
use std::collections::hash_map;
use std::collections::HashMap;
use std::ops::Deref;
@ -24,20 +25,23 @@ where
fn insert<K: Into<String>, V: Into<Entry>>(map: &mut HashMap<String, Entry>, key: K, value: V) {
let key = key.into();
let value = value.into();
if !map.contains_key(&key) {
map.insert(key, value);
return;
let entry = map.entry(key);
match entry {
hash_map::Entry::Vacant(entry) => {
entry.insert(value);
}
if let Some(&mut Entry::Array(ref mut array)) = map.get_mut(&key) {
hash_map::Entry::Occupied(mut entry) => match entry.get_mut() {
Entry::Array(ref mut array) => {
array.push(value);
return;
}
let mut array = Array::from(map.remove(&key).unwrap());
_ => {
let (key, old_value) = entry.remove_entry();
let mut array = Array::from(old_value);
array.push(value);
map.insert(key, array.into());
map.insert(key, Entry::Array(array));
}
},
}
}
impl Table {
@ -65,13 +69,13 @@ impl Table {
}
}
return Ok(Table(map));
Ok(Table(map))
}
}
impl Into<Entry> for Table {
fn into(self) -> Entry {
Entry::Table(self)
impl From<Table> for Entry {
fn from(table: Table) -> Self {
Entry::Table(table)
}
}

View file

@ -12,9 +12,9 @@ impl From<Cow<'_, str>> for Value {
}
}
impl Into<Entry> for Value {
fn into(self) -> Entry {
Entry::Value(self)
impl From<Value> for Entry {
fn from(value: Value) -> Self {
Entry::Value(value)
}
}