Add error to JsValue conversion and example (#691)
This commit is contained in:
committed by
Sean McArthur
parent
57300edbc4
commit
43f2ff083c
5
examples/wasm_github_fetch/.gitignore
vendored
Normal file
5
examples/wasm_github_fetch/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
node_modules
|
||||||
|
pkg
|
||||||
|
target
|
||||||
|
Cargo.lock
|
||||||
|
*.swp
|
||||||
18
examples/wasm_github_fetch/Cargo.toml
Normal file
18
examples/wasm_github_fetch/Cargo.toml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "wasm"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["John Gallagher <john.willis.gallagher@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# Config mostly pulled from: https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/Cargo.toml
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate-type = ["cdylib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
reqwest = {path = "../../"}
|
||||||
|
serde = { version = "1.0.101", features = ["derive"] }
|
||||||
|
serde_derive = "^1.0.59"
|
||||||
|
wasm-bindgen-futures = "0.4.1"
|
||||||
|
serde_json = "1.0.41"
|
||||||
|
wasm-bindgen = { version = "0.2.51", features = ["serde-serialize"] }
|
||||||
11
examples/wasm_github_fetch/README.md
Normal file
11
examples/wasm_github_fetch/README.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
## Example usage of Reqwest from WASM
|
||||||
|
|
||||||
|
You can build the example locally with:
|
||||||
|
|
||||||
|
|
||||||
|
npm run serve
|
||||||
|
|
||||||
|
and then visiting http://localhost:8080 in a browser should run the example!
|
||||||
|
|
||||||
|
|
||||||
|
This example is loosely based off of [this example](https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs), an example usage of `fetch` from `wasm-bindgen`.
|
||||||
12
examples/wasm_github_fetch/index.js
Normal file
12
examples/wasm_github_fetch/index.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
const rust = import('./pkg');
|
||||||
|
|
||||||
|
rust
|
||||||
|
.then(m => {
|
||||||
|
return m.run().then((data) => {
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
console.log("The latest commit to the wasm-bindgen %s branch is:", data.name);
|
||||||
|
console.log("%s, authored by %s <%s>", data.commit.sha, data.commit.commit.author.name, data.commit.commit.author.email);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(console.error);
|
||||||
6079
examples/wasm_github_fetch/package-lock.json
generated
Normal file
6079
examples/wasm_github_fetch/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
examples/wasm_github_fetch/package.json
Normal file
14
examples/wasm_github_fetch/package.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"build": "webpack",
|
||||||
|
"serve": "webpack-dev-server"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@wasm-tool/wasm-pack-plugin": "1.0.1",
|
||||||
|
"text-encoding": "^0.7.0",
|
||||||
|
"html-webpack-plugin": "^3.2.0",
|
||||||
|
"webpack": "^4.29.4",
|
||||||
|
"webpack-cli": "^3.1.1",
|
||||||
|
"webpack-dev-server": "^3.1.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
46
examples/wasm_github_fetch/src/lib.rs
Normal file
46
examples/wasm_github_fetch/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
// NOTE: This test is a clone of https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs
|
||||||
|
// but uses Reqwest instead of the web_sys fetch api directly
|
||||||
|
|
||||||
|
/// A struct to hold some data from the github Branch API.
|
||||||
|
///
|
||||||
|
/// Note how we don't have to define every member -- serde will ignore extra
|
||||||
|
/// data when deserializing
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Branch {
|
||||||
|
pub name: String,
|
||||||
|
pub commit: Commit,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Commit {
|
||||||
|
pub sha: String,
|
||||||
|
pub commit: CommitDetails,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct CommitDetails {
|
||||||
|
pub author: Signature,
|
||||||
|
pub committer: Signature,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Signature {
|
||||||
|
pub name: String,
|
||||||
|
pub email: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub async fn run() -> Result<JsValue, JsValue> {
|
||||||
|
let res = reqwest::Client::new()
|
||||||
|
.get("https://api.github.com/repos/rustwasm/wasm-bindgen/branches/master")
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let text = res.text().await?;
|
||||||
|
let branch_info: Branch = serde_json::from_str(&text).unwrap();
|
||||||
|
|
||||||
|
Ok(JsValue::from_serde(&branch_info).unwrap())
|
||||||
|
}
|
||||||
25
examples/wasm_github_fetch/webpack.config.js
Normal file
25
examples/wasm_github_fetch/webpack.config.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
const path = require('path');
|
||||||
|
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||||
|
const webpack = require('webpack');
|
||||||
|
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
entry: './index.js',
|
||||||
|
output: {
|
||||||
|
path: path.resolve(__dirname, 'dist'),
|
||||||
|
filename: 'index.js',
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new HtmlWebpackPlugin(),
|
||||||
|
new WasmPackPlugin({
|
||||||
|
crateDirectory: path.resolve(__dirname, ".")
|
||||||
|
}),
|
||||||
|
// Have this example work in Edge which doesn't ship `TextEncoder` or
|
||||||
|
// `TextDecoder` at this time.
|
||||||
|
new webpack.ProvidePlugin({
|
||||||
|
TextDecoder: ['text-encoding', 'TextDecoder'],
|
||||||
|
TextEncoder: ['text-encoding', 'TextEncoder']
|
||||||
|
})
|
||||||
|
],
|
||||||
|
mode: 'development'
|
||||||
|
};
|
||||||
14
src/error.rs
14
src/error.rs
@@ -170,6 +170,20 @@ impl StdError for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
impl From<crate::error::Error> for wasm_bindgen::JsValue {
|
||||||
|
fn from(err: Error) -> wasm_bindgen::JsValue {
|
||||||
|
js_sys::Error::from(err).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
impl From<crate::error::Error> for js_sys::Error {
|
||||||
|
fn from(err: Error) -> js_sys::Error {
|
||||||
|
js_sys::Error::new(&format!("{}", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum Kind {
|
pub(crate) enum Kind {
|
||||||
Builder,
|
Builder,
|
||||||
|
|||||||
Reference in New Issue
Block a user