{"id":"xonae/result","name":"result","scope":"xonae","platform":"roblox","description":"Lightweight functional-style Result implementation","version":"1.0.0","latest":"1.0.0","versions":["1.0.0"],"license":"MIT","licenseRating":"safe","licenseCaveats":[],"licenseVerified":true,"dependencies":{},"integrity":"10ea82c7e739322e94b542d6759218762c549ccaf902e30fc67d8300079b5eb0","likes":0,"downloads":0,"install":"forest install xonae/result","url":"https://forest.dev/p/roblox/xonae/result","files":"https://api.forest.dev/ai/package/roblox/xonae/result/files","readme":"# Result\r\n\r\nA lightweight functional-style Result implementation based off of Rust's Result type.\r\n\r\nInspired by [this amazing port](https://github.com/nightcycle/result) from Nightcycle. My variant was made to leverage the functional-style paradigm that I find myself using more and more these days.\r\n\r\n## Usage\r\n\r\n```luau\r\n-- create an ok result\r\nlocal ok = Result.ok(10)\r\n\r\n-- create an err result\r\nlocal err = Result.err(\"not a number\")\r\n\r\n-- check if a result is ok\r\nlocal isOk = Result.isOk(ok)\r\n\r\n-- check if a result is err\r\nlocal isErr = Result.isErr(err)\r\n\r\n-- get the ok value. throws the err value if the result is err\r\nlocal value = Result.unwrap(ok)\r\n\r\n-- get the err value. throws if the result is ok\r\nlocal reason = Result.unwrapErr(err)\r\n\r\n-- get the ok value, or a fallback if the result is err\r\nlocal valueOrZero = Result.unwrapOr(err, 0)\r\n\r\n-- get the ok value, or compute a fallback from the err value\r\nlocal valueOrLength = Result.unwrapOrElse(err, function (message: string)\r\n    return #message\r\nend)\r\n\r\n-- get the ok value, throws \"amount was invalid: not a number\" if the result is err\r\nlocal expected = Result.expect(ok, \"amount was invalid\")\r\n\r\n-- get the ok value, or nil if the result is err\r\nlocal maybeValue = Result.asNullable(err)\r\n\r\n-- run one callback for ok and another for err, then get the output\r\nlocal text = Result.match(ok, function (amount: number)\r\n    return `got {amount}`\r\nend, function (message: string)\r\n    return `failed: {message}`\r\nend)\r\n\r\n-- run a callback on the ok value, if any\r\nResult.inspect(ok, function (amount: number)\r\n    print(amount)\r\nend)\r\n\r\n-- run a callback on the err value, if any\r\nResult.inspectErr(err, function (message: string)\r\n    print(message)\r\nend)\r\n\r\n-- change the ok value. an err result passes through\r\nlocal doubled = Result.map(ok, function (amount: number)\r\n    return amount * 2\r\nend)\r\n\r\n-- change the err value. an ok result passes through\r\nlocal detailed = Result.mapErr(err, function (message: string)\r\n    return `parse failed: {message}`\r\nend)\r\n\r\n-- run a step that can fail on the ok value. the output type can change\r\nlocal asText = Result.andThen(ok, function (amount: number)\r\n    return if amount > 0 then Result.ok(tostring(amount)) else Result.err(\"not positive\")\r\nend)\r\n\r\n-- run many steps that can fail. stops at the first err\r\nlocal checked = Result.chain(ok, function (amount: number)\r\n    return if amount > 0 then Result.ok(amount) else Result.err(\"not positive\")\r\nend, function (amount: number)\r\n    return if amount % 1 == 0 then Result.ok(amount) else Result.err(\"not whole\")\r\nend)\r\n```\r\n\r\n## Installation\r\n\r\n### Wally\r\n\r\n```toml\r\n[dependencies]\r\nResult = \"xonae/result@1.0.0\"\r\n```\r\n\r\n### Everything Else\r\n\r\nYou can also manually copy the source from `src/init.luau` into a ModuleScript or file.\r\n","readmeTruncated":false}