From bb5ea2f9705cbc1afc7b4e03a1017234949d5b78 Mon Sep 17 00:00:00 2001 From: Cyteon <129582290+Cyteon@users.noreply.github.com> Date: Wed, 17 Jul 2024 21:01:27 +0200 Subject: [PATCH] Create snowflake.gleam --- src/discord_gleam/discord/snowflake.gleam | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/discord_gleam/discord/snowflake.gleam diff --git a/src/discord_gleam/discord/snowflake.gleam b/src/discord_gleam/discord/snowflake.gleam new file mode 100644 index 0000000..997abc4 --- /dev/null +++ b/src/discord_gleam/discord/snowflake.gleam @@ -0,0 +1,30 @@ +import gleam/dynamic +import gleam/int + +pub type Snowflake = + String + +pub fn from_dynamic( + dyn: dynamic.Dynamic, +) -> Result(Snowflake, List(dynamic.DecodeError)) { + case dynamic.classify(dyn) { + "String" -> { + dynamic.string(dyn) + } + "Int" -> { + case dynamic.int(dyn) { + Ok(num) -> Ok(int.to_string(num)) + Error(errors) -> Error(errors) + } + } + // Should be a String or Int, this should not happen + _ -> + Error([ + dynamic.DecodeError( + expected: "String or Int", + found: dynamic.classify(dyn), + path: [], + ), + ]) + } +}