OCaml library for Crockford's Base32

errors

+24
+7
lib/crockford.ml
··· 35 35 | Invalid_checksum e -> pp_invalid_checksum fmt e 36 36 | Checksum_mismatch e -> pp_checksum_mismatch fmt e 37 37 38 + let decode_error_to_string err = 39 + Format.asprintf "%a" pp_decode_error err 40 + 38 41 let encoding_chars = "0123456789abcdefghjkmnpqrstvwxyz" 39 42 40 43 let generate_checksum number = ··· 165 168 | None -> ()); 166 169 167 170 !number 171 + 172 + let decode_result ?checksum str = 173 + try Ok (decode ?checksum str) 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
··· 166 166 val pp_decode_error : Format.formatter -> decode_error -> unit 167 167 (** Pretty-print a decode_error *) 168 168 169 + val decode_error_to_string : decode_error -> string 170 + (** [decode_error_to_string e] converts a decode error to a human-readable string. *) 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 + ]} *) 242 + 243 + val decode_result : ?checksum:bool -> string -> (int64, decode_error) result 244 + (** [decode_result ?checksum str] is like {!decode} but returns a result instead 245 + of raising an exception. 246 + 247 + This is the recommended interface for code that prefers explicit error handling 248 + over exceptions. 249 + 250 + {b Examples:} 251 + {[ 252 + decode_result "16j";; (* Ok 1234L *) 253 + decode_result "invalid!";; (* Error (Invalid_character ...) *) 254 + decode_result ~checksum:true "16j48";; (* Ok 1234L *) 238 255 ]} *) 239 256 240 257 (** {1 Utility Functions}