Skip to content

Commit

Permalink
Use Rust 2021 edition. (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-levin authored Jan 14, 2025
1 parent 417e1ab commit a83c305
Show file tree
Hide file tree
Showing 43 changed files with 397 additions and 365 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ name = "elan"
version = "4.0.0-pre"
authors = [ "Sebastian Ullrich <[email protected]>" ]
description = "Manage multiple Lean installations with ease"
edition = "2021"
publish = false

license = "MIT OR Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions src/download/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
name = "download"
version = "0.4.0"
authors = [ "Brian Anderson <[email protected]>" ]
edition = "2021"

license = "MIT/Apache-2.0"

Expand Down
2 changes: 2 additions & 0 deletions src/download/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use error_chain::error_chain;

error_chain! {
links { }

Expand Down
29 changes: 9 additions & 20 deletions src/download/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
//! Easy file downloading
#[macro_use]
extern crate error_chain;
extern crate url;
#![deny(rust_2018_idioms)]

use std::path::Path;
use url::Url;

mod errors;
pub use errors::*;
pub use crate::errors::*;

#[derive(Debug, Copy, Clone)]
pub enum Backend {
Expand All @@ -26,7 +23,7 @@ pub enum Event<'a> {
fn download_with_backend(
backend: Backend,
url: &Url,
callback: &dyn Fn(Event) -> Result<()>,
callback: &dyn Fn(Event<'_>) -> Result<()>,
) -> Result<()> {
match backend {
Backend::Curl => curl::download(url, callback),
Expand All @@ -37,7 +34,7 @@ pub fn download_to_path_with_backend(
backend: Backend,
url: &Url,
path: &Path,
callback: Option<&dyn Fn(Event) -> Result<()>>,
callback: Option<&dyn Fn(Event<'_>) -> Result<()>>,
) -> Result<()> {
use std::cell::RefCell;
use std::fs::OpenOptions;
Expand All @@ -47,7 +44,7 @@ pub fn download_to_path_with_backend(
let file = OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.open(path)
.chain_err(|| "error creating file for download")?;

let file = RefCell::new(file);
Expand All @@ -70,30 +67,24 @@ pub fn download_to_path_with_backend(

Ok(())
}()
.map_err(|e| {
// TODO is there any point clearing up here? What kind of errors will leave us with an unusable partial?
e
})
}

/// Download via libcurl; encrypt with the native (or OpenSSl) TLS
/// stack via libcurl
#[cfg(feature = "curl-backend")]
pub mod curl {

extern crate curl;

use self::curl::easy::Easy;
use super::Event;
use errors::*;
use crate::errors::*;
use curl::easy::Easy;
use std::cell::RefCell;
use std::str;
use std::time::Duration;
use url::Url;

thread_local!(pub static EASY: RefCell<Easy> = RefCell::new(Easy::new()));

pub fn download(url: &Url, callback: &dyn Fn(Event) -> Result<()>) -> Result<()> {
pub fn download(url: &Url, callback: &dyn Fn(Event<'_>) -> Result<()>) -> Result<()> {
// Fetch either a cached libcurl handle (which will preserve open
// connections) or create a new one if it isn't listed.
//
Expand All @@ -102,9 +93,7 @@ pub mod curl {
EASY.with(|handle| {
let mut handle = handle.borrow_mut();

handle
.url(&url.to_string())
.chain_err(|| "failed to set url")?;
handle.url(url.as_ref()).chain_err(|| "failed to set url")?;
handle
.follow_location(true)
.chain_err(|| "failed to set follow redirects")?;
Expand Down
11 changes: 5 additions & 6 deletions src/elan-cli/common.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
//! Just a dumping ground for cli stuff
use crate::errors::*;
use crate::term2;
use elan::{Cfg, Notification, Toolchain};
use elan_dist::dist::ToolchainDesc;
use elan_utils::notify::NotificationLevel;
use elan_utils::utils;
use errors::*;
use std;
use std::io::{BufRead, BufReader, Write};
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::time::Duration;
use term2;
use wait_timeout::ChildExt;

pub fn confirm(question: &str, default: bool) -> Result<bool> {
Expand Down Expand Up @@ -101,12 +100,12 @@ pub fn read_line() -> Result<String> {
}

pub fn set_globals(verbose: bool) -> Result<Cfg> {
use download_tracker::DownloadTracker;
use crate::download_tracker::DownloadTracker;
use std::cell::RefCell;

let download_tracker = RefCell::new(DownloadTracker::new());

Ok(Cfg::from_env(Arc::new(move |n: Notification| {
Ok(Cfg::from_env(Arc::new(move |n: Notification<'_>| {
if download_tracker.borrow_mut().handle_notification(&n) {
return;
}
Expand Down Expand Up @@ -153,7 +152,7 @@ pub fn show_channel_update(cfg: &Cfg, desc: &ToolchainDesc) -> Result<()> {
Ok(())
}

pub fn lean_version(toolchain: &Toolchain) -> String {
pub fn lean_version(toolchain: &Toolchain<'_>) -> String {
if toolchain.exists() {
let lean_path = toolchain.binary_file("lean");
if utils::is_file(&lean_path) {
Expand Down
5 changes: 2 additions & 3 deletions src/elan-cli/download_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use elan_utils::tty;
use elan_utils::Notification as Un;
use std::collections::VecDeque;
use std::fmt;
use term;
use time::OffsetDateTime;

/// Keep track of this many past download amounts
Expand Down Expand Up @@ -53,7 +52,7 @@ impl DownloadTracker {
}
}

pub fn handle_notification(&mut self, n: &Notification) -> bool {
pub fn handle_notification(&mut self, n: &Notification<'_>) -> bool {
match *n {
Notification::Install(In::Utils(Un::DownloadContentLengthReceived(content_len))) => {
self.content_length_received(content_len);
Expand Down Expand Up @@ -178,7 +177,7 @@ impl DownloadTracker {
struct HumanReadable(f64);

impl fmt::Display for HumanReadable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
// repurposing the alternate mode for ETA
let sec = self.0;
Expand Down
50 changes: 28 additions & 22 deletions src/elan-cli/elan_mode.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use crate::common;
use crate::errors::*;
use crate::help::*;
use crate::self_update;
use crate::term2;
use clap::{App, AppSettings, Arg, ArgMatches, Shell, SubCommand};
use common;
use elan::{command, gc, lookup_toolchain_desc, lookup_unresolved_toolchain_desc, Cfg, Toolchain};
use elan_dist::dist::ToolchainDesc;
use elan_utils::utils;
use errors::*;
use help::*;
use self_update;
use std::error::Error;
use std::io::{self, Write};
use std::path::Path;
use std::process::Command;
use term2;

use serde_derive::Serialize;

use crate::json_dump;

pub fn main() -> Result<()> {
::self_update::cleanup_self_updater()?;
crate::self_update::cleanup_self_updater()?;

let matches = &cli().get_matches();
let verbose = matches.is_present("verbose");
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn main() -> Result<()> {
&mut io::stdout(),
);
}
},
}
("dump-state", Some(m)) => dump_state(cfg, m)?,
(_, _) => unreachable!(),
}
Expand Down Expand Up @@ -249,7 +249,7 @@ pub fn cli() -> App<'static, 'static> {
)
}

fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn default_(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let name = m.value_of("toolchain").expect("");
// sanity-check
let _ = lookup_unresolved_toolchain_desc(cfg, name)?;
Expand All @@ -258,7 +258,7 @@ fn default_(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Ok(())
}

fn install(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn install(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let names = m.values_of("toolchain").expect("");
for name in names {
let desc = lookup_toolchain_desc(cfg, name)?;
Expand All @@ -274,7 +274,7 @@ fn install(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Ok(())
}

fn run(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn run(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let toolchain = m.value_of("toolchain").expect("");
let args = m.values_of("command").unwrap();
let args: Vec<_> = args.collect();
Expand All @@ -284,7 +284,7 @@ fn run(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Ok(command::run_command_for_dir(cmd, args[0], &args[1..])?)
}

fn which(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn which(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let binary = m.value_of("command").expect("");

let binary_path = cfg
Expand Down Expand Up @@ -415,7 +415,7 @@ fn show(cfg: &Cfg) -> Result<()> {
Ok(())
}

fn explicit_or_dir_toolchain<'a>(cfg: &'a Cfg, m: &ArgMatches) -> Result<Toolchain<'a>> {
fn explicit_or_dir_toolchain<'a>(cfg: &'a Cfg, m: &ArgMatches<'_>) -> Result<Toolchain<'a>> {
let toolchain = m.value_of("toolchain");
if let Some(toolchain) = toolchain {
let desc = lookup_toolchain_desc(cfg, toolchain)?;
Expand All @@ -429,7 +429,7 @@ fn explicit_or_dir_toolchain<'a>(cfg: &'a Cfg, m: &ArgMatches) -> Result<Toolcha
Ok(toolchain)
}

fn toolchain_link(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn toolchain_link(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let toolchain = &m.value_of("toolchain").expect("");
let path = &m.value_of("path").expect("");
let desc = ToolchainDesc::Local {
Expand All @@ -440,7 +440,7 @@ fn toolchain_link(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Ok(toolchain.install_from_dir(Path::new(path), true)?)
}

fn toolchain_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn toolchain_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
for toolchain in m.values_of("toolchain").expect("") {
let desc = lookup_toolchain_desc(cfg, toolchain)?;
let toolchain = cfg.get_toolchain(&desc, false)?;
Expand Down Expand Up @@ -468,7 +468,10 @@ fn toolchain_gc(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let json = m.is_present("json");
if json {
let result = GCResult {
unused_toolchains: unused_toolchains.iter().map(|t| t.desc.to_string()).collect(),
unused_toolchains: unused_toolchains
.iter()
.map(|t| t.desc.to_string())
.collect(),
used_toolchains: used_toolchains
.iter()
.map(|(root, tc)| UsedToolchain {
Expand All @@ -477,7 +480,10 @@ fn toolchain_gc(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
})
.collect(),
};
println!("{}", serde_json::to_string_pretty(&result).chain_err(|| "failed to print JSON")?);
println!(
"{}",
serde_json::to_string_pretty(&result).chain_err(|| "failed to print JSON")?
);
return Ok(());
}

Expand All @@ -504,15 +510,15 @@ fn toolchain_gc(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
Ok(())
}

fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn override_add(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let toolchain = m.value_of("toolchain").expect("");
let desc = lookup_toolchain_desc(cfg, toolchain)?;
let toolchain = cfg.get_toolchain(&desc, false)?;
toolchain.make_override(&utils::current_dir()?)?;
Ok(())
}

fn override_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn override_remove(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let paths = if m.is_present("nonexistent") {
let list: Vec<_> = cfg.settings_file.with(|s| {
Ok(s.overrides
Expand Down Expand Up @@ -555,7 +561,7 @@ fn override_remove(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Ok(())
}

fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn doc(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let doc_url = if m.is_present("book") {
"book/index.html"
} else if m.is_present("std") {
Expand All @@ -567,7 +573,7 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Ok(cfg.open_docs_for_dir(&utils::current_dir()?, doc_url)?)
}

fn man(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn man(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let manpage = m.value_of("command").expect("");
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
let mut man_path = toolchain.path().to_path_buf();
Expand All @@ -583,13 +589,13 @@ fn man(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
Ok(())
}

fn self_uninstall(m: &ArgMatches) -> Result<()> {
fn self_uninstall(m: &ArgMatches<'_>) -> Result<()> {
let no_prompt = m.is_present("no-prompt");

self_update::uninstall(no_prompt)
}

fn dump_state(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn dump_state(cfg: &Cfg, m: &ArgMatches<'_>) -> Result<()> {
let no_net = m.is_present("no-net");

Ok(json_dump::StateDump::new(cfg, no_net)?.print()?)
Expand Down
3 changes: 1 addition & 2 deletions src/elan-cli/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
use std::io;
use std::path::PathBuf;

use elan;
use elan_dist::{self, temp};
use elan_utils;
use error_chain::error_chain;

error_chain! {
links {
Expand Down
2 changes: 0 additions & 2 deletions src/elan-cli/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ mod imp {

#[cfg(windows)]
mod imp {
extern crate winapi;

use std::ffi::OsString;
use std::io;
use std::mem;
Expand Down
Loading

0 comments on commit a83c305

Please sign in to comment.