diff --git a/src/at/tid.gleam b/src/at/tid.gleam index 4005eb9..6532b59 100644 --- a/src/at/tid.gleam +++ b/src/at/tid.gleam @@ -4,6 +4,7 @@ import gleam/dynamic/decode.{type Decoder} import gleam/int import gleam/json.{type Json} import gleam/order.{type Order} +import gleam/string import gleam/time/timestamp.{type Timestamp} // TYPES ----------------------------------------------------------------------- @@ -85,16 +86,19 @@ pub fn next(tid: Tid) -> Tid { @external(javascript, "./tid_ffi.mjs", "next") fn do_next(microseconds: Int) -> Int -/// Attempt to parse a [`Tid`](#Tid) from a base32-sortable string like `"3jzfcijpj2z2a"`. -/// In particular, an encoded [`Tid`](#Tid) will always be 13 characters long and -/// contain only the characters `234567abcdefghijklmnopqrstuvwxyz`. +/// Attempt to parse a [`Tid`](#Tid) from a base32-sortable string. /// pub fn from_string(value: String) -> Result(Tid, Nil) { - case parse_tid_string(value) { - Ok(<<0:int-size(1), microseconds:int-size(53), clock:int-size(10)>>) -> - Ok(Tid(microseconds:, clock:)) + case string.length(value) == 13 { + True -> + case parse_tid_string(value) { + Ok(<<0:int-size(1), microseconds:int-size(53), clock:int-size(10)>>) -> + Ok(Tid(microseconds:, clock:)) - Ok(_) | Error(_) -> Error(Nil) + Ok(_) | Error(_) -> Error(Nil) + } + + False -> Error(Nil) } }