Skip to content

Commit 9ab8311

Browse files
committed
*: better import grouping and short hand struct init
1 parent ea0273c commit 9ab8311

File tree

8 files changed

+177
-182
lines changed

8 files changed

+177
-182
lines changed

src/byte_record.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
use std::cmp;
2-
use std::fmt;
3-
use std::iter::FromIterator;
4-
use std::ops::{self, Range};
5-
use std::result;
6-
7-
use bstr::{BString, ByteSlice};
8-
use serde::de::Deserialize;
9-
10-
use crate::deserializer::deserialize_byte_record;
11-
use crate::error::{new_utf8_error, Result, Utf8Error};
12-
use crate::string_record::StringRecord;
1+
use std::{
2+
cmp, fmt,
3+
iter::FromIterator,
4+
ops::{self, Range},
5+
result,
6+
};
7+
8+
use {
9+
bstr::{BString, ByteSlice},
10+
serde::de::Deserialize,
11+
};
12+
13+
use crate::{
14+
deserializer::deserialize_byte_record,
15+
error::{new_utf8_error, Result, Utf8Error},
16+
string_record::StringRecord,
17+
};
1318

1419
/// A single CSV record stored as raw bytes.
1520
///
@@ -681,7 +686,7 @@ impl Bounds {
681686
None => 0,
682687
Some(&start) => start,
683688
};
684-
Some(ops::Range { start: start, end: end })
689+
Some(ops::Range { start, end })
685690
}
686691

687692
/// Returns a slice of ending positions of all fields.

