Skip to content

Commit

Permalink
ref: improve readability of find_contract
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfertel committed Apr 23, 2024
1 parent 2a22fe5 commit 2ad52c2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
24 changes: 11 additions & 13 deletions src/check/violation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,24 @@ impl ViolationKind {

/// Optionally returns a help text to be used when displaying the violation kind.
pub(crate) fn help(&self) -> Option<Cow<'static, str>> {
match self {
let text = match self {
ViolationKind::ContractMissing(name) => {
let text = format!(r#"consider adding a contract with name "{name}""#);
Some(text.into())
format!(r#"consider adding a contract with name "{name}""#).into()
}
ViolationKind::ContractNameNotMatches(name, _) => {
let text = format!(r#"consider renaming the contract to "{name}""#);
Some(text.into())
format!(r#"consider renaming the contract to "{name}""#).into()
}
ViolationKind::SolidityFileMissing(filename) => {
let filename = filename.replace(".t.sol", ".tree");
let text = format!("consider running `bulloak scaffold {filename}`");
Some(text.into())
format!("consider running `bulloak scaffold {filename}`").into()
}
ViolationKind::FunctionOrderMismatch(_, _, _) => {
let text = "consider reordering the function in the file";
Some(text.into())
"consider reordering the function in the file".into()
}
_ => None,
}
_ => return None,
};

Some(text)
}

pub(crate) fn fix(&self, mut ctx: Context) -> Context {
Expand All @@ -190,15 +188,15 @@ impl ViolationKind {
let pt =
sol::Translator::new(INTERNAL_DEFAULT_SOL_VERSION, false).translate(&ctx.hir);
let source = sol::Formatter::new().emit(pt.clone());
let parsed = parse(&source).expect("should parse solidity string");
let parsed = parse(&source).expect("should parse Solidity string");
ctx.from_parsed(parsed)
}
ViolationKind::ContractNameNotMatches(new_name, old_name) => {
let source = ctx.src.replace(
&format!("contract {old_name}"),
&format!("contract {new_name}"),
);
let parsed = parse(&source).expect("should parse solidity string");
let parsed = parse(&source).expect("should parse Solidity string");
ctx.from_parsed(parsed)
}
// Assume order violations have been taken care of first.
Expand Down
9 changes: 3 additions & 6 deletions src/hir/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,9 @@ pub struct Root {

impl Root {
pub(crate) fn find_contract(&self) -> Option<&ContractDefinition> {
self.children.iter().find_map(|child| {
if let Hir::ContractDefinition(contract) = child {
Some(contract)
} else {
None
}
self.children.iter().find_map(|child| match child {
Hir::ContractDefinition(contract) => Some(contract),
_ => None,
})
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/sol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ pub(crate) use translator::Translator;

/// Searches for and returns the first `ContractDefinition` found in a given `SourceUnit`.
pub(crate) fn find_contract(pt: &SourceUnit) -> Option<Box<ContractDefinition>> {
pt.0.iter().find_map(|part| {
if let SourceUnitPart::ContractDefinition(contract) = part {
Some(contract.clone())
} else {
None
}
pt.0.iter().find_map(|part| match part {
SourceUnitPart::ContractDefinition(contract) => Some(contract.clone()),
_ => None,
})
}

Expand Down

0 comments on commit 2ad52c2

Please sign in to comment.