Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeam committed Jan 27, 2021
1 parent f6ae5ab commit e626775
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 61 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ debug = true
opt-level = 3
debug = true

[profile.doc]
opt-level = 0
debug = false

[features]
default = []
nightly = []
2 changes: 1 addition & 1 deletion src/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Directory {
return Err(Error::UnexpectedEofInDirectory);
}
}
Ok(Directory { entries: entries })
Ok(Directory { entries })
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/field/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ pub struct Fields<'a> {
impl<'a> Fields<'a> {
#[doc(hidden)]
pub fn new(record: &'a Record<'a>) -> Fields<'a> {
Fields {
record: record,
offset: 0,
}
Fields { record, offset: 0 }
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ pub struct Field<'a> {

impl<'a> Field<'a> {
#[doc(hidden)]
pub fn new<'x>(tag: Tag, data: &'x [u8]) -> Field<'x> {
Field {
tag: tag,
data: data,
}
pub fn new(tag: Tag, data: &[u8]) -> Field<'_> {
Field { tag, data }
}

/// Will find all subfields with identifier `ident`.
pub fn subfield<'r, Ident: Into<Identifier>>(&'r self, ident: Ident) -> Vec<Subfield<'r>> {
pub fn subfield<Ident: Into<Identifier>>(&self, ident: Ident) -> Vec<Subfield<'_>> {
Subfield::find(self, ident)
}

Expand Down
2 changes: 1 addition & 1 deletion src/field/subfield/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a> Subfield<'a> {
output.push(sf);
}
}
return output;
output
}