src/deserializer.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
use std::error::Error as StdError;
2-
use std::fmt;
3-
use std::iter;
4-
use std::num;
5-
use std::str;
6-
7-
use serde::de::value::BorrowedBytesDeserializer;
8-
use serde::de::{
9-
Deserialize, DeserializeSeed, Deserializer, EnumAccess,
10-
Error as SerdeError, IntoDeserializer, MapAccess, SeqAccess, Unexpected,
11-
VariantAccess, Visitor,
1+
use std::{error::Error as StdError, fmt, iter, num, str};
2+
3+
use serde::{
4+
de::value::BorrowedBytesDeserializer,
5+
de::{
6+
Deserialize, DeserializeSeed, Deserializer, EnumAccess,
7+
Error as SerdeError, IntoDeserializer, MapAccess, SeqAccess,
8+
Unexpected, VariantAccess, Visitor,
9+
},
10+
serde_if_integer128,
1211
};
13-
use serde::serde_if_integer128;
1412

15-
use crate::byte_record::{ByteRecord, ByteRecordIter};
16-
use crate::error::{Error, ErrorKind};
17-
use crate::string_record::{StringRecord, StringRecordIter};
13+
use crate::{
14+
byte_record::{ByteRecord, ByteRecordIter},
15+
error::{Error, ErrorKind},
16+
string_record::{StringRecord, StringRecordIter},
17+
};
1818

1919
use self::DeserializeErrorKind as DEK;
2020

@@ -795,13 +795,16 @@ fn try_float_bytes(s: &[u8]) -> Option<f64> {
795795
mod tests {
796796
use std::collections::HashMap;
797797

798-
use bstr::BString;
799-
use serde::{de::DeserializeOwned, serde_if_integer128, Deserialize};
798+
use {
799+
bstr::BString,
800+
serde::{de::DeserializeOwned, serde_if_integer128, Deserialize},
801+
};
802+
803+
use crate::{
804+
byte_record::ByteRecord, error::Error, string_record::StringRecord,
805+
};
800806

801807
use super::{deserialize_byte_record, deserialize_string_record};
802-
use crate::byte_record::ByteRecord;
803-
use crate::error::Error;
804-
use crate::string_record::StringRecord;
805808

806809
fn de<D: DeserializeOwned>(fields: &[&str]) -> Result<D, Error> {
807810
let record = StringRecord::from(fields);

src/error.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::error::Error as StdError;
2-
use std::fmt;
3-
use std::io;
4-
use std::result;
1+
use std::{error::Error as StdError, fmt, io, result};
52

6-
use crate::byte_record::{ByteRecord, Position};
7-
use crate::deserializer::DeserializeError;
3+
use crate::{
4+
byte_record::{ByteRecord, Position},
5+
deserializer::DeserializeError,
6+
};
87

98
/// A type alias for `Result<T, csv::Error>`.
109
pub type Result<T> = result::Result<T, Error>;
@@ -227,8 +226,8 @@ pub struct FromUtf8Error {
227226

228227
impl FromUtf8Error {
229228
/// Create a new FromUtf8Error.
230-
pub(crate) fn new(rec: ByteRecord, err: Utf8Error) -> FromUtf8Error {
231-
FromUtf8Error { record: rec, err: err }
229+
pub(crate) fn new(record: ByteRecord, err: Utf8Error) -> FromUtf8Error {
230+
FromUtf8Error { record, err }
232231
}
233232

234233
/// Access the underlying `ByteRecord` that failed UTF-8 validation.
@@ -271,7 +270,7 @@ pub struct Utf8Error {
271270

272271
/// Create a new UTF-8 error.
273272
pub fn new_utf8_error(field: usize, valid_up_to: usize) -> Utf8Error {
274-
Utf8Error { field: field, valid_up_to: valid_up_to }
273+
Utf8Error { field, valid_up_to }
275274
}
276275

277276
impl Utf8Error {
@@ -315,7 +314,7 @@ impl<W> IntoInnerError<W> {
315314
/// (This is a visibility hack. It's public in this module, but not in the
316315
/// crate.)
317316
pub(crate) fn new(wtr: W, err: io::Error) -> IntoInnerError<W> {
318-
IntoInnerError { wtr: wtr, err: err }
317+
IntoInnerError { wtr, err }
319318
}
320319

321320
/// Returns the error which caused the call to `into_inner` to fail.

src/lib.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ stdout.
6565
There are more examples in the [cookbook](cookbook/index.html).
6666
6767
```no_run
68-
use std::error::Error;
69-
use std::io;
70-
use std::process;
68+
use std::{error::Error, io, process};
7169
7270
fn example() -> Result<(), Box<dyn Error>> {
7371
// Build the CSV reader and iterate over each record.
@@ -104,13 +102,9 @@ By default, the member names of the struct are matched with the values in the
104102
header record of your CSV data.
105103
106104
```no_run
107-
use std::error::Error;
108-
use std::io;
109-
use std::process;
105+
use std::{error::Error, io, process};
110106
111-
use serde::Deserialize;
112-
113-
#[derive(Debug, Deserialize)]
107+
#[derive(Debug, serde::Deserialize)]
114108
struct Record {
115109
city: String,
116110
region: String,
@@ -153,18 +147,20 @@ use std::result;
153147

154148
use serde::{Deserialize, Deserializer};
155149

156-
pub use crate::byte_record::{ByteRecord, ByteRecordIter, Position};
157-
pub use crate::deserializer::{DeserializeError, DeserializeErrorKind};
158-
pub use crate::error::{
159-
Error, ErrorKind, FromUtf8Error, IntoInnerError, Result, Utf8Error,
160-
};
161-
pub use crate::reader::{
162-
ByteRecordsIntoIter, ByteRecordsIter, DeserializeRecordsIntoIter,
163-
DeserializeRecordsIter, Reader, ReaderBuilder, StringRecordsIntoIter,
164-
StringRecordsIter,
150+
pub use crate::{
151+
byte_record::{ByteRecord, ByteRecordIter, Position},
152+
deserializer::{DeserializeError, DeserializeErrorKind},
153+
error::{
154+
Error, ErrorKind, FromUtf8Error, IntoInnerError, Result, Utf8Error,
155+
},
156+
reader::{
157+
ByteRecordsIntoIter, ByteRecordsIter, DeserializeRecordsIntoIter,
158+
DeserializeRecordsIter, Reader, ReaderBuilder, StringRecordsIntoIter,
159+
StringRecordsIter,
160+
},
161+
string_record::{StringRecord, StringRecordIter},
162+
writer::{Writer, WriterBuilder},
165163
};
166-
pub use crate::string_record::{StringRecord, StringRecordIter};
167-
pub use crate::writer::{Writer, WriterBuilder};
168164

169165
mod byte_record;
170166
pub mod cookbook;
@@ -321,10 +317,7 @@ impl Default for Trim {
321317
/// ```
322318
/// use std::error::Error;
323319
///
324-
/// use csv::Reader;
325-
/// use serde::Deserialize;
326-
///
327-
/// #[derive(Debug, Deserialize, Eq, PartialEq)]
320+
/// #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
328321
/// struct Row {
329322
/// #[serde(deserialize_with = "csv::invalid_option")]
330323
/// a: Option<i32>,
@@ -340,7 +333,7 @@ impl Default for Trim {
340333
/// a,b,c
341334
/// 5,\"\",xyz
342335
/// ";
343-
/// let mut rdr = Reader::from_reader(data.as_bytes());
336+
/// let mut rdr = csv::Reader::from_reader(data.as_bytes());
344337
/// if let Some(result) = rdr.deserialize().next() {
345338
/// let record: Row = result?;
346339
/// assert_eq!(record, Row { a: Some(5), b: None, c: None });

0 commit comments

Comments
 (0)