diff --git a/gleam.toml b/gleam.toml index 1ddab83..7167ef4 100644 --- a/gleam.toml +++ b/gleam.toml @@ -14,6 +14,9 @@ version = "1.0.0" [dependencies] gleam_stdlib = ">= 0.44.0 and < 2.0.0" +glam = ">= 2.0.3 and < 3.0.0" +splitter = ">= 1.1.0 and < 2.0.0" +lazy_const = ">= 1.0.0 and < 2.0.0" [dev-dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml index b3c53aa..0c638f8 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,10 +2,16 @@ # You typically do not need to edit this file packages = [ + { name = "glam", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "237C2CE218A2A0A5D46D625F8EF5B78F964BC91018B78D692B17E1AB84295229" }, { name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" }, { name = "gleeunit", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "FDC68A8C492B1E9B429249062CD9BAC9B5538C6FBF584817205D0998C42E1DAC" }, + { name = "lazy_const", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "lazy_const", source = "hex", outer_checksum = "BFD534B6728C9CF86F368345C96A247FCF6EDD606FAC48D905CA060BF4C1C296" }, + { name = "splitter", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "splitter", source = "hex", outer_checksum = "05564A381580395DCDEFF4F88A64B021E8DAFA6540AE99B4623962F52976AA9D" }, ] [requirements] +glam = { version = ">= 2.0.3 and < 3.0.0" } gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +lazy_const = { version = ">= 1.0.0 and < 2.0.0" } +splitter = { version = ">= 1.1.0 and < 2.0.0" } diff --git a/src/trick.gleam b/src/trick.gleam index d0665b3..b41ed36 100644 --- a/src/trick.gleam +++ b/src/trick.gleam @@ -1,5 +1,52 @@ -import gleam/io +import glam/doc.{type Document} +import gleam/float +import gleam/int +import lazy_const +import splitter -pub fn main() -> Nil { - io.println("Hello from trick!") +pub opaque type Expression(type_) { + Expression(document: Document) +} + +const width = 80 + +pub fn expression_to_string(expression: Expression(any)) -> String { + doc.to_string(expression.document, width) +} + +pub fn int(value: Int) -> Expression(Int) { + value |> int.to_string |> doc.from_string |> Expression +} + +pub fn float(value: Float) -> Expression(Float) { + value |> float.to_string |> doc.from_string |> Expression +} + +pub fn string(value: String) -> Expression(String) { + value |> escape_string_literal |> doc.from_string |> Expression +} + +pub fn escape_string_literal(string: String) -> String { + escape_string_literal_loop(string, escape_string_literal_splitter(), "\"") +} + +fn escape_string_literal_splitter() -> splitter.Splitter { + use <- lazy_const.new(lazy_const.defined_in(escape_string_literal_splitter)) + splitter.new(["\"", "\\"]) +} + +fn escape_string_literal_loop( + string: String, + splitter: splitter.Splitter, + escaped: String, +) -> String { + case splitter.split(splitter, string) { + #(rest_of_string, "", "") -> escaped <> rest_of_string <> "\"" + #(before, split, after) -> + escape_string_literal_loop( + after, + splitter, + escaped <> before <> "\\" <> split, + ) + } } diff --git a/test/trick_test.gleam b/test/trick_test.gleam index fba3c88..0582899 100644 --- a/test/trick_test.gleam +++ b/test/trick_test.gleam @@ -1,13 +1,23 @@ import gleeunit +import trick pub fn main() -> Nil { gleeunit.main() } -// gleeunit test functions end in `_test` -pub fn hello_world_test() { - let name = "Joe" - let greeting = "Hello, " <> name <> "!" +pub fn int_literal_test() { + assert 53 |> trick.int |> trick.expression_to_string == "53" +} + +pub fn float_literal_test() { + assert 3.1415 |> trick.float |> trick.expression_to_string == "3.1415" +} - assert greeting == "Hello, Joe!" +pub fn string_literal_test() { + assert "Hello! This is a string. +It has multiple lines,\nand even quotes: \". This is how you type a backslash: \\\\" + |> trick.string + |> trick.expression_to_string + == "\"Hello! This is a string. +It has multiple lines,\nand even quotes: \\\". This is how you type a backslash: \\\\\\\\\"" }