-
Notifications
You must be signed in to change notification settings - Fork 1
/
rocks_types.ml
65 lines (53 loc) · 1.51 KB
/
rocks_types.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open Result
let kNotFound = 1
let code_to_string = function
| 0 -> "Ok"
| 1 -> "NotFound"
| 2 -> "Corruption"
| 3 -> "NotSupported"
| 4 -> "InvalidArgument"
| 5 -> "IOError"
| 6 -> "MergeInProgress"
| 7 -> "Incomplete"
| 8 -> "ShutdownInProgress"
| 9 -> "TimedOut"
| 10 -> "Aborted"
| 11 -> "Busy"
| 12 -> "Expired"
| 13 -> "TryAgain"
| n -> Printf.sprintf "<Unrecognized code %d>" n
let subcode_to_string = function
| 0 -> "None"
| 1 -> "MutexTimeout"
| 2 -> "LockTimeout"
| 3 -> "LockLimit"
| 4 -> "NoSpace"
| 5 -> "Deadlock"
| n -> Printf.sprintf "<Unrecognized subcode %d>" n
type status_t = {code : int ; subcode : int ; msg : string }
let format_status st =
Printf.sprintf "<%s, %s, %s>\n" (code_to_string st.code) (subcode_to_string st.subcode) st.msg
let status_to_result st =
if 0 = st.code then Ok ()
else Error (format_status st)
let status2_raise_not_found (st, rv as p) =
if kNotFound = st.code then raise Not_found ;
p
let status2_to_result (st, rv) =
if 0 = st.code then Ok rv
else Error (format_status st)
let status3_to_result (st, rv1, rv2) =
if 0 = st.code then Ok (rv1, rv2)
else Error (format_status st)
let error_to_failure ?(msg="") = function
| Error s ->
failwith (msg^": "^s)
| Ok s -> s
let error_to_assert_failure = function
| Error s ->
assert false
| Ok s -> s
let none_to_failure ?(msg="") = function
| None ->
failwith msg
| Some s -> s