Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bosunski committed Jan 14, 2024
1 parent 32d7eaf commit ed6d068
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Results [WIP]

Results is a PHP library that provides a set of helper functions and classes for handling optional values and results of operations. It is inspired by the [`Option`](https://doc.rust-lang.org/std/option/enum.Option.html) and [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) types in Rust. The implementation itself is based on the TypeScript implementation called [`ts-results`](https://github.com/vultix/ts-results).
Results is a PHP simple (dependency-free) library that provides a set of helper functions and classes for handling optional values and results of operations.
It is inspired by the [`Option`](https://doc.rust-lang.org/std/option/enum.Option.html) and [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) types in Rust. The implementation itself is based on the TypeScript implementation called [`ts-results`](https://github.com/vultix/ts-results).

## Installation

You can install the library via Composer:
You can install the library through Composer:

```bash
composer require bosunski/results
Expand All @@ -17,6 +18,8 @@ composer require bosunski/results
The `Option` type represents an optional value: every `Option` is either `Some` and contains a value, or `None`, and does not.

```php
<?php

use function Bosunski\Results\Option;

$some = Option('value'); // Some
Expand All @@ -28,6 +31,8 @@ $none = Option(null); // None
The `Result` type is a type that represents either success (`Ok`) or failure (`Err`).

```php
<?php

use function Bosunski\Results\Result;

$ok = Result('value'); // Ok
Expand All @@ -51,6 +56,8 @@ The library provides a set of helper functions for creating `Option` and `Result
Optional values and results in PHP can be represented using the `Option` and `Result` types provided by the Results library. Here are some examples:

```php
<?php

use function Bosunski\Results\Option;
use function Bosunski\Results\Result;

Expand All @@ -76,6 +83,8 @@ Let's dive into more complex examples of using the `Option` and `Result` types i
Consider a scenario where we have a function that may or may not return a value. We can use the `Option` type to handle this uncertainty.

```php
<?php

use Bosunski\Results\Option as OptionInterface;
use function Bosunski\Results\Option;

Expand All @@ -95,11 +104,16 @@ if ($userOption->isSome()) {
} else {
// Handle the case where no user was found
}

// You can also do this
$user = $userOption->unwrap() // Throws error if null
```

Now, let's consider a scenario where we have a function that can either succeed or fail. We can use the `Result` type to handle this.

```php
<?php

use Bosunski\Results\Result\Result as ResultInterface;
use function Bosunski\Results\Result;

Expand All @@ -121,6 +135,9 @@ if ($result->isOk()) {
$error = $result->unwrapErr();
// Handle the error
}

// You can also do this
$user = $result->unwrap() // Throws error if an error is present
```

In these examples, the `Option` and `Result` types provide a way to handle optional values and the results of operations in a safe and expressive manner.
Expand All @@ -135,6 +152,8 @@ The `wrap` function is a utility function provided by the library. It is designe
Here's an example of how you might use the wrap function:

```php
<?php

use function Bosunski\Results\wrap;

function mightThrowException(): int {
Expand All @@ -145,7 +164,7 @@ function mightThrowException(): int {
return 42;
}

$result = wrap('mightThrowException');
$result = wrap(mightThrowException(...));

if ($result->isOk()) {
echo "Success: " . $result->unwrap();
Expand All @@ -159,12 +178,18 @@ In this example, `mightThrowException` is a function that might throw an excepti
The `wrap` function provides a safe and expressive way to handle operations that can throw errors, allowing you to focus on your application logic rather than error handling when you don't need to.

## Contributing

You can see the [Contributing] docs for more details
### Setting up the project
You can install development dependencies by running:
```shell
composer install
```
### Running tests
```shell
composer run test
```

## License

This project is licensed under the [MIT License].

[MIT license]: LICENSE
[Contributing]: CONTRIBUTING.md
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"license": "MIT",
"require": {
"php": "^8.0"
"php": ">=8.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
Expand Down

0 comments on commit ed6d068

Please sign in to comment.