OCaml library for Crockford's Base32

Compare changes

Choose any two refs to compare.

Changed files
+24
lib
+7
lib/crockford.ml
···
| Invalid_checksum e -> pp_invalid_checksum fmt e
| Checksum_mismatch e -> pp_checksum_mismatch fmt e
+
let decode_error_to_string err =
+
Format.asprintf "%a" pp_decode_error err
+
let encoding_chars = "0123456789abcdefghjkmnpqrstvwxyz"
let generate_checksum number =
···
!number
+
let decode_result ?checksum str =
+
try Ok (decode ?checksum str)
+
with Decode_error e -> Error e
+
let generate ~length ?(split_every=0) ?(checksum=false) ?(rng=Random.float) () =
if checksum && length < 3 then
raise (Decode_error (Invalid_length {
+17
lib/crockford.mli
···
val pp_decode_error : Format.formatter -> decode_error -> unit
(** Pretty-print a decode_error *)
+
val decode_error_to_string : decode_error -> string
+
(** [decode_error_to_string e] converts a decode error to a human-readable string. *)
+
(** {1 Constants} *)
val encoding_chars : string
···
decode ~checksum:true "16j48";; (* 1234L - with checksum validation *)
]} *)
+
val decode_result : ?checksum:bool -> string -> (int64, decode_error) result
+
(** [decode_result ?checksum str] is like {!decode} but returns a result instead
+
of raising an exception.
+
+
This is the recommended interface for code that prefers explicit error handling
+
over exceptions.
+
+
{b Examples:}
+
{[
+
decode_result "16j";; (* Ok 1234L *)
+
decode_result "invalid!";; (* Error (Invalid_character ...) *)
+
decode_result ~checksum:true "16j48";; (* Ok 1234L *)
+
]} *)
+
(** {1 Utility Functions}
Low-level functions for working with Crockford base32 strings and checksums. *)