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" name = "vdf-reader"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
rust-version = "1.65.0"
[dependencies] [dependencies]
logos = "0.13.0" logos = "0.13.0"

View file

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

View file

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

View file

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

View file

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

View file

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