Skip to content

Commit

Permalink
refactor: remove result type (#192)
Browse files Browse the repository at this point in the history
* refactor: refactor result module to use pointer type

* style: run forge fmt

* chore: implement LibPointer

* refactor: use pointers on decode

* style: run forge fmt

* chore: rename `asPointer` -> `toPointer`
  • Loading branch information
gnkz authored Sep 12, 2023
1 parent 4e69e1c commit 2dccb82
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 156 deletions.
51 changes: 28 additions & 23 deletions src/_modules/Commands.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pragma solidity >=0.8.13 <0.9.0;

import {VmSafe} from "forge-std/Vm.sol";
import {vulcan} from "./Vulcan.sol";
import {Result, ResultType, Ok} from "./Result.sol";
import {Pointer} from "./Pointer.sol";
import {ResultType, LibResultPointer} from "./Result.sol";
import {LibError, Error} from "./Error.sol";
import {removeSelector} from "../_utils/removeSelector.sol";

Expand Down Expand Up @@ -414,47 +415,51 @@ library CommandError {
}

function toCommandResult(Error self) internal pure returns (CommandResult) {
return CommandResult.wrap(Result.unwrap(self.toResult()));
return CommandResult.wrap(Pointer.unwrap(self.toPointer()));
}
}

library LibCommandOutputPointer {
function asCommandOutput(Pointer self) internal pure returns (CommandOutput memory output) {
bytes32 memoryAddr = self.asBytes32();

assembly {
output := memoryAddr
}
}
}