/// Returns tag of a field this subfield belongs to.
Expand Down
6 changes: 3 additions & 3 deletions src/field/subfield/subfields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a> Iterator for Subfields<'a> {
self.state = State::Start(2);
self.next()
}
State::Start(offset) => match self.field.data.get(offset).map(|x| *x) {
State::Start(offset) => match self.field.data.get(offset).copied() {
Some(SUBFIELD_DELIMITER) => {
self.state = State::SubfieldStart(offset + 1);
self.next()
Expand All @@ -40,7 +40,7 @@ impl<'a> Iterator for Subfields<'a> {
}
},
State::SubfieldStart(offset) => {
match self.field.data.get(offset).map(|x| *x) {
match self.field.data.get(offset).copied() {
Some(SUBFIELD_DELIMITER) | None => {
// Subfield ends unexpectedly
self.state = State::Done;
Expand All @@ -53,7 +53,7 @@ impl<'a> Iterator for Subfields<'a> {
}
}
State::Subfield(identifier, start, offset) => {
match self.field.data.get(start + offset).map(|x| *x) {
match self.field.data.get(start + offset).copied() {
Some(SUBFIELD_DELIMITER) => {
self.state = State::SubfieldStart(start + offset + 1);
Some(Subfield {
Expand Down
6 changes: 3 additions & 3 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Identifier(pub u8);

impl Into<u8> for Identifier {
fn into(self) -> u8 {
self.0
impl From<Identifier> for u8 {
fn from(x: Identifier) -> Self {
x.0
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/indicator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Indicator(pub [u8; 2]);

impl Indicator {
pub fn as_ref(&self) -> &[u8] {
impl AsRef<[u8]> for Indicator {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
Expand Down
62 changes: 32 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<'a> Record<'a> {
/// Will try to parse record from a buffer.
///
/// Will borrow an `input` for the lifetime of a produced record.
pub fn parse<'x>(input: &'x [u8]) -> Result<Record<'x>> {
pub fn parse(input: &[u8]) -> Result<Record<'_>> {
let len = misc::read_dec_5(input)?;
if input.len() < len {
return Err(Error::UnexpectedEof);
Expand All @@ -84,8 +84,8 @@ impl<'a> Record<'a> {

Ok(Record {
data: Cow::Borrowed(data),
data_offset: data_offset,
directory: directory,
data_offset,
directory,
})
}

Expand All @@ -111,8 +111,8 @@ impl<'a> Record<'a> {

Ok(Record {
data: Cow::Owned(input),
data_offset: data_offset,
directory: directory,
data_offset,
directory,
})
}

Expand All @@ -122,9 +122,8 @@ impl<'a> Record<'a> {
pub fn read<T: io::Read>(input: &mut T) -> Result<Option<Record<'static>>> {
let mut data = vec![0; 5];

match input.read(&mut data[..1])? {
0 => return Ok(None),
_ => (),
if let 0 = input.read(&mut data[..1])? {
return Ok(None);
}

input.read_exact(&mut data[1..])?;
Expand All @@ -144,8 +143,8 @@ impl<'a> Record<'a> {

Ok(Some(Record {
data: Cow::Owned(data),
data_offset: data_offset,
directory: directory,
data_offset,
directory,
}))
}

Expand All @@ -163,15 +162,10 @@ impl<'a> Record<'a> {
}

/// Will return iterator over fields of a record
pub fn fields<'r>(&'r self) -> Fields<'r> {
pub fn fields(&self) -> Fields<'_> {
Fields::new(self)
}

/// View into a data of a record.
pub fn as_ref(&self) -> &[u8] {
self.data.borrow()
}

get!(RecordStatus, record_status, 5);
get!(TypeOfRecord, type_of_record, 6);
get!(BibliographicLevel, bibliographic_level, 7);
Expand All @@ -186,6 +180,12 @@ impl<'a> Record<'a> {
);
}

impl AsRef<[u8]> for Record<'_> {
fn as_ref(&self) -> &[u8] {
self.data.borrow()
}
}

impl<'a> fmt::Display for Record<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(
Expand Down Expand Up @@ -291,11 +291,8 @@ impl RecordBuilder {
pub fn from_record(record: &Record) -> RecordBuilder {
let mut leader = [0; 24];
leader.copy_from_slice(&record.as_ref()[0..24]);
let fields = record.fields().map(|f| FieldRepr::from(f)).collect();
RecordBuilder {
leader: leader,
fields: fields,
}
let fields = record.fields().map(FieldRepr::from).collect();
RecordBuilder { leader, fields }
}

/// Iterator over fields of this builder.
Expand Down Expand Up @@ -385,7 +382,7 @@ impl RecordBuilder {
}

// writing record length
&mut data[0..5].copy_from_slice(format!("{:05}", size).as_bytes());
data[0..5].copy_from_slice(format!("{:05}", size).as_bytes());

// writing directory
let mut offset = 0;
Expand All @@ -400,7 +397,7 @@ impl RecordBuilder {

// writing base address of data
let len = data.len();
&mut data[12..17].copy_from_slice(format!("{:05}", len).as_bytes());
data[12..17].copy_from_slice(format!("{:05}", len).as_bytes());

// writing fields
for f in self.fields.iter() {
Expand All @@ -421,8 +418,8 @@ impl RecordBuilder {

Ok(Record {
data: Cow::Owned(data),
data_offset: data_offset,
directory: directory,
data_offset,
directory,
})
}

Expand Down Expand Up @@ -456,6 +453,12 @@ impl RecordBuilder {
);
}

impl Default for RecordBuilder {
fn default() -> Self {
Self::new()
}
}

macro_rules! leader_field(
($name:ident {
$($val:expr => $kind:ident,)+
Expand All @@ -476,10 +479,10 @@ macro_rules! leader_field(
}
}

impl Into<u8> for $name {
impl From<$name> for u8 {
#[inline]
fn into(self) -> u8 {
match self {
fn from(x: $name) -> u8 {
match x {
$($name::$kind => $val),+,
$name::Unknown(b) => b,
}
Expand Down Expand Up @@ -1070,7 +1073,6 @@ mod tests {

mod write {
use super::{super::*, RECS};
use std::error::Error;

#[test]
fn should_write_record() {
Expand All @@ -1079,7 +1081,7 @@ mod tests {
let record = Record::parse(&RECS.as_bytes()[..963]).unwrap();

match vec.write_record(record.clone()) {
Err(why) => panic!("couldn't write file: {}", why.description()),
Err(why) => panic!("couldn't write file: {}", why),
Ok(_) => (),
}

Expand Down
10 changes: 5 additions & 5 deletions src/misc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::errors::*;

pub fn read_dec_1(i: u8) -> Result<u8> {
if 0x30 > i || i > 0x39 {
if !(0x30..=0x39).contains(&i) {
Err(Error::UnexpectedByteInDecNum(i))
} else {
Ok(0x0F & i)
Expand All @@ -13,8 +13,8 @@ pub fn read_dec_4(i: &[u8]) -> Result<usize> {
return Err(Error::UnexpectedEofInDecNum);
}
let mut result = 0usize;
for x in 0..4usize {
result += 10usize.pow(3 - x as u32) * read_dec_1(i[x])? as usize;
for (x, byte) in i.iter().enumerate().take(4) {
result += 10_usize.pow(3 - x as u32) * read_dec_1(*byte)? as usize;
}
Ok(result)
}
Expand All @@ -24,8 +24,8 @@ pub fn read_dec_5(i: &[u8]) -> Result<usize> {
return Err(Error::UnexpectedEofInDecNum);
}
let mut result = 0usize;
for x in 0..5usize {
result += 10usize.pow(4 - x as u32) * read_dec_1(i[x])? as usize;
for (x, byte) in i.iter().enumerate().take(5) {
result += 10_usize.pow(4 - x as u32) * read_dec_1(*byte)? as usize;
}
Ok(result)
}
4 changes: 2 additions & 2 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{fmt, str};
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Tag(pub [u8; 3]);

impl Tag {
pub fn as_ref(&self) -> &[u8] {
impl AsRef<[u8]> for Tag {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
Expand Down

0 comments on commit e626775

Please sign in to comment.