diff options
author | Giacomo Cavalieri <giacomo.cavalieri@icloud.com> | 2024-02-29 21:29:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-29 20:29:23 +0000 |
commit | 4970e2ab89c6b84487ec3d302e41603bedb1a950 (patch) | |
tree | fcc0c5a8e6c19f7120d6cb9f19c1d566770c71df | |
parent | 9baad8dc9c36c79190759d595a22681af95917c6 (diff) | |
download | lustre-4970e2ab89c6b84487ec3d302e41603bedb1a950.tar.gz lustre-4970e2ab89c6b84487ec3d302e41603bedb1a950.zip |
🔀 Use `package-interface` package (#49)
-rw-r--r-- | gleam.toml | 1 | ||||
-rw-r--r-- | manifest.toml | 4 | ||||
-rw-r--r-- | src/lustre/cli/build.gleam | 5 | ||||
-rw-r--r-- | src/lustre/cli/dev.gleam | 7 | ||||
-rw-r--r-- | src/lustre/cli/project.gleam | 82 |
5 files changed, 28 insertions, 71 deletions
@@ -53,6 +53,7 @@ shellout = "~> 1.6" simplifile = "~> 1.4" spinner = "~> 1.1" tom = "~> 0.3" +gleam_package_interface = "~> 1.0" [dev-dependencies] birdie = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index 9169792..31e4e21 100644 --- a/manifest.toml +++ b/manifest.toml @@ -11,6 +11,7 @@ packages = [ { name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" }, { 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.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" }, + { name = "gleam_package_interface", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_package_interface", source = "hex", outer_checksum = "52A721BCA972C8099BB881195D821AAA64B9F2655BECC102165D5A1097731F01" }, { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, { name = "glearray", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glearray", source = "hex", outer_checksum = "908154F695D330E06A37FAB2C04119E8F315D643206F8F32B6A6C14A8709FFF4" }, { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, @@ -34,11 +35,12 @@ gleam_community_ansi = { version = "~> 1.4" } gleam_erlang = { version = "~> 0.24" } gleam_json = { version = "~> 1.0" } gleam_otp = { version = "~> 0.9" } +gleam_package_interface = { version = "~> 1.0"} gleam_stdlib = { version = "~> 0.34 or ~> 1.0" } gleeunit = { version = "~> 1.0" } glint = { version = "0.16.0" } justin = { version = "~> 1.0" } -shellout = { version = "~> 1.6"} +shellout = { version = "~> 1.6" } simplifile = { version = "~> 1.4" } spinner = { version = "~> 1.1" } tom = { version = "~> 0.3" } diff --git a/src/lustre/cli/build.gleam b/src/lustre/cli/build.gleam index 8d02b97..352329d 100644 --- a/src/lustre/cli/build.gleam +++ b/src/lustre/cli/build.gleam @@ -4,12 +4,13 @@ import filepath import gleam/dict import gleam/io import gleam/list +import gleam/package_interface.{type Type, Named, Variable} import gleam/result import gleam/string import glint.{type Command, CommandInput} import glint/flag import lustre/cli/esbuild -import lustre/cli/project.{type Module, type Type, Named, Variable} +import lustre/cli/project.{type Module} import lustre/cli/utils.{keep, replace, try} import lustre/cli/step.{type Step} import simplifile @@ -167,7 +168,7 @@ type Error { ComponentMissing(module: String) MainMissing(module: String) ModuleMissing(module: String) - NameIncorrectType(module: String, got: project.Type) + NameIncorrectType(module: String, got: Type) NameMissing(module: String) } diff --git a/src/lustre/cli/dev.gleam b/src/lustre/cli/dev.gleam index 0cbec11..4f49ce1 100644 --- a/src/lustre/cli/dev.gleam +++ b/src/lustre/cli/dev.gleam @@ -3,12 +3,13 @@ import filepath import gleam/dict import gleam/io +import gleam/package_interface.{type Type, Fn, Named, Variable} import gleam/string import glint.{type Command, CommandInput} import glint/flag import lustre/attribute.{attribute} import lustre/cli/esbuild -import lustre/cli/project.{type Module, type Type, Fn, Named, Variable} +import lustre/cli/project.{type Module} import lustre/cli/step import lustre/cli/utils.{guard, keep, map, replace, try} import lustre/element @@ -116,8 +117,8 @@ type Error { BuildError BundleError(esbuild.Error) MainMissing(module: String) - MainIncorrectType(module: String, got: project.Type) - MainBadAppType(module: String, got: project.Type) + MainIncorrectType(module: String, got: Type) + MainBadAppType(module: String, got: Type) ModuleMissing(module: String) } diff --git a/src/lustre/cli/project.gleam b/src/lustre/cli/project.gleam index 8d04244..2071588 100644 --- a/src/lustre/cli/project.gleam +++ b/src/lustre/cli/project.gleam @@ -2,11 +2,12 @@ import filepath import gleam/dict.{type Dict} -import gleam/dynamic.{type DecodeError, type Dynamic, DecodeError} +import gleam/dynamic.{type DecodeError, type Decoder, type Dynamic, DecodeError} import gleam/int import gleam/io import gleam/json import gleam/list +import gleam/package_interface.{type Type, Fn, Named, Tuple, Variable} import gleam/pair import gleam/result import gleam/string @@ -33,13 +34,6 @@ pub type Function { Function(parameters: List(Type), return: Type) } -pub type Type { - Named(name: String, package: String, module: String, parameters: List(Type)) - Variable(id: Int) - Fn(parameters: List(Type), return: Type) - Tuple(elements: List(Type)) -} - // COMMANDS -------------------------------------------------------------------- /// Compile the current project running the `gleam build` command. @@ -73,7 +67,7 @@ pub fn interface() -> Result(Interface, String) { ) let assert Ok(json) = simplifile.read(out) - let assert Ok(interface) = json.decode(json, decode_interface) + let assert Ok(interface) = json.decode(json, interface_decoder) Ok(interface) } @@ -159,82 +153,40 @@ pub fn type_to_string(type_: Type) -> String { // DECODERS -------------------------------------------------------------------- -fn decode_interface(dyn: Dynamic) -> Result(Interface, List(DecodeError)) { +fn interface_decoder(dyn: Dynamic) -> Result(Interface, List(DecodeError)) { dynamic.decode3( Interface, dynamic.field("name", dynamic.string), dynamic.field("version", dynamic.string), - dynamic.field("modules", dynamic.dict(dynamic.string, decode_module)), + dynamic.field("modules", string_dict(module_decoder)), )(dyn) } -fn decode_module(dyn: Dynamic) -> Result(Module, List(DecodeError)) { +fn module_decoder(dyn: Dynamic) -> Result(Module, List(DecodeError)) { dynamic.decode2( Module, dynamic.field( "constants", - dynamic.dict(dynamic.string, dynamic.field("type", decode_type)), + string_dict(dynamic.field("type", package_interface.type_decoder)), ), - dynamic.field("functions", dynamic.dict(dynamic.string, decode_function)), + dynamic.field("functions", string_dict(function_decoder)), )(dyn) } -fn decode_function(dyn: Dynamic) -> Result(Function, List(DecodeError)) { +fn function_decoder(dyn: Dynamic) -> Result(Function, List(DecodeError)) { dynamic.decode2( Function, - dynamic.field("parameters", dynamic.list(decode_labelled_argument)), - dynamic.field("return", decode_type), - )(dyn) -} - -fn decode_type(dyn: Dynamic) -> Result(Type, List(DecodeError)) { - use kind <- result.try(dynamic.field("kind", dynamic.string)(dyn)) - - case kind { - "named" -> decode_named_type(dyn) - "variable" -> decode_variable_type(dyn) - "fn" -> decode_fn_type(dyn) - "tuple" -> decode_tuple_type(dyn) - - _ -> - Error([ - DecodeError(found: kind, expected: "'named' | 'variable' | 'fn'", path: [ - "kind", - ]), - ]) - } -} - -fn decode_named_type(dyn: Dynamic) -> Result(Type, List(DecodeError)) { - dynamic.decode4( - Named, - dynamic.field("name", dynamic.string), - dynamic.field("package", dynamic.string), - dynamic.field("module", dynamic.string), - dynamic.field("parameters", dynamic.list(decode_type)), + dynamic.field("parameters", dynamic.list(labelled_argument_decoder)), + dynamic.field("return", package_interface.type_decoder), )(dyn) } -fn decode_variable_type(dyn: Dynamic) -> Result(Type, List(DecodeError)) { - dynamic.decode1(Variable, dynamic.field("id", dynamic.int))(dyn) -} - -fn decode_fn_type(dyn: Dynamic) -> Result(Type, List(DecodeError)) { - dynamic.decode2( - Fn, - dynamic.field("parameters", dynamic.list(decode_type)), - dynamic.field("return", decode_type), - )(dyn) -} - -fn decode_tuple_type(dyn: Dynamic) -> Result(Type, List(DecodeError)) { - dynamic.decode1(Tuple, dynamic.field("elements", dynamic.list(decode_type)))( - dyn, - ) -} - -fn decode_labelled_argument(dyn: Dynamic) -> Result(Type, List(DecodeError)) { +fn labelled_argument_decoder(dyn: Dynamic) -> Result(Type, List(DecodeError)) { // In this case we don't really care about the label, so we're just ignoring // it and returning the argument's type. - dynamic.field("type", decode_type)(dyn) + dynamic.field("type", package_interface.type_decoder)(dyn) +} + +fn string_dict(values: Decoder(a)) -> Decoder(Dict(String, a)) { + dynamic.dict(dynamic.string, values) } |