Skip to content

Commit

Permalink
*: better import grouping and short hand struct init
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Feb 14, 2023
1 parent ea0273c commit 9ab8311
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 182 deletions.
31 changes: 18 additions & 13 deletions src/byte_record.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
use std::cmp;
use std::fmt;
use std::iter::FromIterator;
use std::ops::{self, Range};
use std::result;

use bstr::{BString, ByteSlice};
use serde::de::Deserialize;

use crate::deserializer::deserialize_byte_record;
use crate::error::{new_utf8_error, Result, Utf8Error};
use crate::string_record::StringRecord;
use std::{
cmp, fmt,
iter::FromIterator,
ops::{self, Range},
result,
};

use {
bstr::{BString, ByteSlice},
serde::de::Deserialize,
};

use crate::{
deserializer::deserialize_byte_record,
error::{new_utf8_error, Result, Utf8Error},
string_record::StringRecord,
};

/// A single CSV record stored as raw bytes.
///
Expand Down Expand Up @@ -681,7 +686,7 @@ impl Bounds {
None => 0,
Some(&start) => start,
};
Some(ops::Range { start: start, end: end })
Some(ops::Range { start, end })
}

/// Returns a slice of ending positions of all fields.
Expand Down
43 changes: 23 additions & 20 deletions src/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::error::Error as StdError;
use std::fmt;
use std::iter;
use std::num;
use std::str;

use serde::de::value::BorrowedBytesDeserializer;
use serde::de::{
Deserialize, DeserializeSeed, Deserializer, EnumAccess,
Error as SerdeError, IntoDeserializer, MapAccess, SeqAccess, Unexpected,
VariantAccess, Visitor,
use std::{error::Error as StdError, fmt, iter, num, str};

use serde::{
de::value::BorrowedBytesDeserializer,
de::{
Deserialize, DeserializeSeed, Deserializer, EnumAccess,
Error as SerdeError, IntoDeserializer, MapAccess, SeqAccess,
Unexpected, VariantAccess, Visitor,
},
serde_if_integer128,
};
use serde::serde_if_integer128;

use crate::byte_record::{ByteRecord, ByteRecordIter};
use crate::error::{Error, ErrorKind};
use crate::string_record::{StringRecord, StringRecordIter};
use crate::{
byte_record::{ByteRecord, ByteRecordIter},
error::{Error, ErrorKind},
string_record::{StringRecord, StringRecordIter},
};

use self::DeserializeErrorKind as DEK;

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

use bstr::BString;
use serde::{de::DeserializeOwned, serde_if_integer128, Deserialize};
use {
bstr::BString,
serde::{de::DeserializeOwned, serde_if_integer128, Deserialize},
};

use crate::{
byte_record::ByteRecord, error::Error, string_record::StringRecord,
};

use super::{deserialize_byte_record, deserialize_string_record};
use crate::byte_record::ByteRecord;
use crate::error::Error;
use crate::string_record::StringRecord;

fn de<D: DeserializeOwned>(fields: &[&str]) -> Result<D, Error> {
let record = StringRecord::from(fields);
Expand Down
19 changes: 9 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::result;
use std::{error::Error as StdError, fmt, io, result};

use crate::byte_record::{ByteRecord, Position};
use crate::deserializer::DeserializeError;
use crate::{
byte_record::{ByteRecord, Position},
deserializer::DeserializeError,
};

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

impl FromUtf8Error {
/// Create a new FromUtf8Error.
pub(crate) fn new(rec: ByteRecord, err: Utf8Error) -> FromUtf8Error {
FromUtf8Error { record: rec, err: err }
pub(crate) fn new(record: ByteRecord, err: Utf8Error) -> FromUtf8Error {
FromUtf8Error { record, err }
}

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

/// Create a new UTF-8 error.
pub fn new_utf8_error(field: usize, valid_up_to: usize) -> Utf8Error {
Utf8Error { field: field, valid_up_to: valid_up_to }
Utf8Error { field, valid_up_to }
}

impl Utf8Error {
Expand Down Expand Up @@ -315,7 +314,7 @@ impl<W> IntoInnerError<W> {
/// (This is a visibility hack. It's public in this module, but not in the
/// crate.)
pub(crate) fn new(wtr: W, err: io::Error) -> IntoInnerError<W> {
IntoInnerError { wtr: wtr, err: err }
IntoInnerError { wtr, err }
}

/// Returns the error which caused the call to `into_inner` to fail.
Expand Down
43 changes: 18 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ stdout.
There are more examples in the [cookbook](cookbook/index.html).
```no_run
use std::error::Error;
use std::io;
use std::process;
use std::{error::Error, io, process};
fn example() -> Result<(), Box<dyn Error>> {
// Build the CSV reader and iterate over each record.
Expand Down Expand Up @@ -104,13 +102,9 @@ By default, the member names of the struct are matched with the values in the
header record of your CSV data.
```no_run
use std::error::Error;
use std::io;
use std::process;
use std::{error::Error, io, process};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
#[derive(Debug, serde::Deserialize)]
struct Record {
city: String,
region: String,
Expand Down Expand Up @@ -153,18 +147,20 @@ use std::result;

use serde::{Deserialize, Deserializer};

pub use crate::byte_record::{ByteRecord, ByteRecordIter, Position};
pub use crate::deserializer::{DeserializeError, DeserializeErrorKind};
pub use crate::error::{
Error, ErrorKind, FromUtf8Error, IntoInnerError, Result, Utf8Error,
};
pub use crate::reader::{
ByteRecordsIntoIter, ByteRecordsIter, DeserializeRecordsIntoIter,
DeserializeRecordsIter, Reader, ReaderBuilder, StringRecordsIntoIter,
StringRecordsIter,
pub use crate::{
byte_record::{ByteRecord, ByteRecordIter, Position},
deserializer::{DeserializeError, DeserializeErrorKind},
error::{
Error, ErrorKind, FromUtf8Error, IntoInnerError, Result, Utf8Error,
},
reader::{
ByteRecordsIntoIter, ByteRecordsIter, DeserializeRecordsIntoIter,
DeserializeRecordsIter, Reader, ReaderBuilder, StringRecordsIntoIter,
StringRecordsIter,
},
string_record::{StringRecord, StringRecordIter},
writer::{Writer, WriterBuilder},
};
pub use crate::string_record::{StringRecord, StringRecordIter};
pub use crate::writer::{Writer, WriterBuilder};

mod byte_record;
pub mod cookbook;
Expand Down Expand Up @@ -321,10 +317,7 @@ impl Default for Trim {
/// ```
/// use std::error::Error;
///
/// use csv::Reader;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize, Eq, PartialEq)]
/// #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
/// struct Row {
/// #[serde(deserialize_with = "csv::invalid_option")]
/// a: Option<i32>,
Expand All @@ -340,7 +333,7 @@ impl Default for Trim {
/// a,b,c
/// 5,\"\",xyz
/// ";
/// let mut rdr = Reader::from_reader(data.as_bytes());
/// let mut rdr = csv::Reader::from_reader(data.as_bytes());
/// if let Some(result) = rdr.deserialize().next() {
/// let record: Row = result?;
/// assert_eq!(record, Row { a: Some(5), b: None, c: None });
Expand Down

0 comments on commit 9ab8311

Please sign in to comment.