Skip to content

Commit

Permalink
Introduce Clippy pedantic and nursery
Browse files Browse the repository at this point in the history
This commit adds new Clippy directives.
Most lints have been applied, mostly affecting formatting or control
flow.

`find` function was skipped because it may be replaced with a
non-recursive version (#80).
  • Loading branch information
allan2 committed Mar 15, 2024
1 parent 323b351 commit bbb06cd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
3 changes: 2 additions & 1 deletion dotenv/Cargo.toml
Expand Up @@ -9,12 +9,13 @@ authors = [
"Sean Griffin <sean@seantheprogrammer.com>",
"Adam Sharp <adam@sharplet.me>",
"Arpad Borsos <arpad.borsos@googlemail.com>",
"Allan Zhang <al@ayz.ai>",
"Allan Zhang <allanzhang7@gmail.com>",
]
description = "A well-maintained fork of the dotenv crate"
homepage = "https://github.com/allan2/dotenvy"
readme = "README.md"
keywords = ["dotenv", "env", "environment", "settings", "config"]
categories = ["configuration"]
license = "MIT"
repository = "https://github.com/allan2/dotenvy"
edition = "2018"
Expand Down
23 changes: 9 additions & 14 deletions dotenv/src/errors.rs
@@ -1,7 +1,4 @@
use std::env;
use std::error;
use std::fmt;
use std::io;
use std::{env, error, fmt, io};

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

Expand All @@ -14,8 +11,9 @@ pub enum Error {
}

impl Error {
#[must_use]
pub fn not_found(&self) -> bool {
if let Error::Io(ref io_error) = *self {
if let Self::Io(ref io_error) = *self {
return io_error.kind() == io::ErrorKind::NotFound;
}
false
Expand All @@ -25,8 +23,8 @@ impl Error {
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
Error::EnvVar(err) => Some(err),
Self::Io(err) => Some(err),
Self::EnvVar(err) => Some(err),
_ => None,
}
}
Expand All @@ -35,22 +33,19 @@ impl error::Error for Error {
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Io(err) => write!(fmt, "{}", err),
Error::EnvVar(err) => write!(fmt, "{}", err),
Error::LineParse(line, error_index) => write!(
Self::Io(e) => write!(fmt, "{e}"),
Self::EnvVar(e) => write!(fmt, "{e}"),
Self::LineParse(line, index) => write!(
fmt,
"Error parsing line: '{}', error at line index: {}",
line, error_index
"Error parsing line: '{line}', error at line index: {index}",
),
}
}
}

#[cfg(test)]
mod test {
use std::env;
use std::error::Error as StdError;
use std::io;

use super::*;

Expand Down
15 changes: 9 additions & 6 deletions dotenv/src/find.rs
@@ -1,8 +1,11 @@
use std::fs::File;
use std::path::{Path, PathBuf};
use std::{env, fs, io};

use crate::errors::*;
use std::{
env,
fs::{self, File},
io,
path::{Path, PathBuf},
};

use crate::errors::{Error, Result};
use crate::iter::Iter;

pub struct Finder<'a> {
Expand All @@ -16,7 +19,7 @@ impl<'a> Finder<'a> {
}
}

pub fn filename(mut self, filename: &'a Path) -> Self {
pub const fn filename(mut self, filename: &'a Path) -> Self {
self.filename = filename;
self
}
Expand Down
32 changes: 17 additions & 15 deletions dotenv/src/iter.rs
@@ -1,19 +1,21 @@
use std::collections::HashMap;
use std::env;
use std::io::prelude::*;
use std::io::BufReader;

use crate::errors::*;
use crate::parse;
use crate::{
errors::{Error, Result},
parse,
};
use std::{
collections::HashMap,
env,
io::{BufRead, BufReader, Read},
};

pub struct Iter<R> {
lines: QuotedLines<BufReader<R>>,
substitution_data: HashMap<String, Option<String>>,
}

impl<R: Read> Iter<R> {
pub fn new(reader: R) -> Iter<R> {
Iter {
pub fn new(reader: R) -> Self {
Self {
lines: QuotedLines {
buf: BufReader::new(reader),
},
Expand Down Expand Up @@ -133,13 +135,13 @@ impl<B: BufRead> Iterator for QuotedLines<B> {
loop {
buf_pos = buf.len();
match self.buf.read_line(&mut buf) {
Ok(0) => match cur_state {
ParseState::Complete => return None,
_ => {
let len = buf.len();
return Some(Err(Error::LineParse(buf, len)));
Ok(0) => {
if matches!(cur_state, ParseState::Complete) {
return None;
}
},
let len = buf.len();
return Some(Err(Error::LineParse(buf, len)));
}
Ok(_n) => {
// Skip lines which start with a # before iteration
// This optimizes parsing a bit.
Expand Down
4 changes: 3 additions & 1 deletion dotenv/src/lib.rs
@@ -1,3 +1,6 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::missing_errors_doc)]

//! [`dotenv`]: https://crates.io/crates/dotenv
//! A well-maintained fork of the [`dotenv`] crate
//!
Expand Down Expand Up @@ -209,7 +212,6 @@ pub fn from_filename_override<P: AsRef<Path>>(filename: P) -> Result<PathBuf> {
/// # Ok(())
/// # }
/// ```

pub fn from_filename_iter<P: AsRef<Path>>(filename: P) -> Result<Iter<File>> {
let (_, iter) = Finder::new().filename(filename.as_ref()).find()?;
Ok(iter)
Expand Down
16 changes: 6 additions & 10 deletions dotenv/src/parse.rs
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::env;
#![allow(clippy::module_name_repetitions)]

use crate::errors::*;
use crate::errors::{Error, Result};
use std::{collections::HashMap, env};

// for readability's sake
pub type ParsedLine = Result<Option<(String, String)>>;
Expand Down Expand Up @@ -120,10 +120,7 @@ enum SubstitutionMode {
EscapedBlock,
}

fn parse_value(
input: &str,
substitution_data: &mut HashMap<String, Option<String>>,
) -> Result<String> {
fn parse_value(input: &str, substitution_data: &HashMap<String, Option<String>>) -> Result<String> {

Check failure on line 123 in dotenv/src/parse.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has too many lines (115/100)
let mut strong_quote = false; // '
let mut weak_quote = false; // "
let mut escaped = false;
Expand All @@ -145,9 +142,8 @@ fn parse_value(
continue;
} else if c == '#' {
break;
} else {
return Err(Error::LineParse(input.to_owned(), index));
}
return Err(Error::LineParse(input.to_owned(), index));
} else if escaped {
//TODO I tried handling literal \r but various issues
//imo not worth worrying about until there's a use case
Expand Down Expand Up @@ -258,7 +254,7 @@ fn parse_value(
}

fn apply_substitution(
substitution_data: &mut HashMap<String, Option<String>>,
substitution_data: &HashMap<String, Option<String>>,
substitution_name: &str,
output: &mut String,
) {
Expand Down
3 changes: 2 additions & 1 deletion dotenv_codegen/Cargo.toml
Expand Up @@ -10,10 +10,11 @@ authors = [
"Mike Piccolo <mfpiccolo@gmail.com>",
"Alice Maz <alice@alicemaz.com>",
"Sean Griffin <sean@seantheprogrammer.com>",
"Allan Zhang <al@ayz.ai>",
"Allan Zhang <allanzhang7@gmail.com>",
]
readme = "README.md"
keywords = ["dotenv", "env", "environment", "settings", "config"]
categories = ["configuration"]
license = "MIT"
homepage = "https://github.com/allan2/dotenvy"
repository = "https://github.com/allan2/dotenvy"
Expand Down

0 comments on commit bbb06cd

Please sign in to comment.