Skip to content

Commit

Permalink
fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoeyR committed Jun 26, 2020
1 parent d74c038 commit 8c2ba38
Show file tree
Hide file tree
Showing 18 changed files with 207 additions and 153 deletions.
22 changes: 13 additions & 9 deletions dotenv/src/bin/dotenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate dotenv;

use clap::{App, AppSettings, Arg};
use std::os::unix::process::CommandExt;
use std::process::{Command, exit};
use std::process::{exit, Command};

macro_rules! die {
($fmt:expr) => ({
Expand Down Expand Up @@ -33,26 +33,30 @@ fn main() {
.setting(AppSettings::AllowExternalSubcommands)
.setting(AppSettings::ArgRequiredElseHelp)
.setting(AppSettings::UnifiedHelpMessage)
.arg(Arg::with_name("FILE")
.short("f")
.long("file")
.takes_value(true)
.help("Use a specific .env file (defaults to .env)"))
.arg(
Arg::with_name("FILE")
.short("f")
.long("file")
.takes_value(true)
.help("Use a specific .env file (defaults to .env)"),
)
.get_matches();

match matches.value_of("FILE") {
None => dotenv::dotenv(),
Some(file) => dotenv::from_filename(file),
}.unwrap_or_else(|e| die!("error: failed to load environment: {}", e));
}
.unwrap_or_else(|e| die!("error: failed to load environment: {}", e));

let mut command = match matches.subcommand() {
(name, Some(matches)) => {
let args = matches.values_of("")
let args = matches
.values_of("")
.map(|v| v.collect())
.unwrap_or(Vec::new());

make_command(name, args)
},
}
_ => die!("error: missing required argument <COMMAND>"),
};

Expand Down
31 changes: 23 additions & 8 deletions dotenv/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;
use std::fmt;
use std::error;
use std::fmt;
use std::io;

pub type Result<T> = std::result::Result<T, Error>;

Expand All @@ -10,7 +10,7 @@ pub enum Error {
Io(io::Error),
EnvVar(std::env::VarError),
#[doc(hidden)]
__Nonexhaustive
__Nonexhaustive,
}

impl Error {
Expand All @@ -37,7 +37,11 @@ impl fmt::Display for Error {
match self {
Error::Io(err) => write!(fmt, "{}", err),
Error::EnvVar(err) => write!(fmt, "{}", err),
Error::LineParse(line, error_index) => write!(fmt, "Error parsing line: '{}', error at line index: {}", line, error_index),
Error::LineParse(line, error_index) => write!(
fmt,
"Error parsing line: '{}', error at line index: {}",
line, error_index
),
_ => unreachable!(),
}
}
Expand All @@ -52,14 +56,22 @@ mod test {
#[test]
fn test_io_error_source() {
let err = Error::Io(std::io::ErrorKind::PermissionDenied.into());
let io_err = err.source().unwrap().downcast_ref::<std::io::Error>().unwrap();
let io_err = err
.source()
.unwrap()
.downcast_ref::<std::io::Error>()
.unwrap();
assert_eq!(std::io::ErrorKind::PermissionDenied, io_err.kind());
}

#[test]
fn test_envvar_error_source() {
let err = Error::EnvVar(std::env::VarError::NotPresent);
let var_err = err.source().unwrap().downcast_ref::<std::env::VarError>().unwrap();
let var_err = err
.source()
.unwrap()
.downcast_ref::<std::env::VarError>()
.unwrap();
assert_eq!(&std::env::VarError::NotPresent, var_err);
}

Expand Down Expand Up @@ -105,6 +117,9 @@ mod test {
fn test_lineparse_error_display() {
let err = Error::LineParse("test line".to_string(), 2);
let err_desc = format!("{}", err);
assert_eq!("Error parsing line: 'test line', error at line index: 2", err_desc);
assert_eq!(
"Error parsing line: 'test line', error at line index: 2",
err_desc
);
}
}
}
15 changes: 10 additions & 5 deletions dotenv/src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::errors::*;
use crate::iter::Iter;

pub struct Finder<'a> {
filename: &'a Path,
filename: &'a Path,
}

impl<'a> Finder<'a> {
Expand Down Expand Up @@ -34,9 +34,11 @@ pub fn find(directory: &Path, filename: &Path) -> Result<PathBuf> {
let candidate = directory.join(filename);

match fs::metadata(&candidate) {
Ok(metadata) => if metadata.is_file() {
return Ok(candidate);
},
Ok(metadata) => {
if metadata.is_file() {
return Ok(candidate);
}
}
Err(error) => {
if error.kind() != io::ErrorKind::NotFound {
return Err(Error::Io(error));
Expand All @@ -47,6 +49,9 @@ pub fn find(directory: &Path, filename: &Path) -> Result<PathBuf> {
if let Some(parent) = directory.parent() {
find(parent, filename)
} else {
Err(Error::Io(io::Error::new(io::ErrorKind::NotFound, "path not found")))
Err(Error::Io(io::Error::new(
io::ErrorKind::NotFound,
"path not found",
)))
}
}
2 changes: 1 addition & 1 deletion dotenv/src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::env;
use std::io::{BufReader, Lines};
use std::io::prelude::*;
use std::io::{BufReader, Lines};

use crate::errors::*;
use crate::parse;
Expand Down
10 changes: 5 additions & 5 deletions dotenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
//! file, if available, and mashes those with the actual environment variables
//! provided by the operating system.

mod parse;
mod errors;
mod iter;
mod find;
mod iter;
mod parse;

use std::env::{self, Vars};
use std::ffi::OsStr;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::{Once};
use std::sync::Once;

pub use crate::errors::*;
use crate::iter::Iter;
use crate::find::Finder;
use crate::iter::Iter;

static START: Once = Once::new();

Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String> {
/// The returned iterator contains a snapshot of the process's environment variables at the
/// time of this invocation, modifications to environment variables afterwards will not be
/// reflected in the returned iterator.
///
///
/// Examples:
///
/// ```no_run
Expand Down
Loading

0 comments on commit 8c2ba38

Please sign in to comment.