From 3195b50d12b457959a4334b300dda6853e1ae85c Mon Sep 17 00:00:00 2001 From: AlephAlpha Date: Sun, 19 Jan 2020 21:39:08 +0800 Subject: [PATCH] Add Transformer impls to handle handle Option properties (#878) * Add Transformer impls to handle handle Option properties * Add test * More tests --- src/virtual_dom/vcomp.rs | 27 +++++++++ tests/macro/html-component-fail.stderr | 68 ++++++++++++--------- tests/macro/html-component-pass.rs | 81 ++++++++++++++++++++------ 3 files changed, 129 insertions(+), 47 deletions(-) diff --git a/src/virtual_dom/vcomp.rs b/src/virtual_dom/vcomp.rs index bd533fa1a48..0cfe773ea37 100644 --- a/src/virtual_dom/vcomp.rs +++ b/src/virtual_dom/vcomp.rs @@ -249,6 +249,33 @@ impl<'a> Transformer<&'a str, String> for VComp { } } +impl Transformer> for VComp { + fn transform(from: T) -> Option { + Some(from) + } +} + +impl<'a, T> Transformer<&'a T, Option> for VComp +where + T: Clone, +{ + fn transform(from: &T) -> Option { + Some(from.clone()) + } +} + +impl<'a> Transformer<&'a str, Option> for VComp { + fn transform(from: &'a str) -> Option { + Some(from.to_owned()) + } +} + +impl<'a> Transformer, Option> for VComp { + fn transform(from: Option<&'a str>) -> Option { + from.map(|s| s.to_owned()) + } +} + impl PartialEq for VComp { fn eq(&self, other: &VComp) -> bool { self.type_id == other.type_id diff --git a/tests/macro/html-component-fail.stderr b/tests/macro/html-component-fail.stderr index 11fbdc5a112..24fcb2989c8 100644 --- a/tests/macro/html-component-fail.stderr +++ b/tests/macro/html-component-fail.stderr @@ -127,38 +127,47 @@ error[E0599]: no method named `unknown` found for type `ChildPropertiesBuilder }; | ^^^^^^^ method not found in `ChildPropertiesBuilder` -error[E0308]: mismatched types +error[E0277]: the trait bound `yew::virtual_dom::vcomp::VComp: yew::virtual_dom::Transformer<(), std::string::String>` is not satisfied --> $DIR/html-component-fail.rs:66:33 | 66 | html! { }; - | ^^ expected struct `std::string::String`, found () - | - = note: expected type `std::string::String` - found type `()` + | ^^ the trait `yew::virtual_dom::Transformer<(), std::string::String>` is not implemented for `yew::virtual_dom::vcomp::VComp` + | + = help: the following implementations were found: + > + >> + >> + > + and 3 others + = note: required by `yew::virtual_dom::Transformer::transform` -error[E0308]: mismatched types +error[E0277]: the trait bound `yew::virtual_dom::vcomp::VComp: yew::virtual_dom::Transformer<{integer}, std::string::String>` is not satisfied --> $DIR/html-component-fail.rs:67:33 | 67 | html! { }; - | ^ - | | - | expected struct `std::string::String`, found integer - | help: try using a conversion method: `3.to_string()` - | - = note: expected type `std::string::String` - found type `{integer}` + | ^ the trait `yew::virtual_dom::Transformer<{integer}, std::string::String>` is not implemented for `yew::virtual_dom::vcomp::VComp` + | + = help: the following implementations were found: + > + >> + >> + > + and 3 others + = note: required by `yew::virtual_dom::Transformer::transform` -error[E0308]: mismatched types +error[E0277]: the trait bound `yew::virtual_dom::vcomp::VComp: yew::virtual_dom::Transformer<{integer}, std::string::String>` is not satisfied --> $DIR/html-component-fail.rs:68:33 | 68 | html! { }; - | ^^^ - | | - | expected struct `std::string::String`, found integer - | help: try using a conversion method: `{3}.to_string()` - | - = note: expected type `std::string::String` - found type `{integer}` + | ^^^ the trait `yew::virtual_dom::Transformer<{integer}, std::string::String>` is not implemented for `yew::virtual_dom::vcomp::VComp` + | + = help: the following implementations were found: + > + >> + >> + > + and 3 others + = note: required by `yew::virtual_dom::Transformer::transform` error[E0308]: mismatched types --> $DIR/html-component-fail.rs:69:30 @@ -169,16 +178,19 @@ error[E0308]: mismatched types = note: expected type `yew::html::NodeRef` found type `()` -error[E0308]: mismatched types +error[E0277]: the trait bound `yew::virtual_dom::vcomp::VComp: yew::virtual_dom::Transformer` is not satisfied --> $DIR/html-component-fail.rs:71:24 | 71 | html! { }; - | ^^^^ expected i32, found u32 - | -help: you can convert an `u32` to `i32` and panic if the converted value wouldn't fit - | -71 | html! { }; - | ^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^ the trait `yew::virtual_dom::Transformer` is not implemented for `yew::virtual_dom::vcomp::VComp` + | + = help: the following implementations were found: + > + >> + >> + > + and 3 others + = note: required by `yew::virtual_dom::Transformer::transform` error[E0599]: no method named `string` found for type `ChildPropertiesBuilder` in the current scope --> $DIR/html-component-fail.rs:72:20 diff --git a/tests/macro/html-component-pass.rs b/tests/macro/html-component-pass.rs index 9bc72ee5f61..68283badd52 100644 --- a/tests/macro/html-component-pass.rs +++ b/tests/macro/html-component-pass.rs @@ -1,8 +1,8 @@ #![recursion_limit = "256"] use std::marker::PhantomData; -use yew::prelude::*; use yew::html::ChildrenRenderer; +use yew::prelude::*; use yew::virtual_dom::{VChild, VNode}; pub struct Generic { @@ -13,18 +13,30 @@ impl Component for Generic { type Message = (); type Properties = (); - fn create(_: Self::Properties, _: ComponentLink) -> Self { unimplemented!() } - fn update(&mut self, _: Self::Message) -> ShouldRender { unimplemented!() } - fn view(&self) -> Html { unimplemented!() } + fn create(_: Self::Properties, _: ComponentLink) -> Self { + unimplemented!() + } + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!() + } + fn view(&self) -> Html { + unimplemented!() + } } impl Component for Generic> { type Message = (); type Properties = (); - fn create(_: Self::Properties, _: ComponentLink) -> Self { unimplemented!() } - fn update(&mut self, _: Self::Message) -> ShouldRender { unimplemented!() } - fn view(&self) -> Html { unimplemented!() } + fn create(_: Self::Properties, _: ComponentLink) -> Self { + unimplemented!() + } + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!() + } + fn view(&self) -> Html { + unimplemented!() + } } #[derive(Clone, Properties, Default)] @@ -39,9 +51,15 @@ impl Component for Container { type Message = (); type Properties = ContainerProperties; - fn create(_: Self::Properties, _: ComponentLink) -> Self { unimplemented!() } - fn update(&mut self, _: Self::Message) -> ShouldRender { unimplemented!() } - fn view(&self) -> Html { unimplemented!() } + fn create(_: Self::Properties, _: ComponentLink) -> Self { + unimplemented!() + } + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!() + } + fn view(&self) -> Html { + unimplemented!() + } } #[derive(Clone)] @@ -76,6 +94,7 @@ pub struct ChildProperties { pub string: String, #[props(required)] pub int: i32, + pub opt_str: Option, pub vec: Vec, pub optional_callback: Option>, } @@ -85,9 +104,15 @@ impl Component for Child { type Message = (); type Properties = ChildProperties; - fn create(_: Self::Properties, _: ComponentLink) -> Self { unimplemented!() } - fn update(&mut self, _: Self::Message) -> ShouldRender { unimplemented!() } - fn view(&self) -> Html { unimplemented!() } + fn create(_: Self::Properties, _: ComponentLink) -> Self { + unimplemented!() + } + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!() + } + fn view(&self) -> Html { + unimplemented!() + } } pub struct AltChild; @@ -95,9 +120,15 @@ impl Component for AltChild { type Message = (); type Properties = (); - fn create(_: Self::Properties, _: ComponentLink) -> Self { unimplemented!() } - fn update(&mut self, _: Self::Message) -> ShouldRender { unimplemented!() } - fn view(&self) -> Html { unimplemented!() } + fn create(_: Self::Properties, _: ComponentLink) -> Self { + unimplemented!() + } + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!() + } + fn view(&self) -> Html { + unimplemented!() + } } #[derive(Clone, Properties, Default)] @@ -112,9 +143,15 @@ impl Component for ChildContainer { type Message = (); type Properties = ChildContainerProperties; - fn create(_: Self::Properties, _: ComponentLink) -> Self { unimplemented!() } - fn update(&mut self, _: Self::Message) -> ShouldRender { unimplemented!() } - fn view(&self) -> Html { unimplemented!() } + fn create(_: Self::Properties, _: ComponentLink) -> Self { + unimplemented!() + } + fn update(&mut self, _: Self::Message) -> ShouldRender { + unimplemented!() + } + fn view(&self) -> Html { + unimplemented!() + } } mod scoped { @@ -158,6 +195,11 @@ fn compile_pass() { + + + + + // backwards compat @@ -172,6 +214,7 @@ fn compile_pass() { <> + };