aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGiacomo Cavalieri <giacomo.cavalieri@icloud.com>2024-02-29 21:29:23 +0100
committerGitHub <noreply@github.com>2024-02-29 20:29:23 +0000
commit4970e2ab89c6b84487ec3d302e41603bedb1a950 (patch)
treefcc0c5a8e6c19f7120d6cb9f19c1d566770c71df /src
parent9baad8dc9c36c79190759d595a22681af95917c6 (diff)
downloadlustre-4970e2ab89c6b84487ec3d302e41603bedb1a950.tar.gz
lustre-4970e2ab89c6b84487ec3d302e41603bedb1a950.zip
🔀 Use `package-interface` package (#49)
Diffstat (limited to 'src')
-rw-r--r--src/lustre/cli/build.gleam5
-rw-r--r--src/lustre/cli/dev.gleam7
-rw-r--r--src/lustre/cli/project.gleam82
3 files changed, 24 insertions, 70 deletions
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)
}