Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for latest Gleam #21

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: erlef/setup-beam@v1.15.3
with:
otp-version: "25.2"
gleam-version: "0.32.2"
gleam-version: "0.33.0"
rebar3-version: "3"
- run: gleam format --check src test
- run: gleam deps download
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repository = { type = "github", user = "rvcas", repo = "ids" }
gleam = ">= 0.32.2"

[dependencies]
gleam_stdlib = "~> 0.32"
gleam_stdlib = "~> 0.34"
gleam_otp = "~> 0.7"
gleam_erlang = "~> 0.22"

Expand Down
10 changes: 5 additions & 5 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_erlang", version = "0.23.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DA7A8E5540948DE10EB01B530869F8FF2FF6CAD8CFDA87626CE6EF63EBBF87CB" },
{ name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" },
{ name = "gleam_stdlib", version = "0.32.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "07D64C26D014CF570F8ACADCE602761EA2E74C842D26F2FD49B0D61973D9966F" },
{ name = "gleeunit", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D3682ED8C5F9CAE1C928F2506DE91625588CC752495988CBE0F5653A42A6F334" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
]

[requirements]
gleam_erlang = { version = "~> 0.22" }
gleam_otp = { version = "~> 0.7" }
gleam_stdlib = { version = "~> 0.32" }
gleam_stdlib = { version = "~> 0.34" }
gleeunit = { version = "~> 1.0" }
3 changes: 2 additions & 1 deletion src/ids/cuid.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ fn get_fingerprint() -> String {

let hostid = { sum + list.length(localhost) + base } % operator

id + hostid
id
+ hostid
|> int.to_base36()
}

Expand Down
57 changes: 19 additions & 38 deletions src/ids/nanoid.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn generate() -> String {

let size: Int = default_size

let assert Ok(True) = check_nanoid_args(size, alphabet)
let assert Ok(Nil) = check_nanoid_args(size, alphabet)

let mask = calculate_mask(alphabet_length)

Expand Down Expand Up @@ -103,7 +103,7 @@ fn generate_nanoid(
mask: Int,
) -> Result(BitArray, String) {
case check_nanoid_args(size, alphabet) {
Ok(True) ->
Ok(Nil) ->
size
|> random_bytes()
|> list.map(fn(x: Int) -> BitArray {
Expand All @@ -120,48 +120,30 @@ fn generate_nanoid(
}
}

fn check_nanoid_args(size: Int, alphabet: BitArray) -> Result(Bool, String) {
fn check_nanoid_args(size: Int, alphabet: BitArray) -> Result(Nil, String) {
case check_size(size) {
Ok(True) ->
case check_alphabet(alphabet) {
Ok(True) ->
True
|> Ok
Error(error) ->
error
|> Error
}
Error(error) ->
error
|> Error
Ok(Nil) -> check_alphabet(alphabet)
Error(error) -> Error(error)
}
}

fn check_size(size: Int) -> Result(Bool, String) {
fn check_size(size: Int) -> Result(Nil, String) {
case size > 0 {
True ->
True
|> Ok
False -> {
let error: String =
"Error: The specified ID size is too small. Increase the size of the ID."
error
|> Error
}
True -> Ok(Nil)
False ->
Error(
"Error: The specified ID size is too small. Increase the size of the ID.",
)
}
}

fn check_alphabet(alphabet: BitArray) -> Result(Bool, String) {
fn check_alphabet(alphabet: BitArray) -> Result(Nil, String) {
case bit_array.byte_size(alphabet) > 1 {
True ->
True
|> Ok
False -> {
let error: String =
"Error: The specified alphabet size is too small. Increase the size of the alphabet."
error
|> Error
}
True -> Ok(Nil)
False ->
Error(
"Error: The specified alphabet size is too small. Increase the size of the alphabet.",
)
}
}

Expand Down Expand Up @@ -190,9 +172,8 @@ fn calculate_mask(alphabet_length: Int) -> Int {
fn calculate_step(mask: Int, size: Int, alphabet_length: Int) -> Int {
let step: Float =
float.ceiling(
1.6 *. int.to_float(mask) *. int.to_float(size) /. int.to_float(
alphabet_length,
),
1.6 *. int.to_float(mask) *. int.to_float(size)
/. int.to_float(alphabet_length),
)
float.round(step)
}
23 changes: 10 additions & 13 deletions src/ids/ulid.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn encode_bytes(binary: BitArray) -> String {
|> result.unwrap("0")
|> string.append(encode_bytes(rest))
}
<<>> -> ""
_ -> ""
}
}

Expand All @@ -161,22 +161,19 @@ fn decode_base32(binary: String) -> Result(BitArray, Nil) {
let crockford_with_index =
crockford_alphabet
|> string.to_graphemes()
|> list.index_map(fn(i, x) { #(x, i) })
|> list.index_map(fn(x, i) { #(x, i) })

let bits =
binary
|> string.to_graphemes()
|> list.fold(
<<>>,
fn(acc, c) {
let index =
crockford_with_index
|> list.key_find(c)
|> result.unwrap(0)

<<acc:bits, index:5>>
},
)
|> list.fold(<<>>, fn(acc, c) {
let index =
crockford_with_index
|> list.key_find(c)
|> result.unwrap(0)

<<acc:bits, index:5>>
})

let padding =
bits
Expand Down
6 changes: 4 additions & 2 deletions src/ids/uuid.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn crypto_strong_rand_bytes(n: Int) -> BitArray
/// ```
///
pub fn generate_v4() -> Result(String, String) {
let <<u0:size(48), _:size(4), u1:size(12), _:size(2), u2:size(62)>> =
let assert <<u0:size(48), _:size(4), u1:size(12), _:size(2), u2:size(62)>> =
crypto_strong_rand_bytes(16)

cast(<<u0:size(48), 4:size(4), u1:size(12), 2:size(2), u2:size(62)>>)
Expand All @@ -47,7 +47,7 @@ pub fn generate_v7() -> Result(String, String) {

/// Generates a version 7 UUID from a given unix timestamp in milliseconds.
pub fn generate_v7_from_timestamp(timestamp: Int) -> Result(String, String) {
let <<_:size(48), _:size(4), a:size(12), _:size(2), b:size(62)>> =
let assert <<_:size(48), _:size(4), a:size(12), _:size(2), b:size(62)>> =
crypto_strong_rand_bytes(16)

cast(<<timestamp:size(48), 7:size(4), a:size(12), 2:size(2), b:size(62)>>)
Expand Down Expand Up @@ -254,6 +254,7 @@ fn e(n: Int) -> Int {
13 -> 100
14 -> 101
15 -> 102
_ -> 102
}
}

Expand All @@ -275,5 +276,6 @@ fn d(n: Int) -> Int {
100 -> 13
101 -> 14
102 -> 15
_ -> 15
}
}
27 changes: 12 additions & 15 deletions test/ids/cuid_test.gleam
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gleeunit/should
import ids/cuid
import gleam/iterator.{Done, Next}
import gleam/map
import gleam/dict
import gleam/pair
import gleam/string

Expand Down Expand Up @@ -61,20 +61,17 @@ fn check_collision(func: fn() -> String) -> Bool {
True -> Next(element: func(), accumulator: acc + 1)
}
})
|> iterator.fold(
from: #(map.new(), True),
with: fn(acc, id) {
let #(id_map, flag) = acc
|> iterator.fold(from: #(dict.new(), True), with: fn(acc, id) {
let #(id_dict, flag) = acc

case flag {
False -> acc
True ->
case map.get(id_map, id) {
Ok(_) -> #(id_map, False)
Error(_) -> #(map.insert(id_map, id, id), True)
}
}
},
)
case flag {
False -> acc
True ->
case dict.get(id_dict, id) {
Ok(_) -> #(id_dict, False)
Error(_) -> #(dict.insert(id_dict, id, id), True)
}
}
})
|> pair.second()
}
14 changes: 6 additions & 8 deletions test/ids/ulid_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ pub fn from_timestamp_test() {

pub fn from_parts_test() {
let assert Ok(id_1) =
ulid.from_parts(
1_696_346_659_217,
<<150, 184, 121, 192, 42, 76, 148, 57, 61, 61>>,
)
ulid.from_parts(1_696_346_659_217, <<
150, 184, 121, 192, 42, 76, 148, 57, 61, 61,
>>)
id_1
|> should.equal("01HBV27PCHJTW7KG1A9JA3JF9X")

let assert Ok(id_2) =
ulid.from_parts(
281_474_976_710_655,
<<255, 255, 255, 255, 255, 255, 255, 255, 255, 255>>,
)
ulid.from_parts(281_474_976_710_655, <<
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
>>)
id_2
|> should.equal("7ZZZZZZZZZZZZZZZZZZZZZZZZZ")
}
Expand Down