forc_migrate/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pub mod cli;
#[macro_use]
mod migrations;
mod matching;
mod modifying;

use std::fmt::Display;
use std::io::{self, Write};

/// Returns a single error string formed of the `error` and `instructions`.
/// The returned string is formatted to be used as an error message in the [anyhow::bail] macro.
fn instructive_error<E: Display, I: Display>(error: E, instructions: &[I]) -> String {
    let mut error_message = vec![format!("{error}")];
    instructions
        .iter()
        .map(|inst| format!("       {inst}"))
        .for_each(|inst| error_message.push(inst));
    error_message.join("\n")
}

/// Returns a single error string representing an internal error.
/// The returned string is formatted to be used as an error message in the [anyhow::bail] macro.
fn internal_error<E: Display>(error: E) -> String {
    instructive_error(error, &[
        "This is an internal error and signifies a bug in the `forc migrate` tool.",
        "Please report this error by filing an issue at https://github.com/FuelLabs/sway/issues/new?template=bug_report.yml.",
    ])
}

/// Prints a menu containing numbered `options` and asks to choose one of them.
/// Returns zero-indexed index of the chosen option.
fn print_single_choice_menu<S: AsRef<str> + Display>(options: &[S]) -> usize {
    assert!(
        options.len() > 1,
        "There must be at least two options to choose from."
    );

    for (i, option) in options.iter().enumerate() {
        println!("{}. {option}", i + 1);
    }

    let mut choice = usize::MAX;
    while choice == 0 || choice > options.len() {
        print!("Enter your choice [1..{}]: ", options.len());
        io::stdout().flush().unwrap();
        let mut input = String::new();
        choice = match std::io::stdin().read_line(&mut input) {
            Ok(_) => match input.trim().parse() {
                Ok(choice) => choice,
                Err(_) => continue,
            },
            Err(_) => continue,
        }
    }

    choice - 1
}