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

ref(check): use the thiserror crate #73

Merged
merged 2 commits into from
Jul 7, 2024
Merged
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
99 changes: 37 additions & 62 deletions src/check/violation.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Defines a rule-checking error object.

use std::{borrow::Cow, collections::HashSet, fmt};

use forge_fmt::{
Expand All @@ -11,6 +10,7 @@
pt,
pt::{ContractDefinition, ContractPart},
};
use thiserror::Error;

use super::{context::Context, location::Location};
use crate::{
Expand All @@ -22,9 +22,10 @@

/// An error that occurred while checking specification rules between
/// a tree and a Solidity contract.
#[derive(Debug)]
#[derive(Debug, Error)]

Check warning on line 25 in src/check/violation.rs

View check run for this annotation

Codecov / codecov/patch

src/check/violation.rs#L25

Added line #L25 was not covered by tests
pub(crate) struct Violation {
/// The kind of violation.
#[source]
pub(crate) kind: ViolationKind,
/// The location information about this violation.
pub(crate) location: Location,
Expand All @@ -48,31 +49,44 @@
/// NOTE: Adding a variant to this enum most certainly will mean adding a
/// variant to the `Rules` section of `bulloak`'s README. Please, do not forget
/// to add it if you are implementing a rule.
#[derive(Debug)]
#[derive(Debug, Error)]
#[non_exhaustive]
pub(crate) enum ViolationKind {
/// Found no matching Solidity contract.
///
/// (contract name)
#[error("contract \"{0}\" is missing in .sol")]
ContractMissing(String),

/// Contract name doesn't match.
///
/// (tree name, sol name)
#[error("contract \"{0}\" is missing in .sol -- found \"{1}\" instead")]
ContractNameNotMatches(String, String),

/// The corresponding Solidity file does not exist.
#[error("the tree is missing its matching Solidity file: {0}")]
SolidityFileMissing(String),

/// Couldn't read the corresponding Solidity file.
#[error("bulloak couldn't read the file")]
FileUnreadable,

/// Found an incorrectly ordered element.
///
/// (pt function, current position, insertion position)
#[error("incorrect position for function `{}`", .0.name.safe_unwrap())]
FunctionOrderMismatch(pt::FunctionDefinition, usize, usize),

/// Found a tree element without its matching codegen.
///
/// (hir function, insertion position)
#[error("function \"{}\" is missing in .sol", .0.identifier.clone())]
MatchingFunctionMissing(hir::FunctionDefinition, usize),

/// The parsing of a tree or a Solidity file failed.
ParsingFailed(anyhow::Error),
#[error("{}", format_frontend_error(.0))]
ParsingFailed(#[from] anyhow::Error),
}

impl fmt::Display for Violation {
Expand All @@ -92,66 +106,27 @@
}
}

impl fmt::Display for ViolationKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::ViolationKind::{
ContractMissing, ContractNameNotMatches, FileUnreadable,
FunctionOrderMismatch, MatchingFunctionMissing, ParsingFailed,
SolidityFileMissing,
};
match self {
ContractMissing(contract) => {
write!(f, r#"contract "{contract}" is missing in .sol"#)
}
ContractNameNotMatches(tree_name, sol_name) => write!(
f,
r#"contract "{tree_name}" is missing in .sol -- found "{sol_name}" instead"#
fn format_frontend_error(error: &anyhow::Error) -> String {
if let Some(error) = error.downcast_ref::<error::Error>() {
match error {
error::Error::Tokenize(error) => format!(
"an error occurred while parsing the tree: {}",
error.kind()
),

Check warning on line 115 in src/check/violation.rs

View check run for this annotation

Codecov / codecov/patch

src/check/violation.rs#L112-L115

Added lines #L112 - L115 were not covered by tests
error::Error::Parse(error) => format!(
"an error occurred while parsing the tree: {}",
error.kind()
),
error::Error::Combine(error) => format!(
"an error occurred while parsing the tree: {}",
error.kind()
),
error::Error::Semantic(_) => format!(
"at least one semantic error occurred while parsing the tree"

Check warning on line 125 in src/check/violation.rs

View check run for this annotation

Codecov / codecov/patch

src/check/violation.rs#L124-L125

Added lines #L124 - L125 were not covered by tests
),

Check warning on line 126 in src/check/violation.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

warning: useless use of `format!` --> src/check/violation.rs:124:42 | 124 | error::Error::Semantic(_) => format!( | __________________________________________^ 125 | | "at least one semantic error occurred while parsing the tree" 126 | | ), | |_____________^ help: consider using `.to_string()`: `"at least one semantic error occurred while parsing the tree".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `#[warn(clippy::useless_format)]` implied by `#[warn(clippy::all)]`
SolidityFileMissing(_) => {
write!(f, "the tree is missing its matching Solidity file")
}
FileUnreadable => {
write!(f, "bulloak couldn't read the file")
}
FunctionOrderMismatch(fn_sol, _, _) => {
let name = fn_sol.name.safe_unwrap();
write!(f, r#"incorrect position for function `{name}`"#)
}
MatchingFunctionMissing(fn_hir, _) => {
let name = fn_hir.identifier.clone();
write!(f, r#"function "{name}" is missing in .sol"#)
}
ParsingFailed(error) => {
if let Some(error) = error.downcast_ref::<error::Error>() {
match error {
error::Error::Tokenize(error) => write!(
f,
"an error occurred while parsing the tree: {}",
error.kind()
),
error::Error::Parse(error) => write!(
f,
"an error occurred while parsing the tree: {}",
error.kind()
),
error::Error::Combine(error) => write!(
f,
"an error occurred while parsing the tree: {}",
error.kind()
),
error::Error::Semantic(_) => write!(
f,
"at least one semantic error occurred while parsing the tree"
),
}
} else {
write!(
f,
"an error occurred while parsing the solidity file"
)
}
}
}
} else {
format!("an error occurred while parsing the solidity file")

Check warning on line 129 in src/check/violation.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

warning: useless use of `format!` --> src/check/violation.rs:129:9 | 129 | format!("an error occurred while parsing the solidity file") | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"an error occurred while parsing the solidity file".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format

Check warning on line 129 in src/check/violation.rs

View check run for this annotation

Codecov / codecov/patch

src/check/violation.rs#L129

Added line #L129 was not covered by tests
}
}

Expand Down Expand Up @@ -343,7 +318,7 @@
/// This is a safeguard to ensure the output is always a valid Solidity code.
pub(crate) fn fix_order(
violations: &[Violation],
contract_sol: &Box<ContractDefinition>,

Check warning on line 321 in src/check/violation.rs

View workflow job for this annotation

GitHub Actions / clippy

you seem to be trying to use `&Box<T>`. Consider using just `&T`

warning: you seem to be trying to use `&Box<T>`. Consider using just `&T` --> src/check/violation.rs:321:19 | 321 | contract_sol: &Box<ContractDefinition>, | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&ContractDefinition` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box = note: `#[warn(clippy::borrowed_box)]` implied by `#[warn(clippy::all)]`
contract_hir: &hir::ContractDefinition,
ctx: Context,
) -> Context {
Expand Down
Loading