Compare commits
4 Commits
a0a3ebb856
...
2ddbfc2fdb
Author | SHA1 | Date |
---|---|---|
Daan Vanoverloop | 2ddbfc2fdb | |
Daan Vanoverloop | 49d473e213 | |
Daan Vanoverloop | 59dfb89ee6 | |
Daan Vanoverloop | 004885e6e7 |
|
@ -274,6 +274,15 @@ dependencies = [
|
|||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "convert_case"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cookie"
|
||||
version = "0.16.0"
|
||||
|
@ -469,6 +478,17 @@ dependencies = [
|
|||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "displaydoc"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3bf95dc3f046b9da4f2d51833c0d3547d8564ef6910f5c1ed130306a75b92886"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenv"
|
||||
version = "0.15.0"
|
||||
|
@ -1136,11 +1156,27 @@ dependencies = [
|
|||
name = "lan_party_core"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"displaydoc",
|
||||
"lan_party_macros",
|
||||
"log",
|
||||
"paste",
|
||||
"rocket",
|
||||
"schemars",
|
||||
"serde",
|
||||
"sycamore",
|
||||
"thiserror",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lan_party_macros"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"convert_case",
|
||||
"paste",
|
||||
"quote",
|
||||
"sycamore",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
members = [
|
||||
"backend",
|
||||
"core",
|
||||
"web"
|
||||
"web",
|
||||
"macros"
|
||||
]
|
||||
|
||||
|
|
|
@ -9,10 +9,16 @@ edition = "2021"
|
|||
serde = ["dep:serde"]
|
||||
openapi = ["dep:schemars"]
|
||||
rocket = ["dep:rocket", "serde"]
|
||||
sycamore = ["dep:sycamore", "dep:web-sys", "dep:lan_party_macros"]
|
||||
|
||||
[dependencies]
|
||||
schemars = { version = "0.8", optional = true }
|
||||
serde = { version = "1", optional = true }
|
||||
rocket = { version = "0.5.0-rc.2", features = ["json"], optional = true }
|
||||
sycamore = { version = "0.8.1", features = ["serde", "suspense"], optional = true }
|
||||
web-sys = { version = "0.3", features = ["Request", "RequestInit", "RequestMode", "Response", "Headers", "HtmlSelectElement"], optional = true }
|
||||
lan_party_macros = { path = "../macros", optional = true }
|
||||
paste = "1"
|
||||
thiserror = "1.0"
|
||||
displaydoc = "0.2"
|
||||
log = "0.4"
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
//use log::debug;
|
||||
use sycamore::prelude::*;
|
||||
use web_sys::Event;
|
||||
|
||||
#[derive(Prop)]
|
||||
pub struct ButtonProps<F: FnMut(Event)> {
|
||||
pub onclick: F,
|
||||
#[builder(default)]
|
||||
pub text: String,
|
||||
#[builder(default)]
|
||||
pub icon: String,
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn Button<'a, G: Html, F: 'a + FnMut(Event)>(cx: Scope<'a>, props: ButtonProps<F>) -> View<G> {
|
||||
let mut icon_class = String::from("mdi ");
|
||||
|
||||
if !props.icon.is_empty() {
|
||||
icon_class.push_str(&props.icon);
|
||||
}
|
||||
|
||||
view! { cx,
|
||||
button(on:click=props.onclick) {
|
||||
span(class=icon_class)
|
||||
span { (props.text) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Prop)]
|
||||
pub struct TableProps<'a, G: Html> {
|
||||
pub headers: Vec<String>,
|
||||
|
||||
pub children: Children<'a, G>,
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn Table<'a, G: Html>(cx: Scope<'a>, props: TableProps<'a, G>) -> View<G> {
|
||||
let children = props.children.call(cx);
|
||||
|
||||
view! { cx,
|
||||
table {
|
||||
thead {
|
||||
tr {
|
||||
(View::new_fragment(props.headers.iter().cloned().map(|header| view! { cx,
|
||||
th(scope="col") {
|
||||
(header)
|
||||
}
|
||||
}).collect()))
|
||||
}
|
||||
}
|
||||
tbody {
|
||||
(children)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Prop)]
|
||||
pub struct BlockProps<'a, G: Html> {
|
||||
pub title: String,
|
||||
pub children: Children<'a, G>,
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn Block<'a, G: Html>(cx: Scope<'a>, props: BlockProps<'a, G>) -> View<G> {
|
||||
let children = props.children.call(cx);
|
||||
|
||||
view! { cx,
|
||||
details {
|
||||
summary { (props.title) }
|
||||
p { (children) }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,28 @@
|
|||
use std::{marker::PhantomData, str::FromStr};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
hash::Hash,
|
||||
marker::PhantomData,
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use crate::components::Block;
|
||||
use lan_party_core::event::{
|
||||
free_for_all_game::FreeForAllGameSpec, team_game::TeamGameSpec, test::TestSpec, EventSpec,
|
||||
EventTypeSpec,
|
||||
};
|
||||
use log::debug;
|
||||
use paste::paste;
|
||||
use sycamore::prelude::*;
|
||||
|
||||
use crate::components::{BlockProps, Button};
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::{Edit, EditProps, Editable, Editor, IntoEdit};
|
||||
pub use crate::{
|
||||
components::Block, edit_enum, edit_fields, edit_struct, editable, link_fields,
|
||||
link_variants,
|
||||
};
|
||||
pub use paste::paste;
|
||||
pub use sycamore::prelude::*;
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! editable {
|
||||
($type:ty => $editor:ty) => {
|
||||
impl<'a, G: Html> Editable<'a, G> for $type {
|
||||
|
@ -17,6 +31,7 @@ macro_rules! editable {
|
|||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! edit_fields {
|
||||
($cx:ident, $(($name:expr, $prop:expr)),* $(,)?) => {
|
||||
view! { $cx,
|
||||
|
@ -30,6 +45,7 @@ macro_rules! edit_fields {
|
|||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! link_fields {
|
||||
($cx:ident, $($field:ident),* $(,)? => $state:ident as $t:ident) => {
|
||||
$(let $field = create_signal($cx, $state.get_untracked().$field.clone());)*
|
||||
|
@ -43,6 +59,7 @@ macro_rules! link_fields {
|
|||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! edit_struct {
|
||||
($struct:ident => $(($name:expr, $prop:ident)),* $(,)?) => {
|
||||
paste! {
|
||||
|
@ -65,8 +82,9 @@ macro_rules! edit_struct {
|
|||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! link_variants {
|
||||
($cx:ident, $selected:ident => $($index:literal: $var_name:ident = $variant:ident: $var_type:ty),* $(,)? => $state:ident as $t:ident) => {
|
||||
($cx:ident, $selected:ident => $(($var_name:ident = $variant:ident: $var_type:ty)),* $(,)? => $state:ident as $t:ident) => {
|
||||
let $selected = create_signal($cx, String::from("0"));
|
||||
|
||||
$(let $var_name = if let $t::$variant(v) = $state.get_untracked().as_ref().clone() {
|
||||
|
@ -76,17 +94,18 @@ macro_rules! link_variants {
|
|||
};)*
|
||||
|
||||
create_effect($cx, || {
|
||||
debug!("{:#?}", $selected.get());
|
||||
match $selected.get().as_str() {
|
||||
$(stringify!($index) => $state.set($t::$variant($var_name.get().as_ref().clone())),)*
|
||||
_ => unreachable!()
|
||||
$(stringify!($var_name) => $state.set($t::$variant($var_name.get().as_ref().clone())),)*
|
||||
//_ => unreachable!()
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! edit_enum {
|
||||
($enum:ident => $selected:ident => $($index:literal: $var_name:ident = $variant:ident: $var_type:ty),* $(,)?) => {
|
||||
($enum:ident => $selected:ident => $($var_name:ident = $variant:ident: $var_type:ty),* $(,)?) => {
|
||||
paste! {
|
||||
pub struct [<$enum Edit>];
|
||||
|
||||
|
@ -95,18 +114,19 @@ macro_rules! edit_enum {
|
|||
let state = props.state;
|
||||
|
||||
link_variants!(cx, $selected =>
|
||||
$($index: $var_name = $variant: $var_type,)*
|
||||
$(($var_name = $variant: $var_type),)*
|
||||
=> state as $enum
|
||||
);
|
||||
|
||||
view! { cx,
|
||||
Block(title=stringify!($enum).to_string()) {
|
||||
select(bind:value=$selected) {
|
||||
$(option(value={stringify!($index)}, selected=$index==0) { (stringify!($variant)) })*
|
||||
$(option(value={stringify!($var_name)}, selected=true) { (stringify!($variant)) })*
|
||||
}
|
||||
(match $selected.get().as_str() {
|
||||
$(stringify!($index) => $var_name.edit(cx),)*
|
||||
_ => unreachable!()
|
||||
$(stringify!($var_name) => $var_name.edit(cx),)*
|
||||
//_ => unreachable!()
|
||||
_ => view! { cx, }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -165,18 +185,6 @@ pub trait Editable<'a, G: Html>: Sized {
|
|||
type Editor: Editor<'a, G, Self>;
|
||||
}
|
||||
|
||||
edit_struct!(EventSpec => ("Name", name), ("Description", description), ("Event type", event_type));
|
||||
|
||||
edit_enum!(EventTypeSpec => selected =>
|
||||
0: test = Test: TestSpec,
|
||||
1: team_game = TeamGame: TeamGameSpec,
|
||||
2: free_for_all_game = FreeForAllGame: FreeForAllGameSpec
|
||||
);
|
||||
|
||||
edit_struct!(TestSpec => ("Number of players", num_players));
|
||||
edit_struct!(TeamGameSpec => ("Win rewards", win_rewards));
|
||||
edit_struct!(FreeForAllGameSpec => );
|
||||
|
||||
pub struct StringEdit;
|
||||
|
||||
impl<'a, G: Html> Editor<'a, G, String> for StringEdit {
|
||||
|
@ -202,6 +210,10 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, G: Html, T: for<'b> Editable<'b, G>> Editable<'a, G> for Option<T> {
|
||||
type Editor = StubEdit;
|
||||
}
|
||||
|
||||
pub struct InputEdit;
|
||||
|
||||
impl<'a, G: Html, T> Editor<'a, G, T> for InputEdit
|
||||
|
@ -251,7 +263,7 @@ pub struct VecEdit;
|
|||
impl<'a, G, T, I> Editor<'a, G, I> for VecEdit
|
||||
where
|
||||
G: Html,
|
||||
T: Editable<'a, G> + Clone + PartialEq + 'a,
|
||||
T: for<'b> Editable<'b, G> + Clone + PartialEq + Default + std::fmt::Debug + 'a,
|
||||
I: IntoIterator<Item = T> + FromIterator<T> + Clone,
|
||||
{
|
||||
fn edit(cx: Scope<'a>, props: EditProps<'a, I>) -> View<G> {
|
||||
|
@ -266,9 +278,6 @@ where
|
|||
.map(|x| create_signal(cx, x))
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
//let signal = create_signal(cx, 0);
|
||||
//let vec2 = vec.get().as_ref().clone();
|
||||
//let signal = create_ref(cx, vec.get(0).unwrap());
|
||||
|
||||
create_effect(cx, || {
|
||||
props.state.set(
|
||||
|
@ -281,30 +290,175 @@ where
|
|||
)
|
||||
});
|
||||
|
||||
/*
|
||||
let onadd = move |_| vec.modify().push(create_signal(cx, T::default()));
|
||||
let onremove = move |item: &'a Signal<T>| {
|
||||
move |_| {
|
||||
let cloned = vec.get().as_ref().clone();
|
||||
vec.set(
|
||||
cloned
|
||||
.into_iter()
|
||||
.filter(|x| x.get().as_ref() != item.get().as_ref())
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
Block(
|
||||
cx,
|
||||
BlockProps {
|
||||
title: "List".into(),
|
||||
children: Children::new(cx, move |_| {
|
||||
view! { cx,
|
||||
div {
|
||||
Indexed(
|
||||
iterable=vec,
|
||||
view=|cx: BoundedScope<'_, 'a>, x| {
|
||||
let signal = create_ref(cx, x);
|
||||
view=move |cx: BoundedScope<'_, 'a>, x: &'a Signal<T>| {
|
||||
view! { cx,
|
||||
(T::edit(cx, x.into()))
|
||||
(x.edit(cx))
|
||||
Button(onclick=onremove(x), icon="mdi-delete".into())
|
||||
br()
|
||||
}
|
||||
},
|
||||
)
|
||||
Button(onclick=onadd, text="Add new".into(), icon="mdi-plus".into())
|
||||
}
|
||||
*/
|
||||
}
|
||||
}),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, G, T> Editable<'a, G> for Vec<T>
|
||||
where
|
||||
G: Html,
|
||||
T: Editable<'a, G> + Clone + PartialEq + 'a,
|
||||
T: for<'b> Editable<'b, G> + Clone + PartialEq + Default + std::fmt::Debug + 'a,
|
||||
{
|
||||
type Editor = VecEdit;
|
||||
}
|
||||
|
||||
impl<'a, G, T> Editable<'a, G> for HashSet<T>
|
||||
where
|
||||
G: Html,
|
||||
T: for<'b> Editable<'b, G> + Clone + PartialEq + Default + Hash + Eq + std::fmt::Debug + 'a,
|
||||
{
|
||||
type Editor = VecEdit;
|
||||
}
|
||||
|
||||
impl<'a, G, K, V> Editable<'a, G> for HashMap<K, V>
|
||||
where
|
||||
G: Html,
|
||||
K: Clone + Hash + Eq,
|
||||
V: Clone,
|
||||
(K, V):
|
||||
for<'b> Editable<'b, G> + Clone + PartialEq + Default + Hash + Eq + std::fmt::Debug + 'a,
|
||||
{
|
||||
type Editor = VecEdit;
|
||||
}
|
||||
|
||||
pub struct TupleEdit;
|
||||
|
||||
impl<'a, G: Html, A: for<'b> Editable<'b, G> + Clone, B: for<'b> Editable<'b, G> + Clone>
|
||||
Editor<'a, G, (A, B)> for TupleEdit
|
||||
{
|
||||
fn edit(cx: Scope<'a>, props: EditProps<'a, (A, B)>) -> View<G> {
|
||||
let state = props.state;
|
||||
let (a, b) = state.get_untracked().as_ref().clone();
|
||||
|
||||
let a = create_signal(cx, a.clone());
|
||||
let b = create_signal(cx, b.clone());
|
||||
|
||||
create_effect(cx, || {
|
||||
props
|
||||
.state
|
||||
.set((a.get().as_ref().clone(), b.get().as_ref().clone()))
|
||||
});
|
||||
|
||||
view! { cx,
|
||||
Block(title="Tuple".into()) {
|
||||
(a.edit(cx))
|
||||
(b.edit(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LabeledEdit<T> {
|
||||
_t: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<'a, G, T, U> Editor<'a, G, U> for LabeledEdit<T>
|
||||
where
|
||||
G: Html,
|
||||
T: for<'b> Editable<'b, G> + Clone + 'a,
|
||||
U: Into<WithLabel<T>> + From<WithLabel<T>> + Clone,
|
||||
{
|
||||
fn edit(cx: Scope<'a>, props: EditProps<'a, U>) -> View<G> {
|
||||
let cloned: U = props.state.get_untracked().as_ref().clone();
|
||||
let state: WithLabel<T> = cloned.into();
|
||||
let label = state.label.clone();
|
||||
let inner = create_signal(cx, state.inner.clone());
|
||||
|
||||
{
|
||||
let label = label.clone();
|
||||
create_effect(cx, move || {
|
||||
props.state.set(
|
||||
WithLabel {
|
||||
label: label.clone(),
|
||||
inner: inner.get().as_ref().clone(),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
let label = create_signal(cx, label);
|
||||
|
||||
view! { cx,
|
||||
div {
|
||||
(label.get())
|
||||
(inner.edit(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WithLabel<T: Clone> {
|
||||
label: String,
|
||||
inner: T,
|
||||
}
|
||||
|
||||
impl<'a, G: Html, T: for<'b> Editable<'b, G> + Clone + 'a> Editable<'a, G> for WithLabel<T> {
|
||||
type Editor = LabeledEdit<T>;
|
||||
}
|
||||
|
||||
impl<'a, G, A, B> Editable<'a, G> for (A, B)
|
||||
where
|
||||
G: Html,
|
||||
A: for<'b> Editable<'b, G> + Clone,
|
||||
B: for<'b> Editable<'b, G> + Clone,
|
||||
{
|
||||
type Editor = TupleEdit;
|
||||
}
|
||||
|
||||
pub struct User(String);
|
||||
|
||||
impl From<WithLabel<String>> for User {
|
||||
fn from(l: WithLabel<String>) -> Self {
|
||||
User(l.inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<User> for WithLabel<String> {
|
||||
fn from(u: User) -> Self {
|
||||
WithLabel {
|
||||
label: "User".into(),
|
||||
inner: u.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct Test {
|
||||
inner: TestInner,
|
|
@ -1,4 +1,10 @@
|
|||
#[cfg(feature = "sycamore")]
|
||||
use crate::edit::prelude::*;
|
||||
use crate::util::PartyError;
|
||||
#[cfg(feature = "sycamore")]
|
||||
use crate::view::prelude::*;
|
||||
#[cfg(feature = "sycamore")]
|
||||
use lan_party_macros::{WebEdit, WebView};
|
||||
use paste::paste;
|
||||
#[cfg(feature = "openapi")]
|
||||
use schemars::JsonSchema;
|
||||
|
@ -16,9 +22,10 @@ pub struct EventOutcome {
|
|||
/// # Event
|
||||
///
|
||||
/// An event in which participants can win or lose points
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebView))]
|
||||
pub struct Event {
|
||||
/// Has this event concluded?
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
|
@ -29,7 +36,6 @@ pub struct Event {
|
|||
/// Description of the event
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
pub description: String,
|
||||
/// Event type
|
||||
pub event_type: EventType,
|
||||
}
|
||||
|
||||
|
@ -51,8 +57,11 @@ impl Event {
|
|||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct EventSpec {
|
||||
/// Name of the event
|
||||
pub name: String,
|
||||
/// Description of the event
|
||||
pub description: String,
|
||||
pub event_type: EventTypeSpec,
|
||||
}
|
||||
|
@ -66,6 +75,7 @@ macro_rules! events {
|
|||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum EventTypeSpec {
|
||||
$($name($module::[<$name Spec>]),)*
|
||||
}
|
||||
|
@ -76,6 +86,7 @@ macro_rules! events {
|
|||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum EventUpdate {
|
||||
$($name($module::[<$name Update>]),)*
|
||||
}
|
||||
|
@ -83,9 +94,10 @@ macro_rules! events {
|
|||
/// # EventType
|
||||
///
|
||||
/// An enumeration of event types
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum EventType {
|
||||
$($name($module::$name),)*
|
||||
}
|
||||
|
@ -149,9 +161,10 @@ pub trait EventTrait {
|
|||
pub mod test {
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct Test {
|
||||
pub num_players: i64,
|
||||
}
|
||||
|
@ -159,13 +172,15 @@ pub mod test {
|
|||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct TestSpec {
|
||||
pub num_players: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Default)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct TestUpdate {
|
||||
pub win_game: bool,
|
||||
}
|
||||
|
@ -205,9 +220,10 @@ pub mod team_game {
|
|||
*,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct TeamGame {
|
||||
/// Map of teams with a name as key and an array of players as value
|
||||
pub teams: HashMap<String, Vec<String>>,
|
||||
|
@ -219,6 +235,7 @@ pub mod team_game {
|
|||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct TeamGameSpec {
|
||||
/// Map of teams with a name as key and an array of players as value
|
||||
pub teams: HashMap<String, Vec<String>>,
|
||||
|
@ -239,21 +256,38 @@ pub mod team_game {
|
|||
pub lose_rewards: Vec<i64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct TeamGameUpdateSetTeam {
|
||||
pub team: String,
|
||||
pub members: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum TeamGameUpdateInner {
|
||||
/// Add or replace a team with the given name and array of members
|
||||
SetTeam { team: String, members: Vec<String> },
|
||||
SetTeam(TeamGameUpdateSetTeam),
|
||||
|
||||
/// Remove team with given name
|
||||
RemoveTeam(String),
|
||||
}
|
||||
|
||||
impl Default for TeamGameUpdateInner {
|
||||
fn default() -> Self {
|
||||
Self::SetTeam(TeamGameUpdateSetTeam::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum TeamGameFfaInheritedUpdate {
|
||||
/// Change the ranking and scores
|
||||
Ranking(FreeForAllGameUpdateRanking),
|
||||
|
@ -261,17 +295,34 @@ pub mod team_game {
|
|||
Rewards(FreeForAllGameUpdateRewards),
|
||||
}
|
||||
|
||||
impl Default for TeamGameFfaInheritedUpdate {
|
||||
fn default() -> Self {
|
||||
Self::Ranking(FreeForAllGameUpdateRanking::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum TeamGameUpdate {
|
||||
/// # Team
|
||||
///
|
||||
/// Team specific updates
|
||||
Team(TeamGameUpdateInner),
|
||||
/// # Other
|
||||
///
|
||||
/// Inherited from FreeForAllGame
|
||||
Ffa(TeamGameFfaInheritedUpdate),
|
||||
}
|
||||
|
||||
impl Default for TeamGameUpdate {
|
||||
fn default() -> Self {
|
||||
TeamGameUpdate::Team(TeamGameUpdateInner::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl EventTrait for TeamGame {
|
||||
type Spec = TeamGameSpec;
|
||||
type Update = TeamGameUpdate;
|
||||
|
@ -300,12 +351,12 @@ pub mod team_game {
|
|||
}
|
||||
},
|
||||
TeamGameUpdate::Team(update) => match update {
|
||||
TeamGameUpdateInner::SetTeam { team, members } => {
|
||||
TeamGameUpdateInner::SetTeam(u) => {
|
||||
self.ffa_game
|
||||
.apply_update(FreeForAllGameUpdate::Participants(
|
||||
FreeForAllGameUpdateParticipants::AddParticipant(team.clone()),
|
||||
FreeForAllGameUpdateParticipants::AddParticipant(u.team.clone()),
|
||||
))?;
|
||||
self.teams.insert(team, members);
|
||||
self.teams.insert(u.team, u.members);
|
||||
Ok(())
|
||||
}
|
||||
TeamGameUpdateInner::RemoveTeam(team) => {
|
||||
|
@ -347,6 +398,7 @@ pub mod free_for_all_game {
|
|||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum FreeForAllGameRanking {
|
||||
/// Ranking of participants by user name or team name (first element is first place, second element is second
|
||||
/// place, etc.)
|
||||
|
@ -355,6 +407,12 @@ pub mod free_for_all_game {
|
|||
Scores(HashMap<String, i64>),
|
||||
}
|
||||
|
||||
impl Default for FreeForAllGameRanking {
|
||||
fn default() -> Self {
|
||||
Self::Ranking(Vec::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl FreeForAllGameRanking {
|
||||
pub fn is_valid(&self, participants: &HashSet<String>) -> bool {
|
||||
match self {
|
||||
|
@ -364,9 +422,10 @@ pub mod free_for_all_game {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct FreeForAllGame {
|
||||
/// Ranking of participants by user name or team name (first element is first place, second element is second
|
||||
/// place, etc.)
|
||||
|
@ -388,6 +447,7 @@ pub mod free_for_all_game {
|
|||
#[derive(Clone, Debug, PartialEq, Default)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub struct FreeForAllGameSpec {
|
||||
/// Array of user ids that participate in the game
|
||||
pub participants: HashSet<String>,
|
||||
|
@ -405,6 +465,7 @@ pub mod free_for_all_game {
|
|||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum FreeForAllGameUpdateRanking {
|
||||
/// Replace the current ranking with the given ranking
|
||||
SetRanking(FreeForAllGameRanking),
|
||||
|
@ -413,9 +474,16 @@ pub mod free_for_all_game {
|
|||
ScoreDelta(HashMap<String, i64>),
|
||||
}
|
||||
|
||||
impl Default for FreeForAllGameUpdateRanking {
|
||||
fn default() -> Self {
|
||||
Self::SetRanking(FreeForAllGameRanking::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum FreeForAllGameUpdateRewards {
|
||||
/// Set rewards for winning the game
|
||||
SetWinRewards(Vec<i64>),
|
||||
|
@ -424,9 +492,16 @@ pub mod free_for_all_game {
|
|||
SetLoseRewards(Vec<i64>),
|
||||
}
|
||||
|
||||
impl Default for FreeForAllGameUpdateRewards {
|
||||
fn default() -> Self {
|
||||
Self::SetWinRewards(Vec::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum FreeForAllGameUpdateParticipants {
|
||||
/// Set list of participants participating in the game
|
||||
SetParticipants(HashSet<String>),
|
||||
|
@ -438,10 +513,17 @@ pub mod free_for_all_game {
|
|||
RemoveParticipant(String),
|
||||
}
|
||||
|
||||
impl Default for FreeForAllGameUpdateParticipants {
|
||||
fn default() -> Self {
|
||||
Self::SetParticipants(HashSet::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(feature = "openapi", derive(JsonSchema))]
|
||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "serde", serde(untagged))]
|
||||
#[cfg_attr(feature = "sycamore", derive(WebEdit, WebView))]
|
||||
pub enum FreeForAllGameUpdate {
|
||||
/// Change the ranking and scores
|
||||
Ranking(FreeForAllGameUpdateRanking),
|
||||
|
@ -451,6 +533,12 @@ pub mod free_for_all_game {
|
|||
Participants(FreeForAllGameUpdateParticipants),
|
||||
}
|
||||
|
||||
impl Default for FreeForAllGameUpdate {
|
||||
fn default() -> Self {
|
||||
Self::Ranking(FreeForAllGameUpdateRanking::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl EventTrait for FreeForAllGame {
|
||||
type Spec = FreeForAllGameSpec;
|
||||
type Update = FreeForAllGameUpdate;
|
||||
|
@ -560,3 +648,9 @@ impl Default for EventTypeSpec {
|
|||
Self::Test(test::TestSpec::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for EventUpdate {
|
||||
fn default() -> Self {
|
||||
Self::Test(test::TestUpdate::default())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,4 +2,13 @@ pub mod event;
|
|||
pub mod user;
|
||||
pub mod util;
|
||||
|
||||
#[cfg(feature = "sycamore")]
|
||||
pub mod edit;
|
||||
|
||||
#[cfg(feature = "sycamore")]
|
||||
pub mod view;
|
||||
|
||||
#[cfg(feature = "sycamore")]
|
||||
pub mod components;
|
||||
|
||||
pub use util::PartyError;
|
||||
|
|
|
@ -0,0 +1,205 @@
|
|||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
hash::Hash,
|
||||
marker::PhantomData,
|
||||
};
|
||||
use sycamore::view::IntoView as _;
|
||||
|
||||
use sycamore::{builder::prelude::*, prelude::*};
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::{IntoView, ViewProps, Viewable, Viewer, WebView};
|
||||
pub use crate::components::Block;
|
||||
pub use paste::paste;
|
||||
pub use sycamore::prelude::*;
|
||||
}
|
||||
|
||||
use crate::components::{Block, BlockProps};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! viewable {
|
||||
($type:ty => $viewer:ty) => {
|
||||
impl<'a, G: Html> Viewable<'a, G> for $type {
|
||||
type Viewer = $viewer;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Prop)]
|
||||
pub struct ViewProps<'a, T> {
|
||||
pub state: &'a T,
|
||||
}
|
||||
|
||||
impl<'a, T> From<&'a T> for ViewProps<'a, T> {
|
||||
fn from(state: &'a T) -> Self {
|
||||
ViewProps { state }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoView<'a, G: Html> {
|
||||
fn view(self, cx: Scope<'a>) -> View<G>;
|
||||
}
|
||||
|
||||
impl<'a, G: Html, T: Viewable<'a, G>> IntoView<'a, G> for &'a T
|
||||
where
|
||||
ViewProps<'a, T>: From<&'a T>,
|
||||
{
|
||||
fn view(self, cx: Scope<'a>) -> View<G> {
|
||||
T::view(cx, self.into())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WebView<'a, G: Html>: Sized {
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, Self>) -> View<G>;
|
||||
}
|
||||
|
||||
impl<'a, G, E, Type> WebView<'a, G> for Type
|
||||
where
|
||||
G: Html,
|
||||
E: Viewer<'a, G, Type>,
|
||||
Type: Viewable<'a, G, Viewer = E>,
|
||||
{
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, Self>) -> View<G> {
|
||||
E::view(cx, props)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Viewer<'a, G: Html, Type>: Sized {
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, Type>) -> View<G>;
|
||||
}
|
||||
|
||||
pub trait Viewable<'a, G: Html>: Sized {
|
||||
type Viewer: Viewer<'a, G, Self>;
|
||||
}
|
||||
|
||||
pub struct StringView;
|
||||
|
||||
impl<'a, G: Html, T: 'a> Viewer<'a, G, T> for StringView
|
||||
where
|
||||
T: sycamore::view::IntoView<G>,
|
||||
{
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, T>) -> View<G> {
|
||||
view! { cx,
|
||||
(props.state.create())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
viewable!(String => StringView);
|
||||
|
||||
viewable!(i64 => StringView);
|
||||
viewable!(i32 => StringView);
|
||||
viewable!(isize => StringView);
|
||||
|
||||
viewable!(u64 => StringView);
|
||||
viewable!(u32 => StringView);
|
||||
viewable!(usize => StringView);
|
||||
|
||||
viewable!(f64 => StringView);
|
||||
viewable!(f32 => StringView);
|
||||
|
||||
pub struct BoolView;
|
||||
|
||||
impl<'a, G: Html> Viewer<'a, G, bool> for BoolView {
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, bool>) -> View<G> {
|
||||
view! { cx,
|
||||
input(type="checkbox", checked=*props.state, disabled=true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
viewable!(bool => BoolView);
|
||||
|
||||
pub struct VecView;
|
||||
|
||||
impl<'a, G, T, I> Viewer<'a, G, I> for VecView
|
||||
where
|
||||
G: Html,
|
||||
T: for<'b> Viewable<'b, G> + Clone + PartialEq + 'a,
|
||||
I: IntoIterator<Item = T> + FromIterator<T> + Clone,
|
||||
{
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, I>) -> View<G> {
|
||||
Block(
|
||||
cx,
|
||||
BlockProps {
|
||||
title: "List".into(),
|
||||
children: Children::new(cx, move |_| {
|
||||
view! { cx,
|
||||
//Block(title="List".into()) {
|
||||
div {
|
||||
(View::new_fragment(props.state.clone().into_iter().map(|x| create_ref(cx, x)).map(|x| {
|
||||
div().c(x.view(cx)).c(br()).view(cx)
|
||||
}).collect()))
|
||||
}
|
||||
//}
|
||||
}
|
||||
}),
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, G, T> Viewable<'a, G> for Vec<T>
|
||||
where
|
||||
G: Html,
|
||||
T: for<'b> Viewable<'b, G> + Clone + PartialEq + 'a,
|
||||
{
|
||||
type Viewer = VecView;
|
||||
}
|
||||
|
||||
impl<'a, G, T> Viewable<'a, G> for HashSet<T>
|
||||
where
|
||||
G: Html,
|
||||
T: for<'b> Viewable<'b, G> + Clone + PartialEq + Hash + Eq + 'a,
|
||||
{
|
||||
type Viewer = VecView;
|
||||
}
|
||||
|
||||
impl<'a, G, K, V> Viewable<'a, G> for HashMap<K, V>
|
||||
where
|
||||
G: Html,
|
||||
K: Clone + Hash + Eq,
|
||||
V: Clone,
|
||||
(K, V): for<'b> Viewable<'b, G> + Clone + PartialEq + Hash + Eq + 'a,
|
||||
{
|
||||
type Viewer = VecView;
|
||||
}
|
||||
|
||||
pub struct TupleView;
|
||||
|
||||
impl<'a, G: Html, A: for<'b> Viewable<'b, G> + Clone, B: for<'b> Viewable<'b, G> + Clone>
|
||||
Viewer<'a, G, (A, B)> for TupleView
|
||||
{
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, (A, B)>) -> View<G> {
|
||||
view! { cx,
|
||||
Block(title="Tuple".into()) {
|
||||
(props.state.0.view(cx))
|
||||
(props.state.1.view(cx))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, G, A, B> Viewable<'a, G> for (A, B)
|
||||
where
|
||||
G: Html,
|
||||
A: for<'b> Viewable<'b, G> + Clone,
|
||||
B: for<'b> Viewable<'b, G> + Clone,
|
||||
{
|
||||
type Viewer = TupleView;
|
||||
}
|
||||
|
||||
pub struct OptionView;
|
||||
|
||||
impl<'a, G: Html, T: for<'b> Viewable<'b, G> + Clone> Viewer<'a, G, Option<T>> for OptionView {
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, Option<T>>) -> View<G> {
|
||||
match props.state {
|
||||
Some(x) => view! { cx, (x.view(cx)) },
|
||||
None => view! { cx, "None" },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, G: Html, T: for<'b> Viewable<'b, G> + Clone> Viewable<'a, G> for Option<T> {
|
||||
type Viewer = OptionView;
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
[package]
|
||||
name = "lan_party_macros"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
syn = { version = "1.0", features = ["full", "extra-traits"] }
|
||||
quote = "1.0"
|
||||
sycamore = { version = "0.8.1", features = ["serde", "suspense"] }
|
||||
paste = "1.0"
|
||||
convert_case = "0.6"
|
||||
#lan_party_core = { path = "../core", features = ["sycamore"] }
|
|
@ -0,0 +1,505 @@
|
|||
mod edit;
|
||||
|
||||
use convert_case::{Case, Casing};
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{__private::TokenStream as TokenStream2, format_ident, quote};
|
||||
use syn::{token::Do, Attribute, DataEnum, DataStruct, Fields, Ident, Path, Type};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Documentation {
|
||||
Title(String),
|
||||
Description(String),
|
||||
None,
|
||||
}
|
||||
|
||||
impl Documentation {
|
||||
fn parse(attr: &Attribute) -> Documentation {
|
||||
if !attr.path.is_ident("doc") {
|
||||
return Documentation::None;
|
||||
}
|
||||
|
||||
let text = attr.tokens.to_string();
|
||||
|
||||
let text = text.trim_matches(|c: char| c == '\"' || c == '=' || c.is_whitespace());
|
||||
|
||||
match text.get(0..1) {
|
||||
Some("#") => Documentation::Title(text.trim_matches('#').trim().into()),
|
||||
Some(&_) => Documentation::Description(text.trim().into()),
|
||||
None => Documentation::None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_title_description(attrs: &[Attribute]) -> (Option<String>, Option<String>) {
|
||||
let docs: Vec<_> = attrs.iter().map(Documentation::parse).collect();
|
||||
|
||||
let mut title = None;
|
||||
let mut description: Option<String> = None;
|
||||
|
||||
for doc in docs {
|
||||
match doc {
|
||||
Documentation::Title(t) => title = Some(t),
|
||||
Documentation::Description(d) => {
|
||||
if description.is_some() {
|
||||
description.as_mut().unwrap().push(' ');
|
||||
} else {
|
||||
let _ = description.insert(String::new());
|
||||
}
|
||||
description.as_mut().unwrap().push_str(&d);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
(title, description)
|
||||
}
|
||||
|
||||
struct ItemProps {
|
||||
name: Ident,
|
||||
title: String,
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
struct StructField {
|
||||
name: Ident,
|
||||
name_str: String,
|
||||
title: Option<String>,
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
struct EnumVariant {
|
||||
variant: Ident,
|
||||
variant_lower: Ident,
|
||||
inner: Type,
|
||||
title: Option<String>,
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
fn struct_edit(props: &ItemProps, s: DataStruct) -> TokenStream2 {
|
||||
let ItemProps {
|
||||
name,
|
||||
title,
|
||||
description,
|
||||
} = props;
|
||||
|
||||
let fields = s.fields.iter().map(|f| {
|
||||
let name = f
|
||||
.ident
|
||||
.as_ref()
|
||||
.expect("each struct field must be named")
|
||||
.clone();
|
||||
let name_str = name.to_string();
|
||||
let (title, description) = get_title_description(&f.attrs);
|
||||
|
||||
StructField {
|
||||
name_str,
|
||||
name,
|
||||
title,
|
||||
description,
|
||||
}
|
||||
});
|
||||
|
||||
let fields_view = fields.clone().map(|f| {
|
||||
let title = f.title.unwrap_or(f.name_str.to_case(Case::Title));
|
||||
let description = if let Some(d) = f.description {
|
||||
quote! {
|
||||
span(class="description") {
|
||||
" " #d
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
let name = f.name;
|
||||
quote! {
|
||||
p {
|
||||
label { (#title) br() #description }
|
||||
(#name.edit(cx))
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let signals = fields.clone().map(|f| {
|
||||
let name = f.name;
|
||||
quote! {
|
||||
let #name = create_signal(cx, state.get_untracked().#name.clone());
|
||||
}
|
||||
});
|
||||
|
||||
let effect_fields = fields.clone().map(|f| {
|
||||
let name = f.name;
|
||||
quote! {
|
||||
#name: #name.get().as_ref().clone()
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
let state = props.state;
|
||||
|
||||
#(#signals)*
|
||||
|
||||
create_effect(cx, || {
|
||||
state.set(#name {
|
||||
#(#effect_fields,)*
|
||||
..Default::default()
|
||||
});
|
||||
});
|
||||
|
||||
view! { cx,
|
||||
Block(title=#title.to_string()) {
|
||||
p(class="description") {
|
||||
#description
|
||||
}
|
||||
#(#fields_view)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn enum_edit(props: &ItemProps, e: DataEnum) -> TokenStream2 {
|
||||
let ItemProps {
|
||||
name,
|
||||
title,
|
||||
description,
|
||||
} = props;
|
||||
|
||||
let variants = e.variants.iter().map(|v| {
|
||||
let variant = v.ident.clone();
|
||||
|
||||
let inner = match &v.fields {
|
||||
Fields::Unnamed(u) => u.unnamed.first().expect("the should be a field").ty.clone(),
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
let (title, description) = get_title_description(&v.attrs);
|
||||
let variant_lower = format_ident!("{}", variant.to_string().to_case(Case::Snake));
|
||||
EnumVariant {
|
||||
variant_lower,
|
||||
variant,
|
||||
inner,
|
||||
title,
|
||||
description,
|
||||
}
|
||||
});
|
||||
|
||||
let first = variants.clone().next().unwrap().variant_lower;
|
||||
let first_str = first.to_string();
|
||||
|
||||
let options = variants.clone().map(|v| {
|
||||
let lower = v.variant_lower;
|
||||
let title = v
|
||||
.title
|
||||
.unwrap_or(v.variant.to_string().to_case(Case::Title));
|
||||
let selected = first == lower;
|
||||
quote! { option(value={stringify!(#lower)}, selected=#selected) { (#title) } }
|
||||
});
|
||||
|
||||
let view_match = variants.clone().map(|v| {
|
||||
let lower = v.variant_lower;
|
||||
let lower_str = format!("{}", lower);
|
||||
|
||||
quote! {
|
||||
#lower_str => #lower.edit(cx)
|
||||
}
|
||||
});
|
||||
|
||||
let view_description = variants.clone().map(|v| {
|
||||
let lower = v.variant_lower;
|
||||
let lower_str = format!("{}", lower);
|
||||
let description = if let Some(d) = v.description {
|
||||
quote! {
|
||||
span(class="description") {
|
||||
" " #d
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
quote! {
|
||||
#lower_str => view! { cx, #description }
|
||||
}
|
||||
});
|
||||
|
||||
let signals = variants.clone().map(
|
||||
|EnumVariant {
|
||||
variant,
|
||||
variant_lower,
|
||||
inner,
|
||||
..
|
||||
}| {
|
||||
quote! {
|
||||
let #variant_lower = if let #name::#variant(v) = state.get_untracked().as_ref().clone() {
|
||||
create_signal(cx, v.clone())
|
||||
} else {
|
||||
create_signal(cx, <#inner>::default())
|
||||
};
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
let effect_match = variants.clone().map(|v| {
|
||||
let lower = v.variant_lower;
|
||||
let variant = v.variant;
|
||||
let lower_str = format!("{}", lower);
|
||||
|
||||
quote! {
|
||||
#lower_str => state.set(#name::#variant(#lower.get().as_ref().clone()))
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
let state = props.state;
|
||||
|
||||
let selected = create_signal(cx, String::from(#first_str));
|
||||
|
||||
#(#signals)*
|
||||
|
||||
create_effect(cx, || {
|
||||
match selected.get().as_str() {
|
||||
#(#effect_match,)*
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
|
||||
view! { cx,
|
||||
Block(title=#title.to_string()) {
|
||||
p(class="description") {
|
||||
#description
|
||||
}
|
||||
select(bind:value=selected) {
|
||||
#(#options)*
|
||||
}
|
||||
(match selected.get().as_str() {
|
||||
#(#view_description,)*
|
||||
_ => view! { cx, }
|
||||
})
|
||||
(match selected.get().as_str() {
|
||||
#(#view_match,)*
|
||||
_ => view! { cx, }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn struct_view(props: &ItemProps, s: DataStruct) -> TokenStream2 {
|
||||
let ItemProps {
|
||||
name,
|
||||
title,
|
||||
description,
|
||||
} = props;
|
||||
|
||||
let fields = s.fields.iter().map(|f| {
|
||||
let name = f
|
||||
.ident
|
||||
.as_ref()
|
||||
.expect("each struct field must be named")
|
||||
.clone();
|
||||
let name_str = name.to_string();
|
||||
let (title, description) = get_title_description(&f.attrs);
|
||||
|
||||
StructField {
|
||||
name_str,
|
||||
name,
|
||||
title,
|
||||
description,
|
||||
}
|
||||
});
|
||||
|
||||
let refs = fields.clone().map(|f| {
|
||||
let name = f.name;
|
||||
quote! {
|
||||
let #name = create_ref(cx, state.#name.clone());
|
||||
}
|
||||
});
|
||||
|
||||
let fields_view = fields.clone().map(|f| {
|
||||
let title = f.title.unwrap_or(f.name_str.to_case(Case::Title));
|
||||
let description = if let Some(d) = f.description {
|
||||
quote! {
|
||||
span(class="description") {
|
||||
" " #d
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
let name = f.name;
|
||||
quote! {
|
||||
p {
|
||||
label { (#title) br() #description }
|
||||
(#name.view(cx))
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
let state = props.state;
|
||||
|
||||
#(#refs)*
|
||||
|
||||
view! { cx,
|
||||
Block(title=#title.to_string()) {
|
||||
p(class="description") {
|
||||
#description
|
||||
}
|
||||
#(#fields_view)*
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn enum_view(props: &ItemProps, e: DataEnum) -> TokenStream2 {
|
||||
let ItemProps {
|
||||
name,
|
||||
title,
|
||||
description,
|
||||
} = props;
|
||||
|
||||
let variants = e.variants.iter().map(|v| {
|
||||
let variant = v.ident.clone();
|
||||
|
||||
let inner = match &v.fields {
|
||||
Fields::Unnamed(u) => u.unnamed.first().expect("the should be a field").ty.clone(),
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
let (title, description) = get_title_description(&v.attrs);
|
||||
let variant_lower = format_ident!("{}", variant.to_string().to_case(Case::Snake));
|
||||
EnumVariant {
|
||||
variant_lower,
|
||||
variant,
|
||||
inner,
|
||||
title,
|
||||
description,
|
||||
}
|
||||
});
|
||||
|
||||
let view_match = variants.clone().map(|v| {
|
||||
let variant = v.variant;
|
||||
|
||||
quote! {
|
||||
#name::#variant(x) => x.view(cx)
|
||||
}
|
||||
});
|
||||
|
||||
let view_description = variants.clone().map(|v| {
|
||||
let variant = v.variant;
|
||||
let description = if let Some(d) = v.description {
|
||||
quote! {
|
||||
span(class="description") {
|
||||
" " #d
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
quote! {
|
||||
#name::#variant(_) => view! { cx, #description }
|
||||
}
|
||||
});
|
||||
|
||||
quote! {
|
||||
let state = props.state;
|
||||
|
||||
view! { cx,
|
||||
Block(title=#title.to_string()) {
|
||||
p(class="description") {
|
||||
#description
|
||||
}
|
||||
(match state {
|
||||
#(#view_description,)*
|
||||
_ => view! { cx, }
|
||||
})
|
||||
(match state {
|
||||
#(#view_match,)*
|
||||
_ => view! { cx, }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro_derive(WebEdit)]
|
||||
pub fn web_edit(tokens: TokenStream) -> TokenStream {
|
||||
let input: syn::DeriveInput = syn::parse(tokens).unwrap();
|
||||
|
||||
let name = input.ident;
|
||||
let edit_ident = format_ident!("{}Edit", name);
|
||||
|
||||
let (t, d) = get_title_description(&input.attrs);
|
||||
|
||||
let title = t.unwrap_or(name.to_string());
|
||||
let description = d;
|
||||
|
||||
let props = ItemProps {
|
||||
name: name.clone(),
|
||||
title: title.clone(),
|
||||
description: description.clone(),
|
||||
};
|
||||
|
||||
let inner = match input.data {
|
||||
syn::Data::Struct(s) => struct_edit(&props, s),
|
||||
syn::Data::Enum(e) => enum_edit(&props, e),
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
let res = quote! {
|
||||
pub struct #edit_ident;
|
||||
|
||||
impl<'a, G: Html> Editor<'a, G, #name> for #edit_ident {
|
||||
fn edit(cx: Scope<'a>, props: EditProps<'a, #name>) -> View<G> {
|
||||
#inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, G: Html> Editable<'a, G> for #name {
|
||||
type Editor = #edit_ident;
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(res)
|
||||
}
|
||||
|
||||
#[proc_macro_derive(WebView)]
|
||||
pub fn web_view(tokens: TokenStream) -> TokenStream {
|
||||
let input: syn::DeriveInput = syn::parse(tokens).unwrap();
|
||||
|
||||
let name = input.ident;
|
||||
let view_ident = format_ident!("{}View", name);
|
||||
|
||||
let (t, d) = get_title_description(&input.attrs);
|
||||
|
||||
let title = t.unwrap_or(name.to_string());
|
||||
let description = d;
|
||||
|
||||
let props = ItemProps {
|
||||
name: name.clone(),
|
||||
title: title.clone(),
|
||||
description: description.clone(),
|
||||
};
|
||||
|
||||
let inner = match input.data {
|
||||
syn::Data::Struct(s) => struct_view(&props, s),
|
||||
syn::Data::Enum(e) => enum_view(&props, e),
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
let res = quote! {
|
||||
pub struct #view_ident;
|
||||
|
||||
impl<'a, G: Html> Viewer<'a, G, #name> for #view_ident {
|
||||
fn view(cx: Scope<'a>, props: ViewProps<'a, #name>) -> View<G> {
|
||||
#inner
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, G: Html> Viewable<'a, G> for #name {
|
||||
type Viewer = #view_ident;
|
||||
}
|
||||
};
|
||||
|
||||
TokenStream::from(res)
|
||||
}
|
|
@ -11,7 +11,7 @@ yew = "0.19"
|
|||
yew-router = "0.16"
|
||||
#yew-router = { git = "https://github.com/yewstack/yew" }
|
||||
web-sys = { version = "0.3", features = ["Request", "RequestInit", "RequestMode", "Response", "Headers", "HtmlSelectElement"] }
|
||||
lan_party_core = { path = "../core", features = ["serde"] }
|
||||
lan_party_core = { path = "../core", features = ["serde", "sycamore"] }
|
||||
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
|
||||
wasm-bindgen-futures = "0.4"
|
||||
serde = "1"
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
<meta charset="utf-8">
|
||||
<!--<link data-trunk href="tailwind.css" rel="css">-->
|
||||
<link rel="stylesheet" href="/simple.min-d15f5ff500b4c62a.css">
|
||||
<link rel="stylesheet" href="/style-13bbcf1f99e2f75c.css">
|
||||
<link rel="stylesheet" href="/style-5e1a36be9e479fbe.css">
|
||||
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@6.9.96/css/materialdesignicons.min.css" rel="stylesheet">
|
||||
<title>LAN Party</title>
|
||||
|
||||
<link rel="preload" href="/index-a2c5ffc914f34da_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
|
||||
<link rel="modulepreload" href="/index-a2c5ffc914f34da.js"></head>
|
||||
<link rel="preload" href="/index-61979c95126900f4_bg.wasm" as="fetch" type="application/wasm" crossorigin="">
|
||||
<link rel="modulepreload" href="/index-61979c95126900f4.js"></head>
|
||||
<body>
|
||||
<script type="module">import init from '/index-a2c5ffc914f34da.js';init('/index-a2c5ffc914f34da_bg.wasm');</script></body></html>
|
||||
<script type="module">import init from '/index-61979c95126900f4.js';init('/index-61979c95126900f4_bg.wasm');</script></body></html>
|
|
@ -1,12 +1,8 @@
|
|||
pub mod event;
|
||||
|
||||
use log::debug;
|
||||
use sycamore::prelude::*;
|
||||
use web_sys::Event;
|
||||
use yew::use_effect;
|
||||
|
||||
#[derive(Prop)]
|
||||
pub struct Props<F: FnMut(Event)> {
|
||||
pub struct ButtonProps<F: FnMut(Event)> {
|
||||
pub onclick: F,
|
||||
#[builder(default)]
|
||||
pub text: String,
|
||||
|
@ -15,7 +11,7 @@ pub struct Props<F: FnMut(Event)> {
|
|||
}
|
||||
|
||||
#[component]
|
||||
pub fn Button<'a, G: Html, F: 'a + FnMut(Event)>(cx: Scope<'a>, props: Props<F>) -> View<G> {
|
||||
pub fn Button<'a, G: Html, F: 'a + FnMut(Event)>(cx: Scope<'a>, props: ButtonProps<F>) -> View<G> {
|
||||
let mut icon_class = String::from("mdi ");
|
||||
|
||||
if !props.icon.is_empty() {
|
||||
|
@ -61,8 +57,8 @@ pub fn Table<'a, G: Html>(cx: Scope<'a>, props: TableProps<'a, G>) -> View<G> {
|
|||
|
||||
#[derive(Prop)]
|
||||
pub struct BlockProps<'a, G: Html> {
|
||||
title: String,
|
||||
children: Children<'a, G>,
|
||||
pub title: String,
|
||||
pub children: Children<'a, G>,
|
||||
}
|
||||
|
||||
#[component]
|
||||
|
|
|
@ -1,21 +1,70 @@
|
|||
use crate::components::event::{IntoEdit, Test};
|
||||
use lan_party_core::event::EventSpec;
|
||||
use lan_party_core::{
|
||||
edit::IntoEdit,
|
||||
event::{Event, EventSpec, EventUpdate},
|
||||
view::IntoView,
|
||||
};
|
||||
use log::debug;
|
||||
use sycamore::prelude::*;
|
||||
use reqwasm::http::Method;
|
||||
use sycamore::{futures::spawn_local_scoped, prelude::*};
|
||||
|
||||
use crate::components::{
|
||||
event::{Edit, EditProps},
|
||||
Block, Button,
|
||||
use crate::{
|
||||
components::{Block, Button},
|
||||
util::api_request,
|
||||
};
|
||||
|
||||
#[component]
|
||||
pub fn EventsPage<'a, G: Html>(cx: Scope<'a>) -> View<G> {
|
||||
let event_spec = create_signal(cx, EventSpec::default());
|
||||
let event_update = create_signal(cx, EventUpdate::default());
|
||||
|
||||
let events: &'a Signal<Vec<Event>> = create_signal(cx, Vec::<Event>::new());
|
||||
|
||||
spawn_local_scoped(cx, async move {
|
||||
events.set(
|
||||
api_request::<_, Vec<Event>>(Method::GET, "/event", Option::<()>::None)
|
||||
.await
|
||||
.map(|inner| inner.unwrap())
|
||||
.unwrap(),
|
||||
);
|
||||
});
|
||||
|
||||
let onadd = move |_| {
|
||||
spawn_local_scoped(cx, async move {
|
||||
let new_event = api_request::<EventSpec, Event>(
|
||||
Method::POST,
|
||||
"/event",
|
||||
Some((*event_spec).get().as_ref().clone()),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
debug!("{:#?}", new_event);
|
||||
events.modify().push(new_event.unwrap());
|
||||
});
|
||||
};
|
||||
|
||||
view! { cx,
|
||||
Block(title="Events".into()) {
|
||||
Keyed(
|
||||
iterable=&events,
|
||||
view=move |cx, event| {
|
||||
let event = create_ref(cx, event);
|
||||
view! { cx,
|
||||
(event.view(cx))
|
||||
br()
|
||||
}
|
||||
},
|
||||
key=|event| (event.name.clone()),
|
||||
)
|
||||
}
|
||||
br()
|
||||
Block(title="Create new event".into()) {
|
||||
(event_spec.edit(cx))
|
||||
Button(icon="mdi-check".into(), onclick=move |_| debug!("{:#?}", event_spec.get()))
|
||||
Button(icon="mdi-check".into(), onclick=onadd)
|
||||
}
|
||||
br()
|
||||
Block(title="Update an event".into()) {
|
||||
(event_update.edit(cx))
|
||||
Button(icon="mdi-check".into(), onclick=move |_| debug!("{:#?}", event_update.get()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,9 +93,7 @@ pub fn UsersPage<'a, G: Html>(cx: Scope<'a>) -> View<G> {
|
|||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut cloned = (*users).get().as_ref().clone();
|
||||
cloned.push(user.unwrap());
|
||||
users.set(cloned);
|
||||
users.modify().push(user.unwrap());
|
||||
});
|
||||
};
|
||||
|
||||
|
|
|
@ -58,3 +58,11 @@ textarea:focus, input:focus{
|
|||
outline: none;
|
||||
}
|
||||
|
||||
.description {
|
||||
font-size: 0.9rem;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
body {
|
||||
grid-template-columns: 1fr min(60rem, 90%) 1fr;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue