Lazy Rust 2018 migration copy pastes


#1

Make sure you have the latest rustup and rustc:

rustup self update
rustup update

Warning: I copy pasted the commands into my terminal; didn’t actually run this as a script.

Then use the following (gist):

#! /bin/bash
#
# This is ~~unsafe~~ bash.
#
# Rudimentary automation to update Rust code to use idiomatic 2018.
#
# If you have spaces in your paths, this script will probably not work for you.
#
# Author: azriel

# Moves src/submodule/mod.rs to src/submodule.rs
function rename_mod_rs_to_module {
  for f in $(find . -type f -name "mod.rs")
  do
    echo mv $f ${f%%/mod.rs}.rs
  done
}

# Expects GNU `sed`. Not sure if BSD `sed` works here (OS X users, check `sed --version`).
function cargo_toml_insert_edition {
  for f in $(find . -type f -name "Cargo.toml")
  do
    # Skip file if it already has edition string
    #
    # Rudimentary detection for workspace Cargo.toml:
    # Only adds the edition if there is an `authors` key.
    # Note: it expects the `authors` key to be one line.
    #
    # Idempotent - safe to run multiple times.
    grep -qF 'edition =' $f || sed -i 's/^\(authors = .\+\)$/\1\nedition = "2018"/' $f
  done
}

function rogue {
  rename_mod_rs_to_module
  cargo fix --edition
  cargo_toml_insert_edition
  cargo fix --edition-idioms
  git add .
  git commit -m "Migrate to Rust 2018"
}

function historian {
  rename_mod_rs_to_module
  git add .
  git commit -m 'Renamed all `mod.rs` to the module short name.'

  cargo fix --edition
  git add .
  git commit -m 'Run `cargo fix --edition` over repository.'

  cargo_toml_insert_edition
  git add .
  git commit -m 'Specify `edition = "2018"` in `Cargo.toml`.'

  cargo fix --edition-idioms
  git add .
  git commit -m 'Run `cargo fix --edition-idioms` over repository.'
}

function archeologist {
  rename_mod_rs_to_module
  cargo test --all
  git add .
  git commit -m 'Renamed all `mod.rs` to the module short name.'

  cargo fix --edition
  cargo test --all
  git add .
  git commit -m 'Run `cargo fix --edition` over repository.'

  cargo_toml_insert_edition
  cargo test --all
  git add .
  git commit -m 'Specify `edition = "2018"` in `Cargo.toml`.'

  cargo fix --edition-idioms
  cargo test --all
  git add .
  git commit -m 'Run `cargo fix --edition-idioms` over repository.'
}


# Stop on first error
set -e

archeologist