Crate localghost

Source
Expand description
localghost logo

§Ergonomic Rust bindings to the Web platform.

This crate builds on wasm-bindgen, js-sys, and web-sys to provide high-level ergonomic bindings to all Web platform APIs. The goal is to empower library and framework authors to bring Rust to the web, being able to use familiar idioms exposed through a carefully designed API.

§Examples

use localghost::prelude::*;
use localghost::{log, net};
use std::io;

#[localghost::main]
async fn main() -> io::Result<()> {
    let res = net::Request::get("https://httpbin.org/uuid").send().await?;
    log::info!("status: {:?}", res.status());
    log::info!("body: {:?}", res.body_string().await?);
    Ok(())
}

For more examples see the examples directory.

§Getting started

In order to build a localghost project you need wasm-pack installed, and a fresh Rust project with a lib.rs file. Then add the following to your Cargo.toml:

[lib]
crate-type = ["cdylib"]

[dependencies]
localghost = "0.1.0"

You can then compile the project using wasm-pack build --target web and serve it from an HTML file using the following snippet:

<body>
  <script type="module">
    import init from "./pkg/<project_name>.js";
    init("./pkg/<project_name>.wasm");
  </script>
</body>

Modules§

dom
The browser’s Document Object Model.
events
DOM Event listeners.
fs
Filesystem manipulation operations.
keyboard
Browser keyboard API
log
Structured logging for the browser.
net
Browser networking APIs
prelude
The localghost prelude.
raw
Raw bindings to JS and the DOM.
task
Types and traits for working with asynchronous tasks.

Structs§

History
The Web History API.
Location
Representation of the location (URL) of the object it is linked to.

Attribute Macros§

main
Initializes an async main function.