From 189405647e2b2b50a4453df17e310d08fde312f7 Mon Sep 17 00:00:00 2001 From: Hayleigh Thompson Date: Sat, 25 Jul 2026 22:43:19 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Improve=20tid=20parsing=20?= =?UTF-8?q?performance.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/at/tid.gleam | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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) } } -- 2.51.2