From d08040a8e5d31677f4dcbbf6d26a54bf4f934522 Mon Sep 17 00:00:00 2001 From: Erika Rowland Date: Sat, 30 Mar 2024 23:46:45 -0700 Subject: [PATCH] it works --- .gitignore | 6 +++ README.md | 25 +++++++++ gleam.toml | 24 +++++++++ manifest.toml | 36 +++++++++++++ src/constellation.gleam | 96 +++++++++++++++++++++++++++++++++++ test/constellation_test.gleam | 12 +++++ 6 files changed, 199 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 gleam.toml create mode 100644 manifest.toml create mode 100644 src/constellation.gleam create mode 100644 test/constellation_test.gleam diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4a8f02 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.beam +*.ez +/build +erl_crash.dump +.github +members.* diff --git a/README.md b/README.md new file mode 100644 index 0000000..6801b82 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# constellation + +[![Package Version](https://img.shields.io/hexpm/v/constellation)](https://hex.pm/packages/constellation) +[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/constellation/) + +```sh +gleam add constellation +``` +```gleam +import constellation + +pub fn main() { + // TODO: An example of the project in use +} +``` + +Further documentation can be found at . + +## Development + +```sh +gleam run # Run the project +gleam test # Run the tests +gleam shell # Run an Erlang shell +``` diff --git a/gleam.toml b/gleam.toml new file mode 100644 index 0000000..d0babfe --- /dev/null +++ b/gleam.toml @@ -0,0 +1,24 @@ +name = "constellation" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "username", repo = "project" } +# links = [{ title = "Website", href = "https://gleam.run" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = "~> 0.34 or ~> 1.0" +wisp = "~> 0.14" +mist = "~> 1.0" +sqlight = "~> 0.9" +glesha = "~> 0.1" +simplifile = "~> 1.6" + +[dev-dependencies] +gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml new file mode 100644 index 0000000..8a2cff3 --- /dev/null +++ b/manifest.toml @@ -0,0 +1,36 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "birl", version = "1.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "0757CFE97DA52F19BC3262AC3DD284D9DAD2718D4C1830888DE483FB147477D4" }, + { name = "esqlite", version = "0.8.6", build_tools = ["rebar3"], requirements = [], otp_app = "esqlite", source = "hex", outer_checksum = "607E45F4DA42601D8F530979417F57A4CD629AB49085891849302057E68EA188" }, + { name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" }, + { name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" }, + { name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" }, + { name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" }, + { name = "gleam_http", version = "3.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8C07DF9DF8CC7F054C650839A51C30A7D3C26482AC241C899C1CEA86B22DBE51" }, + { name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" }, + { name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" }, + { name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" }, + { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, + { name = "glesha", version = "0.1.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glesha", source = "hex", outer_checksum = "8DC5F211670C12870FC36C211B8C0A0FFB07BC83708484174CAE1F9DA4B8AF9F" }, + { name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" }, + { name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" }, + { name = "logging", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "82C112ED9B6C30C1772A6FE2613B94B13F62EA35F5869A2630D13948D297BD39" }, + { name = "marceau", version = "1.1.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "1AAD727A30BE0F95562C3403BB9B27C823797AD90037714255EEBF617B1CDA81" }, + { name = "mist", version = "1.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "7765E53DCC9ACCACF217B8E0CA3DE7E848C783BFAE5118B75011E81C2C80385C" }, + { name = "ranger", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "28E615AE7590ED922AF1510DDF606A2ECBBC2A9609AF36D412EDC925F06DFD20" }, + { name = "simplifile", version = "1.6.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "B75D3C64E526D9D7EDEED5F3BA31DAAF5F2B4D80A4183FE17FDB02ED526E4E96" }, + { name = "sqlight", version = "0.9.0", build_tools = ["gleam"], requirements = ["esqlite", "gleam_stdlib"], otp_app = "sqlight", source = "hex", outer_checksum = "2D9C9BA420A5E7DCE7DB2DAAE4CAB0BE6218BEB48FD1531C583550B3D1316E94" }, + { name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" }, + { name = "wisp", version = "0.14.0", build_tools = ["gleam"], requirements = ["exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "9F5453AF1F9275E6F8707BC815D6A6A9DF41551921B16FBDBA52883773BAE684" }, +] + +[requirements] +gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } +gleeunit = { version = "~> 1.0" } +glesha = { version = "~> 0.1" } +mist = { version = "~> 1.0" } +simplifile = { version = "~> 1.6"} +sqlight = { version = "~> 0.9" } +wisp = { version = "~> 0.14" } diff --git a/src/constellation.gleam b/src/constellation.gleam new file mode 100644 index 0000000..6dbe3fb --- /dev/null +++ b/src/constellation.gleam @@ -0,0 +1,96 @@ +import gleam/io +import gleam/list +import gleam/dict +import gleam/bit_array +import gleam/string +import gleam/result +import gleam/dynamic +import glesha +import sqlight +import simplifile + +pub fn main() { + let db_path = "members.db" + use conn <- sqlight.with_connection(db_path) + + let _ = try_create_table(conn) + + use text <- result.map(simplifile.read("members.txt")) + text + |> string.trim + |> string.split(_, "\n") + |> create_ring + |> list.map(create_member(conn, _)) + |> io.debug() +} + +fn create_member(conn, row: Row) { + let sql = + " + insert into ring (hash, member, previous, next) values + (?, ?, ?, ?) + " +sqlight.query( + sql, + on: conn, + with: [ + sqlight.text(row.hash), + sqlight.text(row.link), + sqlight.text(row.previous), + sqlight.text(row.next) + ], + expecting: dynamic.dynamic, + ) +} + +fn try_create_table(conn) { + let sql = + " + drop table ring; + create table ring (hash text PRIMARY KEY, member text, previous text, next text); + " + let _ = sqlight.exec(sql, conn) +} + +fn hash(string) { + {string <> "salty salt"} + |> bit_array.from_string() + |> glesha.hash(glesha.Sha256) + |> bit_array.base64_url_encode(False) + |> string.slice(0, 16) +} + +pub type Row { + Row( + link: String, + hash: String, + previous: String, + next: String + ) +} + +pub type Node(a) { + Node(previous: a, next: a) +} + +fn create_ring(list) { + do_window([], list, list) +} + +fn do_window(acc, l, original) { + let window = list.take(l, 3) + + case window { + [a, b, c, ..] -> + do_window([Row(b, hash(b), previous: a, next: c), ..acc], list.drop(l, 1), original) + [a, b] -> { + let assert [c] = list.take(original, 1) + do_window([Row(b, hash(b), previous: a, next: c), ..acc], list.drop(l, 1), original) + } + [a] -> { + let assert [b, c] = list.take(original, 2) + do_window([Row(b, hash(b), previous: a, next: c), ..acc], list.drop(l, 1), original) + } + [] -> acc + } +} diff --git a/test/constellation_test.gleam b/test/constellation_test.gleam new file mode 100644 index 0000000..3831e7a --- /dev/null +++ b/test/constellation_test.gleam @@ -0,0 +1,12 @@ +import gleeunit +import gleeunit/should + +pub fn main() { + gleeunit.main() +} + +// gleeunit test functions end in `_test` +pub fn hello_world_test() { + 1 + |> should.equal(1) +}