Skip to content

Commit

Permalink
Create snowflake.gleam
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyteon authored Jul 17, 2024
1 parent ed62dba commit bb5ea2f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/discord_gleam/discord/snowflake.gleam
Original file line number Diff line number Diff line change
@@ -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: [],
),
])
}
}

0 comments on commit bb5ea2f

Please sign in to comment.