Skip to content

Commit 58fdec5

Browse files
Add uuid module so we can actually render uuids
1 parent c67aa7b commit 58fdec5

2 files changed

Lines changed: 118 additions & 1 deletion

File tree

gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "espresso_pgo_wrapper"
2-
version = "0.0.4"
2+
version = "0.0.5"
33
description = "Simple wrapper around the espresso PGO tool"
44

55
# Fill out these fields if you intend to generate HTML documentation or publish

src/database/uuid.gleam

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//// UUID helper module for encoding a bit_string into a regular string
2+
//// Most code translated from elixir here:
3+
//// https://github.com/elixir-ecto/ecto/blob/30311c21cb40f39dbbc148e58ff1ed3cac951ea1/lib/ecto/uuid.ex#L179-L188
4+
5+
import gleam/bit_string
6+
7+
/// Casts a bit_string into a regular string
8+
///
9+
/// ### Examples
10+
///
11+
/// ```gleam
12+
/// cast(<<73, 159, 108, 135, 102, 203, 67, 106, 159, 57, 176, 178, 81, 149, 27, 34>>)
13+
/// Ok("499f6c87-66cb-436a-9f39-b0b251951b22")
14+
///
15+
/// cast(<<0, 1, 2, 3>>)
16+
/// Error(Nil)
17+
/// ```
18+
pub fn cast(binary: BitString) -> Result(String, Nil) {
19+
case binary {
20+
<<
21+
a1:4,
22+
a2:4,
23+
a3:4,
24+
a4:4,
25+
a5:4,
26+
a6:4,
27+
a7:4,
28+
a8:4,
29+
b1:4,
30+
b2:4,
31+
b3:4,
32+
b4:4,
33+
c1:4,
34+
c2:4,
35+
c3:4,
36+
c4:4,
37+
d1:4,
38+
d2:4,
39+
d3:4,
40+
d4:4,
41+
e1:4,
42+
e2:4,
43+
e3:4,
44+
e4:4,
45+
e5:4,
46+
e6:4,
47+
e7:4,
48+
e8:4,
49+
e9:4,
50+
e10:4,
51+
e11:4,
52+
e12:4,
53+
>> ->
54+
<<
55+
e(a1),
56+
e(a2),
57+
e(a3),
58+
e(a4),
59+
e(a5),
60+
e(a6),
61+
e(a7),
62+
e(a8),
63+
45,
64+
e(b1),
65+
e(b2),
66+
e(b3),
67+
e(b4),
68+
45,
69+
e(c1),
70+
e(c2),
71+
e(c3),
72+
e(c4),
73+
45,
74+
e(d1),
75+
e(d2),
76+
e(d3),
77+
e(d4),
78+
45,
79+
e(e1),
80+
e(e2),
81+
e(e3),
82+
e(e4),
83+
e(e5),
84+
e(e6),
85+
e(e7),
86+
e(e8),
87+
e(e9),
88+
e(e10),
89+
e(e11),
90+
e(e12),
91+
>>
92+
|> bit_string.to_string()
93+
94+
_ -> Error(Nil)
95+
}
96+
}
97+
98+
fn e(i: Int) {
99+
case i {
100+
0 -> 48
101+
1 -> 49
102+
2 -> 50
103+
3 -> 51
104+
4 -> 52
105+
5 -> 53
106+
6 -> 54
107+
7 -> 55
108+
8 -> 56
109+
9 -> 57
110+
10 -> 97
111+
11 -> 98
112+
12 -> 99
113+
13 -> 100
114+
14 -> 101
115+
15 -> 102
116+
}
117+
}

0 commit comments

Comments
 (0)