library LibCommandResult {
using LibCommandOutputPointer for Pointer;

function isOk(CommandResult self) internal pure returns (bool) {
return self.asResult().isOk();
return LibResultPointer.isOk(self.toPointer());
}

function isError(CommandResult self) internal pure returns (bool) {
return self.asResult().isError();
return LibResultPointer.isError(self.toPointer());
}

function unwrap(CommandResult self) internal pure returns (CommandOutput memory val) {
bytes32 _val = self.asResult().unwrap();
assembly {
val := _val
}
function unwrap(CommandResult self) internal pure returns (CommandOutput memory) {
return LibResultPointer.unwrap(self.toPointer()).asCommandOutput();
}

function expect(CommandResult self, string memory err) internal pure returns (CommandOutput memory) {
if (self.isError()) {
revert(err);
}

return self.toValue();
return LibResultPointer.expect(self.toPointer(), err).asCommandOutput();
}

function toError(CommandResult self) internal pure returns (Error) {
return self.asResult().toError();
return LibResultPointer.toError(self.toPointer());
}

function toValue(CommandResult self) internal pure returns (CommandOutput memory val) {
bytes32 _val = self.asResult().toValue();
assembly {
val := _val
}
function toValue(CommandResult self) internal pure returns (CommandOutput memory) {
(, Pointer ptr) = LibResultPointer.decode(self.toPointer());

return ptr.asCommandOutput();
}

function asResult(CommandResult self) internal pure returns (Result) {
return Result.wrap(CommandResult.unwrap(self));
function toPointer(CommandResult self) internal pure returns (Pointer) {
return Pointer.wrap(CommandResult.unwrap(self));
}
}

Expand All @@ -463,7 +468,7 @@ function Ok(CommandOutput memory value) pure returns (CommandResult) {
assembly {
_value := value
}
return CommandResult.wrap(Result.unwrap(Ok(_value)));
return CommandResult.wrap(Pointer.unwrap(ResultType.Ok.encode(_value)));
}

using commands for Command global;
Expand Down
7 changes: 4 additions & 3 deletions src/_modules/Error.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {LibResult, Result, ResultType} from "./Result.sol";
import {Pointer} from "./Pointer.sol";
import {LibResultPointer, ResultType} from "./Result.sol";

type Error is bytes32;

library LibError {
using LibError for *;

function toResult(Error err) internal pure returns (Result) {
return LibResult.encode(ResultType.Error, Error.unwrap(err));
function toPointer(Error err) internal pure returns (Pointer) {
return ResultType.Error.encode(Error.unwrap(err));
}

// Used internally by error functions
Expand Down
46 changes: 25 additions & 21 deletions src/_modules/Json.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.13 <0.9.0;

import {Result, LibResult, Ok} from "./Result.sol";
import {Pointer} from "./Pointer.sol";
import {ResultType, LibResultPointer} from "./Result.sol";
import {LibError, Error} from "./Error.sol";
import "./Accounts.sol";
import "./Vulcan.sol";
Expand All @@ -25,48 +26,51 @@ library JsonError {
}

function toJsonResult(Error self) internal pure returns (JsonResult) {
return JsonResult.wrap(Result.unwrap(self.toResult()));
return JsonResult.wrap(Pointer.unwrap(self.toPointer()));
}
}

library LibJsonObjectPointer {
function asJsonObject(Pointer self) internal pure returns (JsonObject memory obj) {
bytes32 memoryAddr = self.asBytes32();

assembly {
obj := memoryAddr
}
}
}

library LibJsonResult {
using LibJsonObjectPointer for Pointer;

function isOk(JsonResult self) internal pure returns (bool) {
return self.asResult().isOk();
return LibResultPointer.isOk(self.toPointer());
}

function isError(JsonResult self) internal pure returns (bool) {
return self.asResult().isError();
return LibResultPointer.isError(self.toPointer());
}

function unwrap(JsonResult self) internal pure returns (JsonObject memory val) {
bytes32 _val = self.asResult().unwrap();
assembly {
val := _val
}
return LibResultPointer.unwrap(self.toPointer()).asJsonObject();
}

function expect(JsonResult self, string memory err) internal pure returns (JsonObject memory) {
if (self.isError()) {
revert(err);
}

return self.toValue();
return LibResultPointer.expect(self.toPointer(), err).asJsonObject();
}

function toError(JsonResult self) internal pure returns (Error) {
return self.asResult().toError();
return LibResultPointer.toError(self.toPointer());
}

function toValue(JsonResult self) internal pure returns (JsonObject memory val) {
bytes32 _val = self.asResult().toValue();
(, Pointer ptr) = LibResultPointer.decode(self.toPointer());

assembly {
val := _val
}
return ptr.asJsonObject();
}

function asResult(JsonResult self) internal pure returns (Result) {
return Result.wrap(JsonResult.unwrap(self));
function toPointer(JsonResult self) internal pure returns (Pointer) {
return Pointer.wrap(JsonResult.unwrap(self));
}
}

Expand All @@ -75,7 +79,7 @@ function Ok(JsonObject memory value) pure returns (JsonResult) {
assembly {
_value := value
}
return JsonResult.wrap(Result.unwrap(Ok(_value)));
return JsonResult.wrap(Pointer.unwrap(ResultType.Ok.encode(_value)));
}

library json {
Expand Down
44 changes: 44 additions & 0 deletions src/_modules/Pointer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.13 <0.9.0;

type Pointer is bytes32;

library LibPointer {
function asBytes32(Pointer self) internal pure returns (bytes32) {
return Pointer.unwrap(self);
}

function asString(Pointer self) internal pure returns (string memory val) {
bytes32 memoryAddr = self.asBytes32();

assembly {
val := memoryAddr
}
}

function asBytes(Pointer self) internal pure returns (bytes memory val) {
bytes32 memoryAddr = self.asBytes32();

assembly {
val := memoryAddr
}
}

function asBool(Pointer self) internal pure returns (bool val) {
bytes32 memoryAddr = self.asBytes32();

assembly {
val := memoryAddr
}
}

function asUint256(Pointer self) internal pure returns (uint256 val) {
bytes32 memoryAddr = self.asBytes32();

assembly {
val := memoryAddr
}
}
}

using LibPointer for Pointer global;
Loading

0 comments on commit 2dccb82

Please sign in to comment.