|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity >=0.6.0 <0.9.0; |
| 3 | + |
| 4 | +pragma experimental ABIEncoderV2; |
| 5 | + |
| 6 | +import {VmSafe} from "./Vm.sol"; |
| 7 | + |
| 8 | +// Helpers for parsing and writing TOML files |
| 9 | +// To parse: |
| 10 | +// ``` |
| 11 | +// using stdToml for string; |
| 12 | +// string memory toml = vm.readFile("<some_path>"); |
| 13 | +// toml.readUint("<json_path>"); |
| 14 | +// ``` |
| 15 | +// To write: |
| 16 | +// ``` |
| 17 | +// using stdToml for string; |
| 18 | +// string memory json = "json"; |
| 19 | +// json.serialize("a", uint256(123)); |
| 20 | +// string memory semiFinal = json.serialize("b", string("test")); |
| 21 | +// string memory finalJson = json.serialize("c", semiFinal); |
| 22 | +// finalJson.write("<some_path>"); |
| 23 | +// ``` |
| 24 | + |
| 25 | +library stdToml { |
| 26 | + VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code"))))); |
| 27 | + |
| 28 | + function parseRaw(string memory toml, string memory key) internal pure returns (bytes memory) { |
| 29 | + return vm.parseToml(toml, key); |
| 30 | + } |
| 31 | + |
| 32 | + function readUint(string memory toml, string memory key) internal pure returns (uint256) { |
| 33 | + return vm.parseTomlUint(toml, key); |
| 34 | + } |
| 35 | + |
| 36 | + function readUintArray(string memory toml, string memory key) internal pure returns (uint256[] memory) { |
| 37 | + return vm.parseTomlUintArray(toml, key); |
| 38 | + } |
| 39 | + |
| 40 | + function readInt(string memory toml, string memory key) internal pure returns (int256) { |
| 41 | + return vm.parseTomlInt(toml, key); |
| 42 | + } |
| 43 | + |
| 44 | + function readIntArray(string memory toml, string memory key) internal pure returns (int256[] memory) { |
| 45 | + return vm.parseTomlIntArray(toml, key); |
| 46 | + } |
| 47 | + |
| 48 | + function readBytes32(string memory toml, string memory key) internal pure returns (bytes32) { |
| 49 | + return vm.parseTomlBytes32(toml, key); |
| 50 | + } |
| 51 | + |
| 52 | + function readBytes32Array(string memory toml, string memory key) internal pure returns (bytes32[] memory) { |
| 53 | + return vm.parseTomlBytes32Array(toml, key); |
| 54 | + } |
| 55 | + |
| 56 | + function readString(string memory toml, string memory key) internal pure returns (string memory) { |
| 57 | + return vm.parseTomlString(toml, key); |
| 58 | + } |
| 59 | + |
| 60 | + function readStringArray(string memory toml, string memory key) internal pure returns (string[] memory) { |
| 61 | + return vm.parseTomlStringArray(toml, key); |
| 62 | + } |
| 63 | + |
| 64 | + function readAddress(string memory toml, string memory key) internal pure returns (address) { |
| 65 | + return vm.parseTomlAddress(toml, key); |
| 66 | + } |
| 67 | + |
| 68 | + function readAddressArray(string memory toml, string memory key) internal pure returns (address[] memory) { |
| 69 | + return vm.parseTomlAddressArray(toml, key); |
| 70 | + } |
| 71 | + |
| 72 | + function readBool(string memory toml, string memory key) internal pure returns (bool) { |
| 73 | + return vm.parseTomlBool(toml, key); |
| 74 | + } |
| 75 | + |
| 76 | + function readBoolArray(string memory toml, string memory key) internal pure returns (bool[] memory) { |
| 77 | + return vm.parseTomlBoolArray(toml, key); |
| 78 | + } |
| 79 | + |
| 80 | + function readBytes(string memory toml, string memory key) internal pure returns (bytes memory) { |
| 81 | + return vm.parseTomlBytes(toml, key); |
| 82 | + } |
| 83 | + |
| 84 | + function readBytesArray(string memory toml, string memory key) internal pure returns (bytes[] memory) { |
| 85 | + return vm.parseTomlBytesArray(toml, key); |
| 86 | + } |
| 87 | + |
| 88 | + function serialize(string memory jsonKey, string memory rootObject) internal returns (string memory) { |
| 89 | + return vm.serializeJson(jsonKey, rootObject); |
| 90 | + } |
| 91 | + |
| 92 | + function serialize(string memory jsonKey, string memory key, bool value) internal returns (string memory) { |
| 93 | + return vm.serializeBool(jsonKey, key, value); |
| 94 | + } |
| 95 | + |
| 96 | + function serialize(string memory jsonKey, string memory key, bool[] memory value) |
| 97 | + internal |
| 98 | + returns (string memory) |
| 99 | + { |
| 100 | + return vm.serializeBool(jsonKey, key, value); |
| 101 | + } |
| 102 | + |
| 103 | + function serialize(string memory jsonKey, string memory key, uint256 value) internal returns (string memory) { |
| 104 | + return vm.serializeUint(jsonKey, key, value); |
| 105 | + } |
| 106 | + |
| 107 | + function serialize(string memory jsonKey, string memory key, uint256[] memory value) |
| 108 | + internal |
| 109 | + returns (string memory) |
| 110 | + { |
| 111 | + return vm.serializeUint(jsonKey, key, value); |
| 112 | + } |
| 113 | + |
| 114 | + function serialize(string memory jsonKey, string memory key, int256 value) internal returns (string memory) { |
| 115 | + return vm.serializeInt(jsonKey, key, value); |
| 116 | + } |
| 117 | + |
| 118 | + function serialize(string memory jsonKey, string memory key, int256[] memory value) |
| 119 | + internal |
| 120 | + returns (string memory) |
| 121 | + { |
| 122 | + return vm.serializeInt(jsonKey, key, value); |
| 123 | + } |
| 124 | + |
| 125 | + function serialize(string memory jsonKey, string memory key, address value) internal returns (string memory) { |
| 126 | + return vm.serializeAddress(jsonKey, key, value); |
| 127 | + } |
| 128 | + |
| 129 | + function serialize(string memory jsonKey, string memory key, address[] memory value) |
| 130 | + internal |
| 131 | + returns (string memory) |
| 132 | + { |
| 133 | + return vm.serializeAddress(jsonKey, key, value); |
| 134 | + } |
| 135 | + |
| 136 | + function serialize(string memory jsonKey, string memory key, bytes32 value) internal returns (string memory) { |
| 137 | + return vm.serializeBytes32(jsonKey, key, value); |
| 138 | + } |
| 139 | + |
| 140 | + function serialize(string memory jsonKey, string memory key, bytes32[] memory value) |
| 141 | + internal |
| 142 | + returns (string memory) |
| 143 | + { |
| 144 | + return vm.serializeBytes32(jsonKey, key, value); |
| 145 | + } |
| 146 | + |
| 147 | + function serialize(string memory jsonKey, string memory key, bytes memory value) internal returns (string memory) { |
| 148 | + return vm.serializeBytes(jsonKey, key, value); |
| 149 | + } |
| 150 | + |
| 151 | + function serialize(string memory jsonKey, string memory key, bytes[] memory value) |
| 152 | + internal |
| 153 | + returns (string memory) |
| 154 | + { |
| 155 | + return vm.serializeBytes(jsonKey, key, value); |
| 156 | + } |
| 157 | + |
| 158 | + function serialize(string memory jsonKey, string memory key, string memory value) |
| 159 | + internal |
| 160 | + returns (string memory) |
| 161 | + { |
| 162 | + return vm.serializeString(jsonKey, key, value); |
| 163 | + } |
| 164 | + |
| 165 | + function serialize(string memory jsonKey, string memory key, string[] memory value) |
| 166 | + internal |
| 167 | + returns (string memory) |
| 168 | + { |
| 169 | + return vm.serializeString(jsonKey, key, value); |
| 170 | + } |
| 171 | + |
| 172 | + function write(string memory jsonKey, string memory path) internal { |
| 173 | + vm.writeToml(jsonKey, path); |
| 174 | + } |
| 175 | + |
| 176 | + function write(string memory jsonKey, string memory path, string memory valueKey) internal { |
| 177 | + vm.writeToml(jsonKey, path, valueKey); |
| 178 | + } |
| 179 | +} |
0 commit comments