Skip to content

Commit

Permalink
refactor: rename nature to kind
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Hivert <[email protected]>
  • Loading branch information
ghivert committed May 13, 2024
1 parent d2e434e commit ffe2fda
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import gleam/dynamic
import gleam/result

pub type Nature {
pub type Kind {
Function
TypeDefinition
TypeAlias
Constant
}

pub fn decode_nature(dyn) {
pub fn decode_kind(dyn) {
use str <- result.try(dynamic.string(dyn))
case str {
"function" -> Ok(Function)
Expand All @@ -19,8 +19,8 @@ pub fn decode_nature(dyn) {
}
}

pub fn display_nature(nature) {
case nature {
pub fn display_kind(kind) {
case kind {
Function -> "Function"
TypeDefinition -> "Type"
TypeAlias -> "Type Alias"
Expand Down
6 changes: 3 additions & 3 deletions apps/frontend/src/data/decoders/search_result.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import data/decoders/implementations.{type Implementations, Implementations}
import data/decoders/nature.{type Nature}
import data/decoders/kind.{type Kind}
import data/decoders/signature.{type Signature}
import gleam/dynamic
import gleam/option.{type Option}
Expand Down Expand Up @@ -38,7 +38,7 @@ pub type SearchResult {
documentation: String,
module_name: String,
name: String,
nature: Nature,
kind: Kind,
package_name: String,
json_signature: Signature,
metadata: Metadata,
Expand All @@ -58,7 +58,7 @@ pub fn decode_search_result(dyn) {
dynamic.field("documentation", dynamic.string),
dynamic.field("module_name", dynamic.string),
dynamic.field("name", dynamic.string),
dynamic.field("kind", nature.decode_nature),
dynamic.field("kind", kind.decode_kind),
dynamic.field("package_name", dynamic.string),
dynamic.field("json_signature", signature.decode_signature),
dynamic.field("metadata", decode_metadata),
Expand Down
84 changes: 50 additions & 34 deletions apps/frontend/src/frontend.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ pub fn main() {
|> options.timeout(5000)
|> grille_pain.setup()

let apply_debugger = fn(fun: fn(a, tardis.Instance) -> a) {
fn(app: a) {
result.map(debugger_, fun(app, _))
|> result.unwrap(app)
}
let apply_debugger = fn(app: a, fun: fn(a, tardis.Instance) -> a) {
result.map(debugger_, fun(app, _))
|> result.unwrap(app)
}

let assert Ok(_) =
Expand All @@ -58,35 +56,53 @@ pub fn main() {

fn update(model: Model, msg: Msg) {
case msg {
msg.UpdateInput(content) ->
model
|> model.update_input(content)
|> update.none()
msg.SubmitSearch -> {
use <- bool.guard(when: model.input == "", return: #(model, effect.none()))
http.expect_json(search_result.decode_search_results, msg.SearchResults)
|> http.get("http://localhost:3000/search?q=" <> model.input, _)
|> pair.new(model, _)
}
msg.Reset ->
model
|> model.reset()
|> update.none()
msg.SearchResults(search_results) -> {
let toast =
search_results
|> result.map_error(fn(error) {
error
|> toast_error.describe_http_error()
|> option.map(toast.error)
})
|> result.unwrap_error(option.None)
|> option.unwrap(effect.none())
search_results
|> result.map(model.update_search_results(model, _))
|> result.unwrap(model)
|> pair.new(toast)
}
msg.UpdateInput(content) -> update_input(model, content)
msg.SubmitSearch -> submit_search(model)
msg.Reset -> reset(model)
msg.None -> update.none(model)
msg.SearchResults(search_results) ->
handle_search_results(model, search_results)
}
}

fn update_input(model: Model, content: String) {
model
|> model.update_input(content)
|> update.none()
}

fn reset(model: Model) {
model
|> model.reset()
|> update.none()
}

fn submit_search(model: Model) {
use <- bool.guard(when: model.input == "", return: #(model, effect.none()))
http.expect_json(search_result.decode_search_results, msg.SearchResults)
|> http.get("http://localhost:3000/search?q=" <> model.input, _)
|> pair.new(model, _)
}

fn handle_search_results(
model: Model,
search_results: Result(search_result.SearchResults, http.HttpError),
) {
let toast = display_toast(search_results)
search_results
|> result.map(model.update_search_results(model, _))
|> result.unwrap(model)
|> pair.new(toast)
}

fn display_toast(
search_results: Result(search_result.SearchResults, http.HttpError),
) {
search_results
|> result.map_error(fn(error) {
toast_error.describe_http_error(error)
|> option.map(toast.error)
})
|> result.unwrap_error(option.None)
|> option.unwrap(effect.none())
}
22 changes: 10 additions & 12 deletions apps/frontend/src/frontend/documentation.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import frontend/documentation/styles as s
import gleam/io
import gleam/list
import gleam/string
import lustre/attribute as a
Expand All @@ -8,20 +6,20 @@ import lustre/element/html as h
@external(javascript, "../markdown.ffi.mjs", "convert")
fn converter(content: String) -> String

fn remove_leading_space(str: String) {
case str {
" " <> rest -> rest
str -> str
}
}

pub fn view(document: String) {
let content =
document
|> string.split("\n")
|> list.map(fn(s) {
case s {
" " <> rest -> rest
_ -> s
}
})
|> list.map(remove_leading_space)
|> string.join("\n")
|> converter()
h.div(
[a.attribute("dangerous-unescaped-html", content), a.class("documentation")],
[],
)
|> a.attribute("dangerous-unescaped-html", _)
h.div([content, a.class("documentation")], [])
}
5 changes: 2 additions & 3 deletions apps/frontend/src/frontend/view.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import data/decoders/nature
import data/decoders/kind
import data/decoders/search_result
import data/decoders/signature.{type Parameter, type Type, Parameter}
import data/model.{type Model}
Expand All @@ -10,7 +10,6 @@ import frontend/styles as s
import frontend/types as t
import gleam/bool
import gleam/int
import gleam/io
import gleam/list
import gleam/option.{None, Some}
import gleam/string
Expand Down Expand Up @@ -323,7 +322,7 @@ fn view_search_results(search_results: List(search_result.SearchResult)) {
use item <- list.map(search_results)
h.div([s.search_result()], [
h.div([s.search_details()], [
h.div([], [h.text(nature.display_nature(item.nature))]),
h.div([], [h.text(kind.display_kind(item.kind))]),
h.div([], [
t.dark_white(item.package_name <> "@" <> item.version),
t.dark_white("."),
Expand Down

0 comments on commit ffe2fda

Please sign in to comment.