forc_migrate/
lib.rs

1pub mod cli;
2#[macro_use]
3mod migrations;
4mod matching;
5mod modifying;
6mod visiting;
7
8use std::fmt::Display;
9use std::io::{self, Write};
10
11/// Returns a single error string formed of the `error` and `instructions`.
12/// The returned string is formatted to be used as an error message in the [anyhow::bail] macro.
13fn instructive_error<E: Display, I: Display>(error: E, instructions: &[I]) -> String {
14    let mut error_message = vec![format!("{error}")];
15    instructions
16        .iter()
17        .map(|inst| format!("       {inst}"))
18        .for_each(|inst| error_message.push(inst));
19    error_message.join("\n")
20}
21
22/// Returns a single error string representing an internal error.
23/// The returned string is formatted to be used as an error message in the [anyhow::bail] macro.
24fn internal_error<E: Display>(error: E) -> String {
25    instructive_error(error, &[
26        "This is an internal error and signifies a bug in the `forc migrate` tool.",
27        "Please report this error by filing an issue at https://github.com/FuelLabs/sway/issues/new?template=bug_report.yml.",
28    ])
29}
30
31/// Prints a menu containing numbered `options` and asks to choose one of them.
32/// Returns zero-indexed index of the chosen option.
33fn print_single_choice_menu<S: AsRef<str> + Display>(options: &[S]) -> usize {
34    assert!(
35        options.len() > 1,
36        "There must be at least two options to choose from."
37    );
38
39    for (i, option) in options.iter().enumerate() {
40        println!("{}. {option}", i + 1);
41    }
42
43    let mut choice = usize::MAX;
44    while choice == 0 || choice > options.len() {
45        print!("Enter your choice [1..{}]: ", options.len());
46        io::stdout().flush().unwrap();
47        let mut input = String::new();
48        choice = match std::io::stdin().read_line(&mut input) {
49            Ok(_) => match input.trim().parse() {
50                Ok(choice) => choice,
51                Err(_) => continue,
52            },
53            Err(_) => continue,
54        }
55    }
56
57    choice - 1
58}