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

feat: request module #174

Merged
merged 1 commit into from
Sep 2, 2023
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ See the [Vulcan Book](https://nomoixyz.github.io/vulcan/) for detailed usage inf
## Why Vulcan?

Our goal is to provide:

- Better naming for VM functionality (no more `prank`, `roll`, `warp`, ...)
- A testing framework with better readability and a familiar syntax
- Improved ergonomics
Expand Down Expand Up @@ -84,3 +84,10 @@ contract TestSomething is Test {
## Contributing

At this stage we are looking for all kinds of feedback, as the general direction of the project is not fully defined yet. If you have any ideas to improve naming, ergonomics, or anything else, please open an issue or a PR.

## Acknowledgments

Some of the ideas and code in Vulcan are directly inspired by or adapted from the following projects:

- [forge-std](https://github.com/foundry-rs/forge-std/)
- [memester-xyz/surl](https://github.com/memester-xyz/surl)
290 changes: 191 additions & 99 deletions src/_modules/Json.sol

Large diffs are not rendered by default.

97 changes: 97 additions & 0 deletions src/_modules/Result.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.13 <0.9.0;

struct Error {
bytes32 id;
string message;
}

enum ResultType {
Error,
Ok
}

struct Result {
ResultType _type;
bytes _data;
}

struct StringResult {
Result _inner;
}

library LibResult {
function isError(Result memory self) internal pure returns (bool) {
return self._type == ResultType.Error;
}

function isOk(Result memory self) internal pure returns (bool) {
return self._type == ResultType.Ok;
}

function toError(Result memory self) internal pure returns (Error memory) {
return abi.decode(self._data, (Error));
}

function toValue(Result memory self) internal pure returns (bytes memory) {
return self._data;
}

function unwrap(Result memory self) internal pure returns (bytes memory) {
if (self.isError()) {
revert(self.toError().message);
}

return self._data;
}

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

return self._data;
}

function toResult(Error memory error) internal pure returns (Result memory) {
return Result({_type: ResultType.Error, _data: abi.encode(error)});
}
}

library LibStringResult {
function isOk(StringResult memory self) internal pure returns (bool) {
return self._inner.isOk();
}

function isError(StringResult memory self) internal pure returns (bool) {
return self._inner.isError();
}

function unwrap(StringResult memory self) internal pure returns (string memory) {
return string(self._inner.unwrap());
}

function expect(StringResult memory self, string memory err) internal pure returns (string memory) {
return string(self._inner.expect(err));
}

function toError(StringResult memory self) internal pure returns (Error memory) {
return self._inner.toError();
}

function toValue(StringResult memory self) internal pure returns (string memory) {
return string(self._inner.toValue());
}
}

function Ok(bytes memory value) pure returns (Result memory) {
return Result({_type: ResultType.Ok, _data: value});
}

function Ok(string memory value) pure returns (StringResult memory) {
return StringResult(Ok(bytes(value)));
}

using LibStringResult for StringResult global;
using LibResult for Result global;
using LibResult for Error global;
Loading