Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring cleaning came early #234

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"lightfx",
"webapi",
"webapi-client",
"webapi-model",
"webui",
"visualizer",
"events",
Expand Down
5 changes: 5 additions & 0 deletions animation-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ lightfx = { path = "../lightfx" }
midi-msg = "0.4.0"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
yew = { version = "0.19.0", optional = true }

[features]
default = []
yew = ["dep:yew"]
26 changes: 17 additions & 9 deletions animation-api/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt;
use std::{collections::HashMap, fmt};

use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::parameter_schema::{GetParametersSchema, ParametersSchema};
use crate::schema::{ConfigurationSchema, GetSchema, ParameterValue};

#[derive(Serialize, Deserialize, Debug)]
pub struct AnimationError {
Expand All @@ -18,13 +18,13 @@ impl fmt::Display for AnimationError {
impl std::error::Error for AnimationError {}

pub trait Animation {
type Parameters: GetParametersSchema + DeserializeOwned + Serialize + Default + Clone;
type Parameters: GetSchema + DeserializeOwned + Serialize + Default + Clone;

fn animation_name(&self) -> &str;

fn parameter_schema(&self) -> ParametersSchema
fn get_schema(&self) -> ConfigurationSchema
where
Self::Parameters: GetParametersSchema,
Self::Parameters: GetSchema,
{
Self::Parameters::schema()
}
Expand Down Expand Up @@ -52,13 +52,21 @@ pub trait Animation {
#[derive(Serialize, Deserialize)]
#[serde(tag = "method", content = "params")]
pub enum JsonRpcMethod {
Initialize { points: Vec<(f64, f64, f64)> },
Initialize {
points: Vec<(f64, f64, f64)>,
},
AnimationName,
ParameterSchema,
SetParameters { params: serde_json::Value },
SetParameters {
params: HashMap<String, ParameterValue>,
},
GetParameters,
GetFps,
Update { time_delta: f64 },
OnEvent { event: crate::event::Event },
Update {
time_delta: f64,
},
OnEvent {
event: crate::event::Event,
},
Render,
}
2 changes: 1 addition & 1 deletion animation-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod api;
pub mod event;
mod msg;
pub mod parameter_schema;
pub mod schema;

pub use api::Animation;
pub use api::AnimationError;
Expand Down
48 changes: 0 additions & 48 deletions animation-api/src/parameter_schema.rs

This file was deleted.

96 changes: 96 additions & 0 deletions animation-api/src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use std::collections::HashMap;

use lightfx::Color;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct EnumOption {
pub name: String,
pub description: Option<String>,
pub value: String,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ValueSchema {
Number { min: f64, max: f64, step: f64 },
Color,
Enum { values: Vec<EnumOption> },
Speed,
Percentage,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct ParameterSchema {
pub id: String,
pub name: String,
pub description: Option<String>,
#[serde(flatten)]
pub value: ValueSchema,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug, Default)]
pub struct ConfigurationSchema {
pub parameters: Vec<ParameterSchema>,
}

pub trait GetSchema {
fn schema() -> ConfigurationSchema;
}

impl GetSchema for () {
fn schema() -> ConfigurationSchema {
ConfigurationSchema {
parameters: Vec::new(),
}
}
}

pub trait GetEnumOptions {
fn enum_options() -> Vec<EnumOption>;
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(untagged)]
pub enum ParameterValue {
Number(f64),
Color(Color),
EnumOption(String),
}

#[cfg(feature = "yew")]
impl yew::html::ImplicitClone for ParameterValue {}

impl ParameterValue {
pub fn number(&self) -> Option<f64> {
if let Self::Number(n) = self {
Some(*n)
} else {
None
}
}

pub fn color(&self) -> Option<&Color> {
if let Self::Color(c) = self {
Some(c)
} else {
None
}
}

pub fn enum_option(&self) -> Option<&str> {
if let Self::EnumOption(s) = self {
Some(s)
} else {
None
}
}
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct Configuration {
pub id: String,
pub name: String,
pub schema: ConfigurationSchema,
pub values: HashMap<String, ParameterValue>,
}
34 changes: 17 additions & 17 deletions animation-plugin-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ pub fn plugin(_attr: TokenStream, item: TokenStream) -> TokenStream {
},
JsonRpcMethod::ParameterSchema => {
if let Some(animation) = animation.as_ref() {
respond(message.id, animation.parameter_schema());
respond(message.id, animation.get_schema());
}
},
JsonRpcMethod::SetParameters { params } => {
if let Some(mut animation) = animation.as_mut() {
match serde_json::from_value(params) {
match serde_json::from_value(serde_json::json!(params)) {
Ok(params) => {
animation.set_parameters(params);
respond(message.id, ());
Expand Down Expand Up @@ -158,8 +158,8 @@ struct FieldAttributes {
enum_options: bool,
}

#[proc_macro_derive(ParameterSchema, attributes(schema_field))]
pub fn derive_parameter_schema(input: TokenStream) -> TokenStream {
#[proc_macro_derive(Schema, attributes(schema_field))]
pub fn derive_schema(input: TokenStream) -> TokenStream {
let ast: syn::ItemStruct = syn::parse2(input.into()).unwrap();
let fields: proc_macro2::TokenStream = ast
.fields
Expand All @@ -171,29 +171,29 @@ pub fn derive_parameter_schema(input: TokenStream) -> TokenStream {
let max = number.max;
let step = number.step;
quote! {
animation_api::parameter_schema::ParameterValue::Number {
animation_api::schema::ValueSchema::Number {
min: #min,
max: #max,
step: #step,
}
}
} else if attrs.color {
quote! {
animation_api::parameter_schema::ParameterValue::Color
animation_api::schema::ValueSchema::Color
}
} else if attrs.percentage {
quote! {
animation_api::parameter_schema::ParameterValue::Percentage
animation_api::schema::ValueSchema::Percentage
}
} else if attrs.speed {
quote! {
animation_api::parameter_schema::ParameterValue::Speed
animation_api::schema::ValueSchema::Speed
}
} else if attrs.enum_options {
let ty = field.ty;
quote! {
animation_api::parameter_schema::ParameterValue::Enum {
values: <#ty as animation_api::parameter_schema::GetEnumOptions>::enum_options(),
animation_api::schema::ValueSchema::Enum {
values: <#ty as animation_api::schema::GetEnumOptions>::enum_options(),
}
}
} else {
Expand All @@ -208,7 +208,7 @@ pub fn derive_parameter_schema(input: TokenStream) -> TokenStream {
quote! { None }
};
quote! {
animation_api::parameter_schema::Parameter {
animation_api::schema::ParameterSchema {
id: stringify!(#ident).to_owned(),
name: #name.to_owned(),
description: #description,
Expand All @@ -220,9 +220,9 @@ pub fn derive_parameter_schema(input: TokenStream) -> TokenStream {

let ident = ast.ident;
quote! {
impl animation_api::parameter_schema::GetParametersSchema for #ident {
fn schema() -> animation_api::parameter_schema::ParametersSchema {
animation_api::parameter_schema::ParametersSchema {
impl animation_api::schema::GetSchema for #ident {
fn schema() -> animation_api::schema::ConfigurationSchema {
animation_api::schema::ConfigurationSchema {
parameters: vec![
#fields
],
Expand Down Expand Up @@ -251,7 +251,7 @@ pub fn derive_enum_schema(input: TokenStream) -> TokenStream {
let name = variant.name;
let ident = variant.ident;
quote! {
animation_api::parameter_schema::EnumOption {
animation_api::schema::EnumOption {
name: #name.into(),
description: None,
value: stringify!(#ident).into(),
Expand All @@ -261,8 +261,8 @@ pub fn derive_enum_schema(input: TokenStream) -> TokenStream {
.collect();
let ident = ast.ident;
quote! {
impl animation_api::parameter_schema::GetEnumOptions for #ident {
fn enum_options() -> Vec<animation_api::parameter_schema::EnumOption> {
impl animation_api::schema::GetEnumOptions for #ident {
fn enum_options() -> Vec<animation_api::schema::EnumOption> {
vec![#variants]
}
}
Expand Down
Loading
Loading