grust

package module
v0.0.0-...-d87abf7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2024 License: MIT Imports: 0 Imported by: 0

README

grust

grust is a Go library inspired by Rust's Result type, providing ergonomic error handling and functional programming constructs.

Features

Result

The Result type represents the result of an operation that may fail. It can hold either a successful value (Ok) or an error (Err).

Example Usage
package main

import (
    "fmt"
    "github.com/jaavier/grust"
)

func divide(a, b int) grust.Result {
    if b == 0 {
        return grust.Result{
            Err: func() interface{} { return fmt.Errorf("division by zero") },
            IsErr: true,
        }
    }
    return grust.Result{
        Ok: func() interface{} { return a / b },
        IsOk: true,
    }
}

func main() {
    result := divide(10, 2)
    if result.IsErr {
        fmt.Println("Error:", result.Err())
    } else {
        fmt.Println("Result:", result.Ok())
    }
}
Map

The Map function applies a function to the value contained in a Result, returning a new Result with the transformed value.

Example Usage
result := divide(10, 2).Map(func(val interface{}) interface{} {
    return val.(int) * 2
})
fmt.Println("Mapped Result:", result.Ok()) // Output: Mapped Result: 10
AndThen

The AndThen function chains a function that returns a Result, allowing sequential operations with error handling.

Example Usage
result := divide(10, 2).AndThen(func(val interface{}) *grust.Result {
    return divide(val.(int), 2)
})
fmt.Println("Chained Result:", result.Ok()) // Output: Chained Result: 5
OrElse

The OrElse function returns the Ok value if the Result is successful, otherwise it returns a default value.

Example Usage
result := divide(10, 2).OrElse(0)
fmt.Println("Result:", result) // Output: Result: 5
Unwrap

The Unwrap function extracts the value from a successful Result or panics with the contained error if it's an error.

Example Usage
result := divide(10, 2)
fmt.Println("Unwrapped Result:", result.Unwrap()) // Output: Unwrapped Result: 5
UnwrapOr

The UnwrapOr function extracts the value from a successful Result or returns a default value if it's an error.

Example Usage
result := divide(10, 0)
fmt.Println("Unwrapped Result:", result.UnwrapOr(0)) // Output: Unwrapped Result: 0
UnwrapErr

The UnwrapErr function extracts the error from a failed Result or panics with the error if it's successful.

Example Usage
result := divide(10, 0)
fmt.Println("Unwrapped Error:", result.UnwrapErr()) // Output: Unwrapped Error: division by zero

Installation

To use grust in your Go project, simply import it:

import "github.com/jaavier/grust"

License

This library is licensed under the MIT License. See the LICENSE file for details.


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AndThenFn

type AndThenFn func(interface{}) *Result

type Err

type Err func() interface{}

type MapFn

type MapFn func(interface{}) interface{}

type Ok

type Ok func() interface{}

type Result

type Result struct {
	Ok    Ok
	IsOk  bool
	Err   Err
	IsErr bool
	Panic bool
}

func (*Result) AndThen

func (r *Result) AndThen(fn AndThenFn) *Result

func (*Result) Map

func (r *Result) Map(fn MapFn) *Result

func (*Result) OrElse

func (r *Result) OrElse(defaultValue interface{}) interface{}

func (*Result) Unwrap

func (r *Result) Unwrap() interface{}

func (*Result) UnwrapErr

func (r *Result) UnwrapErr() interface{}

func (*Result) UnwrapOr

func (r *Result) UnwrapOr(defaultValue interface{}) interface{}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL