/
snowflake.ex
167 lines (131 loc) · 4.14 KB
/
snowflake.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
defmodule Crux.Structs.Snowflake do
@moduledoc """
Custom non discord api module to help with working with Discord's snowflakes.
For more information see [Discord Docs](https://discord.com/developers/docs/reference#snowflakes).
"""
@moduledoc since: "0.2.1"
alias Crux.Structs.Snowflake
use Bitwise
@typedoc """
A discord `snowflake`, an unsigned 64 bit integer.
"""
@typedoc since: "0.2.1"
@type t :: 0..0xFFFF_FFFF_FFFF_FFFF
@typedoc """
All valid types that can be resolved into a `t:t/0`.
"""
@typedoc since: "0.2.1"
@type resolvable :: String.t() | t()
@doc """
Returns `true` if `term` is a `t:t/0`; otherwise returns `false`..
"""
@doc since: "0.2.1"
defguard is_snowflake(snowflake)
when is_integer(snowflake) and snowflake in 0..0xFFFF_FFFF_FFFF_FFFF
@doc """
The discord epoch, the first second of 2015 or `1420070400000`.
```elixir
iex> Crux.Structs.Snowflake.discord_epoch()
1_420_070_400_000
```
"""
@doc since: "0.2.1"
@spec discord_epoch() :: non_neg_integer()
defdelegate discord_epoch(), to: Crux.Structs.Snowflake.Parts
@doc """
Deconstructs a `t:t/0` to its `t:Crux.Structs.Snowflake.Parts.t/0`.
```elixir
iex> Crux.Structs.Snowflake.deconstruct(218348062828003328)
%Crux.Structs.Snowflake.Parts{
increment: 0,
process_id: 0,
timestamp: 1472128634889,
worker_id: 1
}
```
"""
@doc since: "0.2.1"
@spec deconstruct(t) :: Snowflake.Parts.t()
defdelegate deconstruct(snowflake), to: Snowflake.Parts
@doc """
Constructs a `t:t/0` from its `t:Crux.Structs.Snowflake.Parts.t/0` or a keyword of its fields.
```elixir
iex> %Crux.Structs.Snowflake.Parts{increment: 0, process_id: 0, timestamp: 1472128634889, worker_id: 1}
...> |> Crux.Structs.Snowflake.construct()
218348062828003328
iex> Crux.Structs.Snowflake.construct(increment: 1, timestamp: 1451106635493)
130175406673231873
iex> Crux.Structs.Snowflake.construct(timestamp: Crux.Structs.Snowflake.discord_epoch())
0
```
"""
@doc since: "0.2.1"
@spec construct(Snowflake.Parts.t() | Keyword.t()) :: t
defdelegate construct(parts), to: Snowflake.Parts
@doc """
Converts a `t:String.t/0` to a `t:t/0` while allowing `t:t/0` and `nil` to pass through.
Raises an `ArgumentError` if the provided string is not an integer.
```elixir
iex> Crux.Structs.Snowflake.to_snowflake(218348062828003328)
218348062828003328
# Fallbacks
iex> Crux.Structs.Snowflake.to_snowflake("218348062828003328")
218348062828003328
iex> Crux.Structs.Snowflake.to_snowflake(nil)
nil
```
"""
@doc since: "0.2.1"
@spec to_snowflake(t()) :: t()
@spec to_snowflake(String.t()) :: t() | no_return()
@spec to_snowflake(nil) :: nil
def to_snowflake(nil), do: nil
def to_snowflake(snowflake) when is_snowflake(snowflake) do
snowflake
end
def to_snowflake(string) when is_binary(string) do
string
|> String.to_integer()
|> to_snowflake()
end
@doc """
Converts a `t:String.t/0` to a `t:t/0` while allowing `t:t/0` to pass through.
Returns `:error` if the provided string is not a `t:t/0`.
```elixir
iex> Crux.Structs.Snowflake.parse("invalid")
:error
iex> Crux.Structs.Snowflake.parse(218348062828003328)
218348062828003328
# Fallbacks
iex> Crux.Structs.Snowflake.parse("218348062828003328")
218348062828003328
```
"""
@doc since: "0.2.1"
@spec parse(t()) :: t()
@spec parse(String.t()) :: t() | :error
def parse(snowflake) when is_snowflake(snowflake) do
snowflake
end
def parse(string) when is_binary(string) do
case Integer.parse(string) do
{snowflake, ""} when is_snowflake(snowflake) ->
snowflake
_ ->
:error
end
end
# Delegates
@doc """
Deconstructs a `t:t/0` to its `t:Crux.Structs.Snowflake.Parts.t/0`.
"""
@doc since: "0.2.1"
@spec from_integer(t) :: Snowflake.Parts.t()
defdelegate from_integer(snowflake), to: Snowflake.Parts, as: :deconstruct
@doc """
Constructs a `t:t/0` from its `t:Crux.Structs.Snowflake.Parts.t/0`.
"""
@doc since: "0.2.1"
@spec to_integer(Snowflake.Parts.t()) :: t()
defdelegate to_integer(t), to: Snowflake.Parts, as: :construct
end