| 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 keyExists(string memory toml, string memory key) internal view returns (bool) { |
| 29 | return vm.keyExistsToml(toml, key); |
| 30 | } |
| 31 | |
| 32 | function parseRaw(string memory toml, string memory key) internal pure returns (bytes memory) { |
| 33 | return vm.parseToml(toml, key); |
| 34 | } |
| 35 | |
| 36 | function readUint(string memory toml, string memory key) internal pure returns (uint256) { |
| 37 | return vm.parseTomlUint(toml, key); |
| 38 | } |
| 39 | |
| 40 | function readUintArray(string memory toml, string memory key) internal pure returns (uint256[] memory) { |
| 41 | return vm.parseTomlUintArray(toml, key); |
| 42 | } |
| 43 | |
| 44 | function readInt(string memory toml, string memory key) internal pure returns (int256) { |
| 45 | return vm.parseTomlInt(toml, key); |
| 46 | } |
| 47 | |
| 48 | function readIntArray(string memory toml, string memory key) internal pure returns (int256[] memory) { |
| 49 | return vm.parseTomlIntArray(toml, key); |
| 50 | } |
| 51 | |
| 52 | function readBytes32(string memory toml, string memory key) internal pure returns (bytes32) { |
| 53 | return vm.parseTomlBytes32(toml, key); |
| 54 | } |
| 55 | |
| 56 | function readBytes32Array(string memory toml, string memory key) internal pure returns (bytes32[] memory) { |
| 57 | return vm.parseTomlBytes32Array(toml, key); |
| 58 | } |
| 59 | |
| 60 | function readString(string memory toml, string memory key) internal pure returns (string memory) { |
| 61 | return vm.parseTomlString(toml, key); |
| 62 | } |
| 63 | |
| 64 | function readStringArray(string memory toml, string memory key) internal pure returns (string[] memory) { |
| 65 | return vm.parseTomlStringArray(toml, key); |
| 66 | } |
| 67 | |
| 68 | function readAddress(string memory toml, string memory key) internal pure returns (address) { |
| 69 | return vm.parseTomlAddress(toml, key); |
| 70 | } |
| 71 | |
| 72 | function readAddressArray(string memory toml, string memory key) internal pure returns (address[] memory) { |
| 73 | return vm.parseTomlAddressArray(toml, key); |
| 74 | } |
| 75 | |
| 76 | function readBool(string memory toml, string memory key) internal pure returns (bool) { |
| 77 | return vm.parseTomlBool(toml, key); |
| 78 | } |
| 79 | |
| 80 | function readBoolArray(string memory toml, string memory key) internal pure returns (bool[] memory) { |
| 81 | return vm.parseTomlBoolArray(toml, key); |
| 82 | } |
| 83 | |
| 84 | function readBytes(string memory toml, string memory key) internal pure returns (bytes memory) { |
| 85 | return vm.parseTomlBytes(toml, key); |
| 86 | } |
| 87 | |
| 88 | function readBytesArray(string memory toml, string memory key) internal pure returns (bytes[] memory) { |
| 89 | return vm.parseTomlBytesArray(toml, key); |
| 90 | } |
| 91 | |
| 92 | function readUintOr(string memory toml, string memory key, uint256 defaultValue) internal view returns (uint256) { |
| 93 | return keyExists(toml, key) ? readUint(toml, key) : defaultValue; |
| 94 | } |
| 95 | |
| 96 | function readUintArrayOr(string memory toml, string memory key, uint256[] memory defaultValue) |
| 97 | internal |
| 98 | view |
| 99 | returns (uint256[] memory) |
| 100 | { |
| 101 | return keyExists(toml, key) ? readUintArray(toml, key) : defaultValue; |
| 102 | } |
| 103 | |
| 104 | function readIntOr(string memory toml, string memory key, int256 defaultValue) internal view returns (int256) { |
| 105 | return keyExists(toml, key) ? readInt(toml, key) : defaultValue; |
| 106 | } |
| 107 | |
| 108 | function readIntArrayOr(string memory toml, string memory key, int256[] memory defaultValue) |
| 109 | internal |
| 110 | view |
| 111 | returns (int256[] memory) |
| 112 | { |
| 113 | return keyExists(toml, key) ? readIntArray(toml, key) : defaultValue; |
| 114 | } |
| 115 | |
| 116 | function readBytes32Or(string memory toml, string memory key, bytes32 defaultValue) |
| 117 | internal |
| 118 | view |
| 119 | returns (bytes32) |
| 120 | { |
| 121 | return keyExists(toml, key) ? readBytes32(toml, key) : defaultValue; |
| 122 | } |
| 123 | |
| 124 | function readBytes32ArrayOr(string memory toml, string memory key, bytes32[] memory defaultValue) |
| 125 | internal |
| 126 | view |
| 127 | returns (bytes32[] memory) |
| 128 | { |
| 129 | return keyExists(toml, key) ? readBytes32Array(toml, key) : defaultValue; |
| 130 | } |
| 131 | |
| 132 | function readStringOr(string memory toml, string memory key, string memory defaultValue) |
| 133 | internal |
| 134 | view |
| 135 | returns (string memory) |
| 136 | { |
| 137 | return keyExists(toml, key) ? readString(toml, key) : defaultValue; |
| 138 | } |
| 139 | |
| 140 | function readStringArrayOr(string memory toml, string memory key, string[] memory defaultValue) |
| 141 | internal |
| 142 | view |
| 143 | returns (string[] memory) |
| 144 | { |
| 145 | return keyExists(toml, key) ? readStringArray(toml, key) : defaultValue; |
| 146 | } |
| 147 | |
| 148 | function readAddressOr(string memory toml, string memory key, address defaultValue) |
| 149 | internal |
| 150 | view |
| 151 | returns (address) |
| 152 | { |
| 153 | return keyExists(toml, key) ? readAddress(toml, key) : defaultValue; |
| 154 | } |
| 155 | |
| 156 | function readAddressArrayOr(string memory toml, string memory key, address[] memory defaultValue) |
| 157 | internal |
| 158 | view |
| 159 | returns (address[] memory) |
| 160 | { |
| 161 | return keyExists(toml, key) ? readAddressArray(toml, key) : defaultValue; |
| 162 | } |
| 163 | |
| 164 | function readBoolOr(string memory toml, string memory key, bool defaultValue) internal view returns (bool) { |
| 165 | return keyExists(toml, key) ? readBool(toml, key) : defaultValue; |
| 166 | } |
| 167 | |
| 168 | function readBoolArrayOr(string memory toml, string memory key, bool[] memory defaultValue) |
| 169 | internal |
| 170 | view |
| 171 | returns (bool[] memory) |
| 172 | { |
| 173 | return keyExists(toml, key) ? readBoolArray(toml, key) : defaultValue; |
| 174 | } |
| 175 | |
| 176 | function readBytesOr(string memory toml, string memory key, bytes memory defaultValue) |
| 177 | internal |
| 178 | view |
| 179 | returns (bytes memory) |
| 180 | { |
| 181 | return keyExists(toml, key) ? readBytes(toml, key) : defaultValue; |
| 182 | } |
| 183 | |
| 184 | function readBytesArrayOr(string memory toml, string memory key, bytes[] memory defaultValue) |
| 185 | internal |
| 186 | view |
| 187 | returns (bytes[] memory) |
| 188 | { |
| 189 | return keyExists(toml, key) ? readBytesArray(toml, key) : defaultValue; |
| 190 | } |
| 191 | |
| 192 | function serialize(string memory jsonKey, string memory rootObject) internal returns (string memory) { |
| 193 | return vm.serializeJson(jsonKey, rootObject); |
| 194 | } |
| 195 | |
| 196 | function serialize(string memory jsonKey, string memory key, bool value) internal returns (string memory) { |
| 197 | return vm.serializeBool(jsonKey, key, value); |
| 198 | } |
| 199 | |
| 200 | function serialize(string memory jsonKey, string memory key, bool[] memory value) |
| 201 | internal |
| 202 | returns (string memory) |
| 203 | { |
| 204 | return vm.serializeBool(jsonKey, key, value); |
| 205 | } |
| 206 | |
| 207 | function serialize(string memory jsonKey, string memory key, uint256 value) internal returns (string memory) { |
| 208 | return vm.serializeUint(jsonKey, key, value); |
| 209 | } |
| 210 | |
| 211 | function serialize(string memory jsonKey, string memory key, uint256[] memory value) |
| 212 | internal |
| 213 | returns (string memory) |
| 214 | { |
| 215 | return vm.serializeUint(jsonKey, key, value); |
| 216 | } |
| 217 | |
| 218 | function serialize(string memory jsonKey, string memory key, int256 value) internal returns (string memory) { |
| 219 | return vm.serializeInt(jsonKey, key, value); |
| 220 | } |
| 221 | |
| 222 | function serialize(string memory jsonKey, string memory key, int256[] memory value) |
| 223 | internal |
| 224 | returns (string memory) |
| 225 | { |
| 226 | return vm.serializeInt(jsonKey, key, value); |
| 227 | } |
| 228 | |
| 229 | function serialize(string memory jsonKey, string memory key, address value) internal returns (string memory) { |
| 230 | return vm.serializeAddress(jsonKey, key, value); |
| 231 | } |
| 232 | |
| 233 | function serialize(string memory jsonKey, string memory key, address[] memory value) |
| 234 | internal |
| 235 | returns (string memory) |
| 236 | { |
| 237 | return vm.serializeAddress(jsonKey, key, value); |
| 238 | } |
| 239 | |
| 240 | function serialize(string memory jsonKey, string memory key, bytes32 value) internal returns (string memory) { |
| 241 | return vm.serializeBytes32(jsonKey, key, value); |
| 242 | } |
| 243 | |
| 244 | function serialize(string memory jsonKey, string memory key, bytes32[] memory value) |
| 245 | internal |
| 246 | returns (string memory) |
| 247 | { |
| 248 | return vm.serializeBytes32(jsonKey, key, value); |
| 249 | } |
| 250 | |
| 251 | function serialize(string memory jsonKey, string memory key, bytes memory value) internal returns (string memory) { |
| 252 | return vm.serializeBytes(jsonKey, key, value); |
| 253 | } |
| 254 | |
| 255 | function serialize(string memory jsonKey, string memory key, bytes[] memory value) |
| 256 | internal |
| 257 | returns (string memory) |
| 258 | { |
| 259 | return vm.serializeBytes(jsonKey, key, value); |
| 260 | } |
| 261 | |
| 262 | function serialize(string memory jsonKey, string memory key, string memory value) |
| 263 | internal |
| 264 | returns (string memory) |
| 265 | { |
| 266 | return vm.serializeString(jsonKey, key, value); |
| 267 | } |
| 268 | |
| 269 | function serialize(string memory jsonKey, string memory key, string[] memory value) |
| 270 | internal |
| 271 | returns (string memory) |
| 272 | { |
| 273 | return vm.serializeString(jsonKey, key, value); |
| 274 | } |
| 275 | |
| 276 | function write(string memory jsonKey, string memory path) internal { |
| 277 | vm.writeToml(jsonKey, path); |
| 278 | } |
| 279 | |
| 280 | function write(string memory jsonKey, string memory path, string memory valueKey) internal { |
| 281 | vm.writeToml(jsonKey, path, valueKey); |
| 282 | } |
| 283 | } |