Skip to content

Commit 360fbeb

Browse files
committed
Better presentation of rust nif & wasm output
1 parent bb8ef97 commit 360fbeb

File tree

6 files changed

+87
-13
lines changed

6 files changed

+87
-13
lines changed

lib/components_guide_web.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ defmodule ComponentsGuideWeb do
111111
import ComponentsGuideWeb.AssetsHelpers
112112
alias ComponentsGuideWeb.StylingHelpers, as: Styling
113113
alias ComponentsGuideWeb.FormattingHelpers, as: Format
114+
# alias ComponentsGuideWeb.PrimitiveHelpers, as: Primitives
114115

115116
def markdown!(markdown) do
116117
Earmark.as_html!(markdown)

lib/components_guide_web/controllers/cheatsheets_controller.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule ComponentsGuideWeb.CheatsheetsController do
22
use ComponentsGuideWeb, :controller
33
require Logger
44

5-
@wasm_constant """
5+
@wasm_source """
66
(module
77
(func (export "answer") (result i32)
88
i32.const 42
@@ -11,7 +11,7 @@ defmodule ComponentsGuideWeb.CheatsheetsController do
1111
"""
1212

1313
def index(conn, _params) do
14-
render(conn, "index.html", article: "intro", wasm_constant: @wasm_constant)
14+
render(conn, "index.html", article: "intro", wasm_source: @wasm_source)
1515
end
1616

1717
@articles ["rxjs", "error-messages", "cloud-limits"]
@@ -30,6 +30,8 @@ defmodule ComponentsGuideWeb.CheatsheetsView do
3030
use ComponentsGuideWeb.Snippets
3131
alias ComponentsGuideWeb.ThemeView
3232

33+
defdelegate code_block(assigns), to: ComponentsGuideWeb.PrimitiveHelpers
34+
3335
def header_styles() do
3436
ThemeView.banner_styles(:cheatsheets)
3537
end

lib/components_guide_web/template_engines/markdown_engine.ex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ defmodule ComponentsGuideWeb.TemplateEngines.MarkdownEngine do
9191

9292
html |> EEx.compile_string(engine: Phoenix.HTML.Engine, file: path, line: 1)
9393

94+
# TODO: use Heex?
95+
# unless Macro.Env.has_var?(__CALLER__, {:assigns, nil}) do
96+
# raise "~H requires a variable named \"assigns\" to exist and be set to a map"
97+
# end
98+
99+
# options = [
100+
# engine: Phoenix.LiveView.HTMLEngine,
101+
# file: __CALLER__.file,
102+
# line: __CALLER__.line + 1,
103+
# module: __CALLER__.module,
104+
# indentation: meta[:indentation] || 0
105+
# ]
106+
107+
# EEx.compile_string(expr, options)
108+
94109
# case live? do
95110
# true ->
96111
# html |> EEx.compile_string(engine: Phoenix.HTML.Engine, file: path, line: 1)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>Various cheatsheets will be added over time. If you have ideas for cheatsheets, please submit them.</p>
2+
3+
<h2>Elixir</h2>
4+
<h3>Add numbers</h3>
5+
<.code_block lang="elixir">
6+
5 + 11
7+
</.code_block>
8+
<.code_block lang="elixir" code={(5 + 11) |> inspect()} />
9+
10+
<hr />
11+
12+
<h2>Rust</h2>
13+
<h3>Add numbers</h3>
14+
<.code_block lang="rust">
15+
fn add(a: i64, b: i64) -> i64 {
16+
a + b
17+
}
18+
</.code_block>
19+
<.code_block lang="elixir">
20+
Math.add(5, 11)
21+
</.code_block>
22+
<.code_block lang="elixir" code={ComponentsGuide.Rustler.Math.add(5, 11) |> inspect()} />
23+
24+
<h3>Reverse string</h3>
25+
<.code_block lang="rust">
26+
fn reverse_string(a: String) -> String {
27+
return a.chars().rev().collect();
28+
}
29+
</.code_block>
30+
<.code_block lang="elixir">
31+
Math.reverse_string("hello")
32+
</.code_block>
33+
<.code_block lang="elixir" code={ComponentsGuide.Rustler.Math.reverse_string("hello") |> inspect()} />
34+
35+
<hr />
36+
37+
<h2>WebAssembly + Wasmtime running via Rust</h2>
38+
<.code_block lang="wasm" code={@wasm_source} />
39+
<.code_block lang="elixir">
40+
Math.wasm_example(@wasm_source, "answer")
41+
</.code_block>
42+
<.code_block lang="elixir" code={ComponentsGuide.Rustler.Math.wasm_example(@wasm_source, "answer") |> inspect()} />

lib/components_guide_web/templates/cheatsheets/intro.html.md

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule ComponentsGuideWeb.PrimitiveHelpers do
2+
@moduledoc """
3+
Conveniences for translating and building error messages.
4+
"""
5+
6+
use Phoenix.HTML
7+
use Phoenix.Component
8+
9+
def code_block(assigns) do
10+
# assigns = %{}
11+
# content_tag(:pre, inspect(assigns))
12+
~H"""
13+
<pre class={"language-#{@lang}"}><code><%=
14+
if assigns[:code] do
15+
assigns[:code]
16+
else
17+
render_slot(@inner_block) |> then(fn
18+
%{static: [code]} -> String.trim(code)
19+
%{static: strings} when is_list(strings) -> strings |> Enum.join() |> String.trim()
20+
end)
21+
end
22+
%></code></pre>
23+
"""
24+
end
25+
end

0 commit comments

Comments
 (0)