Security update for permissions_by_term
[yaffs-website] / node_modules / rust-result / README.md
1 # rust-result.js
2
3 Mimic Rust's [`std::result`][result].
4
5 ## Installation
6
7 ```sh
8 npm install rust-result
9 ```
10
11 ## Usage
12
13 ```js
14 var fs = require('fs');
15 var Result = require('./');
16
17
18 // If you want async just get a promise or something.
19 var readFile = function (path, encoding) {
20   try {
21     return Result.Ok(fs.readFileSync(path, encoding))
22   }
23   catch (ex) {
24     return Result.Err(ex)
25   }
26 }
27
28 var result = readFile(__filename);
29 var v, err;
30
31 if (Result.isOk(result)) {
32   v = Result.Ok(result);
33   console.log('got ' + v.length + ' bytes')
34 }
35 else if (Result.isErr(result)) {
36   err = Result.Err(result);
37   console.error('oops!', err.message)
38 }
39
40 result = readFile(__filename + 'I do not exist')
41 if (Result.isOk(result)) {
42   v = Result.Ok(result)
43   console.log('got ' + v.length + ' bytes')
44 }
45 else if (Result.isErr(result)) {
46   err = Result.Err(result)
47   console.error('oops!', err.message)
48 }
49
50 ```
51
52 ## Documentation
53
54 ```jsig
55 type OkResult<T> : {
56   v: T
57 }
58 type ErrResult<E <: Error> : {
59   err: E
60 }
61
62 rust-result : {
63   Ok: ((T) => OkResult<T>) |
64     ((OkResult<T>) => T) |
65     ((ErrResult<E>) => void),
66   isOk: ((OkResult<T>) => true) |
67     ((ErrResult<E>) => false)
68   Err: ((E <: Error) => ErrResult<E>) |
69     ((ErrResult<E>) => E) |
70     ((OkResult<T>) => void),
71   isErr: ((ErrResult<E>) => true) |
72     ((OkResult<T>) => false)
73 }
74 ```
75
76 ### `Result.Ok`
77
78 The `Result.Ok` function is overloaded to do one of two things.
79   It can create a new `Ok` instance or it can check whether
80   the argument is an instance of `Ok`
81
82 If you call `Result.Ok` with a plain value it will return an
83   instance of `Ok` that boxes your plain value.
84
85 If you call `Result.Ok` with either an `Err` or an `Ok` instance
86   then it will return `undefined` for the `Err` and return the
87   value boxed in the `Ok`
88
89 ### `Result.isOk`
90
91 The `Result.isOk` function just checks whether the argument
92   is an instance of `Ok`.
93
94 This predicate function returns true if you pass it an `Ok` and
95   returns false if you pass it an `Err`
96
97 ### `Result.Err`
98
99 The `Result.Err` function is overloaded to do one of two things.
100   It can create a new `Err` instance or it can check whether
101   the argument is an instance of `Err`
102
103 If you call `Result.Err` with a plain error it will return an
104   instance of `Err` that boxes your plain error.
105
106 If you call `Result.Err` with either an `Err` or an `Ok` instance
107   then it will return `undefined` for the `Ok` and return the
108   value err in the `Err`
109
110 ### `Result.isErr`
111
112 The `Result.isErr` function just checks whether the argument
113   is an instance of `Err`.
114
115 This predicate function returns true if you pass it an `Err` and
116   returns false if you pass it an `Ok`
117
118 ## MIT Licenced.
119
120   [result]: http://doc.rust-lang.org/std/result/