From f5e081ab6812aa69828bc066ae666a20da86cc28 Mon Sep 17 00:00:00 2001 From: Danilo Bargen Date: Fri, 7 Dec 2018 11:42:39 +0100 Subject: [PATCH] Rust 2018 (#69) --- .circleci/config.yml | 2 +- .travis.yml | 2 +- Cargo.toml | 1 + README.md | 2 +- src/cache.rs | 35 ++++++++++++++++------------------- src/config.rs | 6 ++++-- src/formatter.rs | 7 ++++--- src/main.rs | 35 ++++++++++------------------------- src/tokenizer.rs | 5 +++-- src/types.rs | 2 ++ 10 files changed, 43 insertions(+), 54 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f6e7733..f432d19 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -2,7 +2,7 @@ version: 2 jobs: build: docker: - - image: rust:1.30 + - image: rust:1.31 steps: - checkout # Load cargo target from cache if possible. diff --git a/.travis.yml b/.travis.yml index 1f5acb0..3bf39a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: rust os: osx rust: - - 1.30.0 + - 1.31.0 - stable cache: cargo script: cargo test diff --git a/Cargo.toml b/Cargo.toml index b7a1e10..00f2fa2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ readme = "README.md" repository = "https://github.com/dbrgn/tealdeer/" version = "1.1.0" include = ["/src/**/*", "/tests/**/*", "/Cargo.toml", "/README.md", "/LICENSE-*", "/screenshot.png", "/bash_tealdeer"] +edition = "2018" [[bin]] name = "tldr" diff --git a/README.md b/README.md index ea7c788..af9b690 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ tealdeer has been added to a few package managers: ### From Source (any platform) -tealdeer requires at least Rust 1.30. +tealdeer requires at least Rust 1.31. Debug build with logging enabled: diff --git a/src/cache.rs b/src/cache.rs index 5f1359e..9d08457 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -8,13 +8,14 @@ use std::os::unix::fs::MetadataExt; use reqwest::{Client, Proxy}; use flate2::read::GzDecoder; +use log::debug; use tar::Archive; use time; use walkdir::{DirEntry, WalkDir}; use xdg::BaseDirectories; -use error::TealdeerError::{self, CacheError, UpdateError}; -use types::OsType; +use crate::error::TealdeerError::{self, CacheError, UpdateError}; +use crate::types::OsType; #[derive(Debug)] pub struct Cache { @@ -52,7 +53,7 @@ impl Cache { }; // Otherwise, fall back to $XDG_CACHE_HOME/tealdeer. - let xdg_dirs = match BaseDirectories::with_prefix(::NAME) { + let xdg_dirs = match BaseDirectories::with_prefix(crate::NAME) { Ok(dirs) => dirs, Err(_) => return Err(CacheError("Could not determine XDG base directory.".into())), }; @@ -88,20 +89,18 @@ impl Cache { /// Update the pages cache. pub fn update(&self) -> Result<(), TealdeerError> { // First, download the compressed data - let bytes: Vec = try!(self.download()); + let bytes: Vec = self.download()?; // Decompress the response body into an `Archive` let mut archive = self.decompress(&bytes[..]); // Determine paths - let cache_dir = try!(self.get_cache_dir()); + let cache_dir = self.get_cache_dir()?; // Make sure that cache directory exists debug!("Ensure cache directory {:?} exists", &cache_dir); - try!( - fs::create_dir_all(&cache_dir) - .map_err(|e| UpdateError(format!("Could not create cache directory: {}", e))) - ); + fs::create_dir_all(&cache_dir) + .map_err(|e| UpdateError(format!("Could not create cache directory: {}", e)))?; // Clear cache directory // Note: This is not the best solution. Ideally we would download the @@ -109,14 +108,12 @@ impl Cache { // But renaming a directory doesn't work across filesystems and Rust // does not yet offer a recursive directory copying function. So for // now, we'll use this approach. - try!(self.clear()); + self.clear()?; // Extract archive - try!( - archive - .unpack(&cache_dir) - .map_err(|e| UpdateError(format!("Could not unpack compressed data: {}", e))) - ); + archive + .unpack(&cache_dir) + .map_err(|e| UpdateError(format!("Could not unpack compressed data: {}", e)))?; Ok(()) } @@ -181,7 +178,7 @@ impl Cache { /// Return the available pages. pub fn list_pages(&self) -> Result, TealdeerError> { // Determine platforms directory and platform - let cache_dir = try!(self.get_cache_dir()); + let cache_dir = self.get_cache_dir()?; let platforms_dir = cache_dir.join("tldr-master").join("pages"); let platform_dir = self.get_platform_dir(); @@ -230,12 +227,12 @@ impl Cache { /// Delete the cache directory. pub fn clear(&self) -> Result<(), TealdeerError> { - let path = try!(self.get_cache_dir()); + let path = self.get_cache_dir()?; if path.exists() && path.is_dir() { - try!(fs::remove_dir_all(&path).map_err(|_| CacheError(format!( + fs::remove_dir_all(&path).map_err(|_| CacheError(format!( "Could not remove cache directory ({}).", path.display() - )))); + )))?; } else if path.exists() { return Err(CacheError(format!( "Cache path ({}) is not a directory.", diff --git a/src/config.rs b/src/config.rs index 1463aa4..443d87f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,10 +4,12 @@ use std::io::{Error as IoError, Read, Write}; use std::path::PathBuf; use ansi_term::{Color, Style}; +use log::debug; +use serde_derive::{Deserialize, Serialize}; use toml; use xdg::BaseDirectories; -use error::TealdeerError::{self, ConfigError}; +use crate::error::TealdeerError::{self, ConfigError}; pub const CONFIG_FILE_NAME: &str = "config.toml"; @@ -199,7 +201,7 @@ pub fn get_config_dir() -> Result { }; // Otherwise, fall back to $XDG_CONFIG_HOME/tealdeer. - let xdg_dirs = match BaseDirectories::with_prefix(::NAME) { + let xdg_dirs = match BaseDirectories::with_prefix(crate::NAME) { Ok(dirs) => dirs, Err(_) => { return Err(ConfigError("Could not determine XDG base directory.".into())) diff --git a/src/formatter.rs b/src/formatter.rs index b37c080..488f8ed 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -3,10 +3,11 @@ use std::io::BufRead; use ansi_term::{ANSIString, ANSIStrings}; +use log::debug; -use config::Config; -use tokenizer::Tokenizer; -use types::LineType; +use crate::config::Config; +use crate::tokenizer::Tokenizer; +use crate::types::LineType; fn highlight_command<'a>( command: &'a str, diff --git a/src/main.rs b/src/main.rs index b22c8fe..dc9976c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,21 +43,8 @@ ) )] -#[macro_use] -extern crate log; -extern crate ansi_term; -extern crate reqwest; -extern crate docopt; #[cfg(feature = "logging")] extern crate env_logger; -extern crate flate2; -extern crate tar; -extern crate time; -extern crate toml; -extern crate walkdir; -extern crate xdg; -#[macro_use] -extern crate serde_derive; use std::fs::File; use std::io::BufReader; @@ -66,6 +53,7 @@ use std::process; use ansi_term::Color; use docopt::Docopt; +use serde_derive::Deserialize; mod cache; mod config; @@ -74,12 +62,12 @@ mod formatter; mod tokenizer; mod types; -use cache::Cache; -use config::{get_config_path, make_default_config, Config}; -use error::TealdeerError::{CacheError, ConfigError, UpdateError}; -use formatter::print_lines; -use tokenizer::Tokenizer; -use types::OsType; +use crate::cache::Cache; +use crate::config::{get_config_path, make_default_config, Config}; +use crate::error::TealdeerError::{CacheError, ConfigError, UpdateError}; +use crate::formatter::print_lines; +use crate::tokenizer::Tokenizer; +use crate::types::OsType; const NAME: &str = "tealdeer"; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -137,7 +125,7 @@ struct Args { /// Print page by path fn print_page(path: &Path) -> Result<(), String> { // Open file - let file = try!(File::open(path).map_err(|msg| format!("Could not open file: {}", msg))); + let file = File::open(path).map_err(|msg| format!("Could not open file: {}", msg))?; let reader = BufReader::new(file); // Look up config file, if none is found fall back to default config. @@ -375,11 +363,8 @@ fn main() { #[cfg(test)] mod test { - use docopt::Docopt; - use docopt::Error; - use Args; - use OsType; - use USAGE; + use docopt::{Docopt, Error}; + use crate::{Args, OsType, USAGE}; fn test_helper(argv: &[&str]) -> Result { Docopt::new(USAGE).and_then(|d| d.argv(argv.iter()).deserialize()) diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 9448821..846c9f1 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -2,7 +2,8 @@ use std::io::BufRead; -use types::LineType; +use log::warn; +use crate::types::LineType; #[derive(Debug, PartialEq, Eq)] pub enum TldrFormat { @@ -87,7 +88,7 @@ where #[cfg(test)] mod test { use super::Tokenizer; - use types::LineType; + use crate::types::LineType; #[test] fn test_first_line_old_format() { diff --git a/src/types.rs b/src/types.rs index e1456aa..a7d3f35 100644 --- a/src/types.rs +++ b/src/types.rs @@ -2,6 +2,8 @@ use std::fmt; +use serde_derive::{Deserialize, Serialize}; + #[derive(Debug, Eq, PartialEq, Copy, Clone, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] #[allow(dead_code)]