Skip to content

Commit

Permalink
fix: some documents
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanceras committed Apr 7, 2024
1 parent ebc83f3 commit f1649c3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ wasm-bindgen-futures = "0.4.31"
regex = "1"
sauron-html-parser = { path = "crates/html-parser" }
sauron = { path = ".", features = ["test-fixtures", "html-parser", "log-patches"] }
doc-comment = "0.3.3"

[dev-dependencies.web-sys]
version = "0.3"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub fn start() {
```

`index.html`

```html
<!doctype html>
<html>
Expand Down Expand Up @@ -166,7 +167,7 @@ Build using
wasm-pack build --target web --release
```
Serve using
```
```sh
basic-http-server -a 0.0.0.0:4000
```
Then navigate to http://localhost:4000
Expand Down
26 changes: 19 additions & 7 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ cargo install wasm-pack
```

We also use `basic-http-server` to easily serve static files locally.

```sh
cargo install basic-http-server
```

## Creating a new project
We will create a new project called `hello`.
```

```sh
cargo new --lib hello
```
This will create a new folder `hello` with set of files necessary to be compiled as a rust project.
Try to compile this project to test if we installed rust correctly.
```

```sh
cd hello
cargo build
```
Expand Down Expand Up @@ -91,7 +94,9 @@ use sauron::{node, wasm_bindgen, Application, Cmd, Node, Program};

struct App;

impl Application<()> for App {
impl Application for App {
type MSG = ();

fn view(&self) -> Node<()> {
node! {
<p>
Expand All @@ -100,7 +105,7 @@ impl Application<()> for App {
}
}

fn update(&mut self, _msg: ()) -> Cmd<Self, ()> {
fn update(&mut self, _msg: ()) -> Cmd<()> {
Cmd::none()
}
}
Expand All @@ -114,15 +119,18 @@ Take notice of the `view` method. Here we are using `node!` macro which takes ht
We implement the `Application` trait for our `App` so that we can implement the required methods necessary to tell sauron how out app behaves.

To compile, we issue the command:
```shell

```sh
wasm-pack build --release --target=web
```

As mentioned earlier,`wasm-pack` helps us simplify the process of compiling rust for targetting web applications.
A folder `./pkg` is then created inside our project. This will contain the resulting compiled files.
We only pay attention to the 2 files, named derived from the given package name `<package_name>.js` and `<package_name>_bg.wasm`.
In our case, it will be `hello.js` and `hello_bg.wasm`.

We need to reference this file in our page. Let's create `index.html` in our project.

```html
<!DOCTYPE html>
<html>
Expand All @@ -138,20 +146,24 @@ We need to reference this file in our page. Let's create `index.html` in our pro
</body>
</html>
```

Take note, that we are using `<script type=module>`.
Another thing to take note is that we referencing `./pkg/hello.js` from the `./pkg` folder.
If you changed the package name of the crate, you will also need to change the filename here.
Behind the scene, `./pkg/hello.js` will take care of loading `./pkg/hello_bg.wasm` in the background.

Recompile our webapp, issue this command everytime you have changes to the rust code.
```shell

```sh
wasm-pack build --release --target=web
```

Finally, we serve the files using `basic-http-server`
```shell

```sh
basic-http-server
```

By default, it serves the page in port `4000`
Navigate to http://127.0.0.1:4000 to see the 'hello' message.
There you have it, you've built the bare minimum web application using sauron.
Expand Down
7 changes: 5 additions & 2 deletions docs/intermediate-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ the type we send into the `update` method.
Append this code to `src/lib.rs`.

```rust
impl Application<Msg> for App {
use sauron::*;

impl Application for App {
type MSG = Msg;
fn view(&self) -> Node<Msg> {
node! {
<main>
Expand All @@ -80,7 +83,7 @@ impl Application<Msg> for App {
}
}

fn update(&mut self, msg: Msg) -> Cmd<Self, Msg> {
fn update(&mut self, msg: Msg) -> Cmd<Msg> {
match msg {
Msg::Increment => self.count += 1,
Msg::Decrement => self.count -= 1,
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
#[doc(inline)]
pub use prelude::*;

// NOTE: This test the code written in the README file
#[cfg(doctest)]
doc_comment::doctest!("../README.md");
//#[cfg(doctest)]
//doc_comment::doctest!("../docs/getting-started.md");
//#[cfg(doctest)]
//doc_comment::doctest!("../docs/intermediate-example.md");

/// prelude
pub mod prelude {
pub use sauron_core::prelude::*;
Expand Down

0 comments on commit f1649c3

Please sign in to comment.