tangled
alpha
login
or
join now
anil.recoil.org
/
ocaml-crockford
0
fork
atom
OCaml library for Crockford's Base32
0
fork
atom
overview
issues
pulls
pipelines
errors
anil.recoil.org
3 months ago
b2601d34
8bfd5f2f
1/1
build.yml
success
5m 35s
+24
2 changed files
expand all
collapse all
unified
split
lib
crockford.ml
crockford.mli
+7
lib/crockford.ml
reviewed
···
35
35
| Invalid_checksum e -> pp_invalid_checksum fmt e
36
36
| Checksum_mismatch e -> pp_checksum_mismatch fmt e
37
37
38
38
+
let decode_error_to_string err =
39
39
+
Format.asprintf "%a" pp_decode_error err
40
40
+
38
41
let encoding_chars = "0123456789abcdefghjkmnpqrstvwxyz"
39
42
40
43
let generate_checksum number =
···
165
168
| None -> ());
166
169
167
170
!number
171
171
+
172
172
+
let decode_result ?checksum str =
173
173
+
try Ok (decode ?checksum str)
174
174
+
with Decode_error e -> Error e
168
175
169
176
let generate ~length ?(split_every=0) ?(checksum=false) ?(rng=Random.float) () =
170
177
if checksum && length < 3 then
+17
lib/crockford.mli
reviewed
···
166
166
val pp_decode_error : Format.formatter -> decode_error -> unit
167
167
(** Pretty-print a decode_error *)
168
168
169
169
+
val decode_error_to_string : decode_error -> string
170
170
+
(** [decode_error_to_string e] converts a decode error to a human-readable string. *)
171
171
+
169
172
(** {1 Constants} *)
170
173
171
174
val encoding_chars : string
···
235
238
decode "1-6-j";; (* 1234L - hyphens ignored *)
236
239
decode "I6j";; (* 1234L - 'I' mapped to '1' *)
237
240
decode ~checksum:true "16j48";; (* 1234L - with checksum validation *)
241
241
+
]} *)
242
242
+
243
243
+
val decode_result : ?checksum:bool -> string -> (int64, decode_error) result
244
244
+
(** [decode_result ?checksum str] is like {!decode} but returns a result instead
245
245
+
of raising an exception.
246
246
+
247
247
+
This is the recommended interface for code that prefers explicit error handling
248
248
+
over exceptions.
249
249
+
250
250
+
{b Examples:}
251
251
+
{[
252
252
+
decode_result "16j";; (* Ok 1234L *)
253
253
+
decode_result "invalid!";; (* Error (Invalid_character ...) *)
254
254
+
decode_result ~checksum:true "16j48";; (* Ok 1234L *)
238
255
]} *)
239
256
240
257
(** {1 Utility Functions}