Skip to content

Commit

Permalink
feat!: use results on fs module (#191)
Browse files Browse the repository at this point in the history
* feat!: use results on fs module

Refactor the fs module to use results and errors.

Closes #169

* chore: add result to readLine

* chore: use results for metadata

* chore: add empty result

* chore: add results to other fs functions

* chore: add results to the rest of the functions

* chore: export results from test.sol

* chore: add missing lib for metadata result

* chore: export FsMetadataResult from test.sol

* test: update fs tests

* style: run forge fmt

* chore: remove println from fe module

* feat!: use results on `fe.getBytecode`

* chore: review comments

---------

Co-authored-by: gnkz <[email protected]>
  • Loading branch information
gnkz and gnkz authored Sep 12, 2023
1 parent 2dccb82 commit f2998a1
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 92 deletions.
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ libs = ['lib']
ffi = true
fs_permissions = [
{ access = "read", path = "./out"},
{ access = "read", path = "./test/_modules" },
{ access = "read", path = "./test/fixtures/fs/read" },
{ access = "read-write", path = "./test/fixtures/fs/write" },
{ access = "read-write", path = "./test/fixtures/fe/output" }
Expand Down
14 changes: 13 additions & 1 deletion src/_modules/Error.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.13;

import {Pointer} from "./Pointer.sol";
import {LibResultPointer, ResultType} from "./Result.sol";
import {LibResultPointer, ResultType, StringResult, BytesResult, BoolResult} from "./Result.sol";

type Error is bytes32;

Expand Down Expand Up @@ -105,6 +105,18 @@ library LibError {
(,, bytes memory data) = decode(err);
return abi.decode(data, (string));
}

function toStringResult(Error self) internal pure returns (StringResult) {
return StringResult.wrap(Pointer.unwrap(self.toPointer()));
}

function toBytesResult(Error self) internal pure returns (BytesResult) {
return BytesResult.wrap(Pointer.unwrap(self.toPointer()));
}

function toBoolResult(Error self) internal pure returns (BoolResult) {
return BoolResult.wrap(Pointer.unwrap(self.toPointer()));
}
}

using LibError for Error global;
7 changes: 2 additions & 5 deletions src/_modules/Fe.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "./Commands.sol";
import "./Strings.sol";
import "./Fs.sol";
import {formatError} from "../_utils/formatError.sol";
import {BoolResult, BytesResult} from "./Result.sol";

struct Fe {
string compilerPath;
Expand Down Expand Up @@ -80,13 +81,9 @@ library fe {

/// @dev Obtains the bytecode of a compiled contract with `contractName`.
/// @param contractName The name of the contract from which to retrive the bytecode.
function getBytecode(Fe memory self, string memory contractName) internal view returns (bytes memory) {
function getBytecode(Fe memory self, string memory contractName) internal view returns (BytesResult) {
string memory path = string.concat(self.outputDir, "/", contractName, "/", contractName, ".bin");

if (!fs.fileExists(path)) {
revert(_formatError("getBytecode(Fe,string))", "Contract not found"));
}

return fs.readFileBinary(path);
}

Expand Down
Loading

0 comments on commit f2998a1

Please sign in to comment.