Skip to content

Commit

Permalink
Update examples (add failure's Error to the format module)
Browse files Browse the repository at this point in the history
  • Loading branch information
therustmonk committed Feb 22, 2018
1 parent c9e8543 commit e4c37cd
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/dashboard/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Denis Kolodin <[email protected]>"]

[dependencies]
failure = "0.1"
serde = "1"
serde_derive = "1"
yew = { path = "../../.." }
9 changes: 6 additions & 3 deletions examples/dashboard/client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
extern crate failure;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate yew;

use failure::Error;
use yew::prelude::*;
use yew::format::{Nothing, Json};
use yew::services::Task;
Expand Down Expand Up @@ -31,8 +33,8 @@ enum WsAction {
enum Msg {
FetchData,
WsAction(WsAction),
FetchReady(Result<DataFromFile, ()>),
WsReady(Result<WsResponse, ()>),
FetchReady(Result<DataFromFile, Error>),
WsReady(Result<WsResponse, Error>),
Ignore,
}

Expand Down Expand Up @@ -78,8 +80,9 @@ impl Component<Context> for Model {
match msg {
Msg::FetchData => {
self.fetching = true;
let callback = context.send_back(|response: Response<Json<Result<DataFromFile, ()>>>| {
let callback = context.send_back(|response: Response<Json<Result<DataFromFile, Error>>>| {
let (meta, Json(data)) = response.into_parts();
println!("META: {:?}, {:?}", meta, data);
if meta.status.is_success() {
Msg::FetchReady(data)
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/mount_point/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
authors = ["Ben Berman <[email protected]>"]

[dependencies]
stdweb = "0.4"
yew = { path = "../.." }
stdweb = "0.3"
10 changes: 5 additions & 5 deletions examples/mount_point/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate yew;
extern crate stdweb;

use yew::prelude::*;
use stdweb::web::{IElement, document, INode};
use stdweb::web::{IElement, INode, IParentNode, document};

type Context = ();

Expand Down Expand Up @@ -49,10 +49,10 @@ impl Renderable<Context, Model> for Model {

fn main() {
yew::initialize();
let body = document().query_selector("body").unwrap();
let body = document().query_selector("body").unwrap().unwrap();

// This canvas won't be overwritten by yew!
let canvas = document().create_element("canvas");
let canvas = document().create_element("canvas").unwrap();
body.append_child(&canvas);

js! {
Expand All @@ -65,8 +65,8 @@ fn main() {
};

let mount_class = "mount-point";
let mount_point = document().create_element("div");
mount_point.class_list().add(mount_class);
let mount_point = document().create_element("div").unwrap();
mount_point.class_list().add(mount_class).unwrap();
body.append_child(&mount_point);

let app: App<_, Model> = App::new(());
Expand Down
3 changes: 2 additions & 1 deletion examples/npm_and_rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ version = "0.1.0"
authors = ["Denis Kolodin <[email protected]>"]

[dependencies]
failure = "0.1"
serde = "1"
serde_derive = "1"
stdweb = "0.4"
yew = { path = "../.." }
stdweb = "0.3"
5 changes: 3 additions & 2 deletions examples/npm_and_rest/src/gravatar.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use failure::Error;
use yew::format::{Nothing, Json};
use yew::services::fetch::{FetchService, FetchTask, Request, Response};
use yew::html::Callback;
Expand Down Expand Up @@ -28,9 +29,9 @@ impl GravatarService {
}
}

pub fn profile(&mut self, hash: &str, callback: Callback<Result<Profile, ()>>) -> FetchTask {
pub fn profile(&mut self, hash: &str, callback: Callback<Result<Profile, Error>>) -> FetchTask {
let url = format!("https://gravatar.com/{}", hash);
let handler = move |response: Response<Json<Result<Profile, ()>>>| {
let handler = move |response: Response<Json<Result<Profile, Error>>>| {
let (_, Json(data)) = response.into_parts();
callback.emit(data)
};
Expand Down
4 changes: 3 additions & 1 deletion examples/npm_and_rest/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
extern crate failure;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate stdweb;
#[macro_use]
extern crate yew;

use failure::Error;
use yew::prelude::*;
use yew::services::fetch::FetchTask;

Expand All @@ -27,7 +29,7 @@ struct Model {

enum Msg {
Gravatar,
GravatarReady(Result<Profile, ()>),
GravatarReady(Result<Profile, Error>),
Exchanges,
}

Expand Down
3 changes: 2 additions & 1 deletion examples/showcase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function ctrl_c() {

for example in */ ; do
cd $example
cargo web start --target-webasm-emscripten &
cargo update
cargo web start --target wasm32-unknown-emscripten &
PID=$!
wait $PID
cd ..
Expand Down
2 changes: 1 addition & 1 deletion examples/two_apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ version = "0.1.0"
authors = ["Denis Kolodin <[email protected]>"]

[dependencies]
stdweb = "0.3"
stdweb = "0.4"
yew = { path = "../.." }
4 changes: 2 additions & 2 deletions examples/two_apps/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate yew;

use std::rc::Rc;
use std::cell::RefCell;
use stdweb::web::document;
use stdweb::web::{IParentNode, document};
// Use `html` module directly. No use `App`.
use yew::html::*;

Expand Down Expand Up @@ -67,7 +67,7 @@ impl Renderable<Context, Model> for Model {
}

fn mount_app(selector: &'static str, app: Scope<Context, Model>) {
let element = document().query_selector(selector).unwrap();
let element = document().query_selector(selector).unwrap().unwrap();
app.mount(element);
}

Expand Down
8 changes: 4 additions & 4 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ where
}
}

impl<T> From<Restorable> for Json<Result<T, ()>>
impl<T> From<Restorable> for Json<Result<T, Error>>
where
T: for <'de> Deserialize<'de>
{
fn from(value: Restorable) -> Self {
match value {
Ok(data) => {
Json(serde_json::from_str(&data).map_err(drop))
Json(serde_json::from_str(&data).map_err(Error::from))
}
Err(_reason) => {
Json(Err(()))
Err(reason) => {
Json(Err(reason))
}
}
}
Expand Down

0 comments on commit e4c37cd

Please sign in to comment.