use leptos::prelude::{MaybeSignal, Memo, ReadSignal, RwSignal, Signal}; use std::ops::{Deref, DerefMut}; pub struct OptionalProp(Option); impl Default for OptionalProp { fn default() -> Self { Self(None) } } impl Clone for OptionalProp { fn clone(&self) -> Self { Self(self.0.clone()) } } impl OptionalProp { pub fn map(self, f: F) -> Option where F: FnOnce(T) -> U, { self.0.map(f) } pub fn into_option(self) -> Option { self.0 } } impl Deref for OptionalProp { type Target = Option; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for OptionalProp { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl From for OptionalProp { fn from(value: T) -> Self { Self(Some(value)) } } impl From<&str> for OptionalProp { fn from(value: &str) -> Self { Self(Some(value.to_string())) } } impl From<&str> for OptionalProp> { fn from(value: &str) -> Self { Self(Some(MaybeSignal::from(value.to_string()))) } } impl From for OptionalProp> { fn from(value: String) -> Self { Self(Some(MaybeSignal::from(value))) } } impl From> for OptionalProp> { fn from(value: ReadSignal) -> Self { Self(Some(MaybeSignal::from(value))) } } impl From> for OptionalProp> { fn from(value: RwSignal) -> Self { Self(Some(MaybeSignal::from(value))) } } impl From> for OptionalProp> { fn from(value: Memo) -> Self { Self(Some(MaybeSignal::from(value))) } } impl From> for OptionalProp> { fn from(value: Signal) -> Self { Self(Some(MaybeSignal::from(value))) } } impl From> for OptionalProp { fn from(value: Option) -> Self { Self(value) } } #[cfg(test)] mod test { use super::OptionalProp; use leptos::prelude::MaybeSignal; #[test] fn from() { let _prop: OptionalProp> = "prop".into(); let _prop: OptionalProp> = "prop".to_string().into(); let _prop: OptionalProp = "prop".into(); } }