Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds anyhow where result used to be #860

Merged
merged 6 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions forc/src/cli/commands/addr2line.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use std::collections::VecDeque;
use std::fs::{self, File};
Expand Down Expand Up @@ -28,14 +29,15 @@ pub(crate) struct Command {
pub opcode_index: usize,
}

pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
let contents = fs::read(&command.sourcemap_path)
.map_err(|err| format!("{:?}: could not read: {:?}", command.sourcemap_path, err))?;
.map_err(|err| anyhow!("{:?}: could not read: {:?}", command.sourcemap_path, err))?;

let sm: SourceMap = serde_json::from_slice(&contents).map_err(|err| {
format!(
anyhow!(
"{:?}: invalid source map json: {}",
command.sourcemap_path, err
command.sourcemap_path,
err
)
})?;

Expand All @@ -45,7 +47,7 @@ pub(crate) fn exec(command: Command) -> Result<(), String> {
}

let rr = read_range(&path, range, command.context)
.map_err(|err| format!("{:?}: could not read: {:?}", path, err))?;
.map_err(|err| anyhow!("{:?}: could not read: {:?}", path, err))?;

let path_str = format!("{:?}", path);
let snippet = Snippet {
Expand All @@ -71,7 +73,7 @@ pub(crate) fn exec(command: Command) -> Result<(), String> {

Ok(())
} else {
Err("Address did not map to any source code location".to_owned())
Err(anyhow!("Address did not map to any source code location"))
}
}

Expand Down
3 changes: 2 additions & 1 deletion forc/src/cli/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_build;
use anyhow::Result;
use clap::Parser;

/// Compile the current or target project.
Expand Down Expand Up @@ -48,7 +49,7 @@ pub struct Command {
pub minify_json_abi: bool,
}

pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
forc_build::build(command)?;
Ok(())
}
3 changes: 2 additions & 1 deletion forc/src/cli/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_clean;
use anyhow::Result;
use clap::Parser;

/// Removes the default forc compiler output artifact directory, i.e. `<project-name>/out`. Also
Expand All @@ -11,6 +12,6 @@ pub struct Command {
pub path: Option<String>,
}

pub fn exec(command: Command) -> Result<(), String> {
pub fn exec(command: Command) -> Result<()> {
forc_clean::clean(command)
}
3 changes: 2 additions & 1 deletion forc/src/cli/commands/completions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use clap::Command as ClapCommand;
use clap::{CommandFactory, Parser};
use clap_complete::{generate, Generator, Shell};
Expand Down Expand Up @@ -121,7 +122,7 @@ pub struct Command {
shell: Shell,
}

pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
let mut cmd = super::super::Opt::command();
print_completions(command.shell, &mut cmd);
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions forc/src/cli/commands/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_deploy;
use anyhow::{bail, Result};
use clap::Parser;

/// Deploy contract project.
Expand Down Expand Up @@ -44,9 +45,9 @@ pub struct Command {
pub minify_json_abi: bool,
}

pub(crate) async fn exec(command: Command) -> Result<(), String> {
pub(crate) async fn exec(command: Command) -> Result<()> {
match forc_deploy::deploy(command).await {
Err(e) => Err(e.message),
Err(e) => bail!(e.message),
_ => Ok(()),
}
}
5 changes: 3 additions & 2 deletions forc/src/cli/commands/explorer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_explorer;
use anyhow::{bail, Result};
use clap::Parser;

/// Run the network explorer.
Expand All @@ -16,9 +17,9 @@ pub enum CleanCommand {
Clean,
}

pub(crate) async fn exec(_command: Command) -> Result<(), String> {
pub(crate) async fn exec(_command: Command) -> Result<()> {
match forc_explorer::exec(_command).await {
Err(e) => Err(e.to_string()),
Err(e) => bail!(e),
_ => Ok(()),
}
}
5 changes: 3 additions & 2 deletions forc/src/cli/commands/format.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_fmt;
use anyhow::{bail, Result};
use clap::Parser;

/// Format all Sway files of the current project.
Expand All @@ -12,9 +13,9 @@ pub struct Command {
}

// todo: add formatting options in the command line
pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
match forc_fmt::format(command) {
Err(e) => Err(e.message),
Err(e) => bail!(e.message),
_ => Ok(()),
}
}
5 changes: 3 additions & 2 deletions forc/src/cli/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_init;
use anyhow::Result;
use clap::Parser;

/// Create a new Forc project.
Expand All @@ -7,7 +8,7 @@ pub(crate) struct Command {
project_name: String,
}

pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
let project_name = command.project_name;
forc_init::init_new_project(project_name).map_err(|e| e.to_string())
forc_init::init_new_project(project_name).map_err(|e| e)
}
3 changes: 2 additions & 1 deletion forc/src/cli/commands/json_abi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_abi_json;
use anyhow::Result;
use clap::Parser;

/// Output the JSON associated with the ABI.
Expand All @@ -23,7 +24,7 @@ pub struct Command {
pub minify: bool,
}

pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
forc_abi_json::build(command)?;
Ok(())
}
3 changes: 2 additions & 1 deletion forc/src/cli/commands/lsp.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::Result;
use clap::Parser;
use sway_server::start;
/// Run the LSP server.
#[derive(Debug, Parser)]
pub(crate) struct Command {}

