Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Clippy warnings #9

Merged
merged 1 commit into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/fmtstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_write_char() {
assert!(s == "h fff");
}

fn write_from<'a, I>(fmt: &mut Formatter, f: I, n: usize) -> usize
fn write_from<I>(fmt: &mut Formatter, f: I, n: usize) -> usize
where I: Iterator<Item = char>
{
// eexaust f or run out of n, return chars written
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<'a, 'b> Formatter<'a, 'b> {
/// This gives a moderate performance boost.
/// This isn't exactly unsafe, it just ends up ignoring extranious format
/// specifiers
// For example, {x:<-#10} should technically be formatting an int, but ignoring the
/// For example, {x:<-#10} should technically be formatting an int, but ignoring the
/// integer specific formatting is probably not the end of the world
/// This can also be used by the `u64` etc methods to finish their formatting while
/// still using the str formatter for width and alignment
Expand All @@ -111,27 +111,24 @@ impl<'a, 'b> Formatter<'a, 'b> {

let mut chars = s.chars();
let mut pad: usize = 0;
match width {
Some(mut width) => {
if width > len {
let align = self.align();
match align {
Alignment::Left => pad = width - len,
Alignment::Center => {
width = width - len;
pad = width / 2;
write_char(self, fill, pad);
pad += width % 2;
}
Alignment::Right => {
write_char(self, fill, width - len);
}
Alignment::Equal => return Err(FmtError::Invalid(
"sign aware zero padding and Align '=' not yet supported".to_string())),
if let Some(mut width) = width {
if width > len {
let align = self.align();
match align {
Alignment::Left => pad = width - len,
Alignment::Center => {
width -= len;
pad = width / 2;
write_char(self, fill, pad);
pad += width % 2;
}
Alignment::Right => {
write_char(self, fill, width - len);
}
Alignment::Equal => return Err(FmtError::Invalid(
"sign aware zero padding and Align '=' not yet supported".to_string())),
}
}
None => {}
}
write_from(self, &mut chars, len);
write_char(self, fill, pad);
Expand Down
12 changes: 2 additions & 10 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,21 +364,13 @@ impl<'a, 'b> Formatter<'a, 'b> {
/// sign plus getter
/// here because it is in fmt::Formatter
pub fn sign_plus(&self) -> bool {
if self.sign == Sign::Plus {
true
} else {
false
}
self.sign == Sign::Plus
}

/// sign minus getter
/// here because it is in fmt::Formatter
pub fn sign_minus(&self) -> bool {
if self.sign == Sign::Plus {
true
} else {
false
}
self.sign == Sign::Minus
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy and paste of code is evil™.

}

/// alternate getter
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use fmtstr::strfmt_map;
pub use formatter::Formatter;


/// rust-style format a string given a HashMap of the variables
/// Rust-style format a string given a `HashMap` of the variables.
pub fn strfmt<T: fmt::Display>(fmtstr: &str, vars: &HashMap<String, T>) -> Result<String> {
let formatter = |mut fmt: Formatter| {
let v = match vars.get(fmt.key) {
Expand Down
21 changes: 10 additions & 11 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;
use std::error;
use std::fmt::Write;
use std::string::String;
use std::result;

Expand All @@ -23,8 +22,8 @@ pub enum Sign {

impl Sign {
pub fn is_unspecified(&self) -> bool {
match self {
&Sign::Unspecified => false,
match *self {
Sign::Unspecified => false,
_ => true,
}
}
Expand All @@ -42,20 +41,20 @@ pub enum FmtError {

impl fmt::Display for FmtError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&FmtError::Invalid(ref s) => write!(f, "Invalid({})", s),
&FmtError::KeyError(ref s) => write!(f, "KeyError({})", s),
&FmtError::TypeError(ref s) => write!(f, "TypeError({})", s),
match *self {
FmtError::Invalid(ref s) => write!(f, "Invalid({})", s),
FmtError::KeyError(ref s) => write!(f, "KeyError({})", s),
FmtError::TypeError(ref s) => write!(f, "TypeError({})", s),
}
}
}

impl error::Error for FmtError {
fn description(&self) -> &str {
match self {
&FmtError::Invalid(_) => "invalid format string",
&FmtError::KeyError(_) => "invalid key",
&FmtError::TypeError(_) => "error during type resolution",
match *self {
FmtError::Invalid(_) => "invalid format string",
FmtError::KeyError(_) => "invalid key",
FmtError::TypeError(_) => "error during type resolution",
}
}

Expand Down