pub(crate) async fn exec(_command: Command) -> Result<(), String> {
pub(crate) async fn exec(_command: Command) -> Result<()> {
start().await;
Ok(())
}
7 changes: 4 additions & 3 deletions forc/src/cli/commands/parse_bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use std::fs::{self, File};
use std::io::Read;
Expand All @@ -10,11 +11,11 @@ pub(crate) struct Command {
file_path: String,
}

pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
let mut f = File::open(&command.file_path)
.map_err(|_| format!("{}: file not found", command.file_path))?;
.map_err(|_| anyhow!("{}: file not found", command.file_path))?;
let metadata = fs::metadata(&command.file_path)
.map_err(|_| format!("{}: file not found", command.file_path))?;
.map_err(|_| anyhow!("{}: file not found", command.file_path))?;
let mut buffer = vec![0; metadata.len() as usize];
f.read_exact(&mut buffer).expect("buffer overflow");
let mut instructions = vec![];
Expand Down
5 changes: 3 additions & 2 deletions forc/src/cli/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_run;
use anyhow::{bail, Result};
use clap::Parser;

/// Run script project.
Expand Down Expand Up @@ -74,9 +75,9 @@ pub struct Command {
pub minify_json_abi: bool,
}

pub(crate) async fn exec(command: Command) -> Result<(), String> {
pub(crate) async fn exec(command: Command) -> Result<()> {
match forc_run::run(command).await {
Err(e) => Err(e.message),
Err(e) => bail!(e.message),
_ => Ok(()),
}
}
3 changes: 2 additions & 1 deletion forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_build;
use anyhow::Result;
use clap::Parser;
use std::io::{BufRead, BufReader};
use std::process::Command as ProcessCommand;
Expand All @@ -18,7 +19,7 @@ pub(crate) struct Command {
pub test_name: Option<String>,
}

pub(crate) fn exec(command: Command) -> Result<(), String> {
pub(crate) fn exec(command: Command) -> Result<()> {
// Ensure the project builds before running tests.
forc_build::build(Default::default())?;

Expand Down
5 changes: 3 additions & 2 deletions forc/src/cli/commands/update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::ops::forc_update;
use anyhow::{bail, Result};
use clap::Parser;

/// Update dependencies in the Forc dependencies directory.
Expand All @@ -20,9 +21,9 @@ pub struct Command {
pub check: bool,
}

pub(crate) async fn exec(command: Command) -> Result<(), String> {
pub(crate) async fn exec(command: Command) -> Result<()> {
match forc_update::update(command).await {
Ok(_) => Ok(()),
Err(e) => Err(format!("couldn't update dependencies: {}", e)),
Err(e) => bail!("couldn't update dependencies: {}", e),
}
}
3 changes: 2 additions & 1 deletion forc/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use clap::Parser;

mod commands;
Expand Down Expand Up @@ -50,7 +51,7 @@ enum Forc {
Lsp(LspCommand),
}

pub(crate) async fn run_cli() -> Result<(), String> {
pub(crate) async fn run_cli() -> Result<()> {
let opt = Opt::parse();

match opt.command {
Expand Down
4 changes: 3 additions & 1 deletion forc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ mod cli;
mod ops;
mod utils;

use anyhow::Result;

#[tokio::main]
async fn main() -> Result<(), String> {
async fn main() -> Result<()> {
cli::run_cli().await
}
8 changes: 4 additions & 4 deletions forc/src/ops/forc_abi_json.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::cli::{BuildCommand, JsonAbiCommand};

use anyhow::Result;
use serde_json::{json, Value};
use std::fs::File;

pub fn build(command: JsonAbiCommand) -> Result<Value, String> {
pub fn build(command: JsonAbiCommand) -> Result<Value> {
let build_command = BuildCommand {
path: command.path,
offline_mode: command.offline_mode,
Expand All @@ -14,13 +14,13 @@ pub fn build(command: JsonAbiCommand) -> Result<Value, String> {
let (_bytes, json_abi) = crate::ops::forc_build::build(build_command)?;
let json_abi = json!(json_abi);
if let Some(outfile) = command.json_outfile {
let file = File::create(outfile).map_err(|e| e.to_string())?;
let file = File::create(outfile).map_err(|e| e)?;
let res = if command.minify {
serde_json::to_writer(&file, &json_abi)
} else {
serde_json::to_writer_pretty(&file, &json_abi)
};
res.map_err(|e| e.to_string())?;
res.map_err(|e| e)?;
} else if command.minify {
println!("{}", json_abi);
} else {
Expand Down
Loading