Skip to content

Commit

Permalink
animation-api: Unify naming around schemas
Browse files Browse the repository at this point in the history
Now the overall schema is just Schema, made up of individual
ParameterSchemas for each parameter, which include ValueSchema
for their value.
  • Loading branch information
mrozycki committed Dec 30, 2023
1 parent 33298ea commit d2e504f
Show file tree
Hide file tree
Showing 56 changed files with 198 additions and 202 deletions.
8 changes: 4 additions & 4 deletions animation-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

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

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

#[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
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct EnumOption {

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ParameterValue {
pub enum ValueSchema {
Number { min: f64, max: f64, step: f64 },
Color,
Enum { values: Vec<EnumOption> },
Expand All @@ -18,26 +18,26 @@ pub enum ParameterValue {
}

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

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

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

impl GetParametersSchema for () {
fn schema() -> ParametersSchema {
ParametersSchema {
impl GetSchema for () {
fn schema() -> ConfigurationSchema {
ConfigurationSchema {
parameters: Vec::new(),
}
}
Expand Down
32 changes: 16 additions & 16 deletions animation-plugin-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ 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 } => {
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
22 changes: 10 additions & 12 deletions animation-utils/src/decorators/brightness_controlled.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
use animation_api::parameter_schema::{
GetParametersSchema, Parameter, ParameterValue, ParametersSchema,
};
use animation_api::schema::{ConfigurationSchema, GetSchema, ParameterSchema, ValueSchema};
use animation_api::Animation;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Parameters<P: GetParametersSchema> {
pub struct Parameters<P: GetSchema> {
brightness_factor: f64,
#[serde(flatten)]
inner: P,
}

impl<P: GetParametersSchema> GetParametersSchema for Parameters<P> {
fn schema() -> ParametersSchema {
let mut parameters = vec![Parameter {
impl<P: GetSchema> GetSchema for Parameters<P> {
fn schema() -> ConfigurationSchema {
let mut parameters = vec![ParameterSchema {
id: "brightness_factor".to_owned(),
name: "Brightness".to_owned(),
description: None,
value: ParameterValue::Percentage,
value: ValueSchema::Percentage,
}];
parameters.extend(P::schema().parameters);
ParametersSchema { parameters }
ConfigurationSchema { parameters }
}
}

pub struct BrightnessControlled<P: GetParametersSchema, A: Animation<Parameters = P>> {
pub struct BrightnessControlled<P: GetSchema, A: Animation<Parameters = P>> {
animation: A,
parameters: Parameters<P>,
}

impl<A, P> Animation for BrightnessControlled<P, A>
where
A: Animation<Parameters = P>,
P: GetParametersSchema + Default + Clone + Serialize + DeserializeOwned,
P: GetSchema + Default + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<P>;

Expand Down Expand Up @@ -75,7 +73,7 @@ where
}
}

impl<P: GetParametersSchema + Default, A: Animation<Parameters = P>> BrightnessControlled<P, A> {
impl<P: GetSchema + Default, A: Animation<Parameters = P>> BrightnessControlled<P, A> {
pub fn new(animation: A) -> Self {
Self {
animation,
Expand Down
28 changes: 14 additions & 14 deletions animation-utils/src/decorators/off_switch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use animation_api::parameter_schema::{
GetEnumOptions, GetParametersSchema, Parameter, ParameterValue, ParametersSchema,
use animation_api::schema::{
ConfigurationSchema, GetEnumOptions, GetSchema, ParameterSchema, ValueSchema,
};
use animation_api::Animation;
use animation_plugin_macro::EnumSchema;
Expand All @@ -17,42 +17,42 @@ pub enum Switch {
}

#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Parameters<P: GetParametersSchema> {
pub struct Parameters<P: GetSchema> {
off_switch_delay: f64,
off_switch_state: Switch,

#[serde(flatten)]
inner: P,
}

impl<P: GetParametersSchema> GetParametersSchema for Parameters<P> {
fn schema() -> ParametersSchema {
impl<P: GetSchema> GetSchema for Parameters<P> {
fn schema() -> ConfigurationSchema {
let mut parameters = vec![
Parameter {
ParameterSchema {
id: "off_switch_state".to_owned(),
name: "State".to_owned(),
description: None,
value: ParameterValue::Enum {
value: ValueSchema::Enum {
values: Switch::enum_options(),
},
},
Parameter {
ParameterSchema {
id: "off_switch_delay".to_owned(),
name: "Switch delay".to_owned(),
description: None,
value: ParameterValue::Number {
value: ValueSchema::Number {
min: 0.0,
max: 5.0,
step: 0.1,
},
},
];
parameters.extend(P::schema().parameters);
ParametersSchema { parameters }
ConfigurationSchema { parameters }
}
}

pub struct OffSwitch<P: GetParametersSchema, A: Animation<Parameters = P>> {
pub struct OffSwitch<P: GetSchema, A: Animation<Parameters = P>> {
animation: A,
parameters: Parameters<P>,
energy: f64,
Expand All @@ -61,7 +61,7 @@ pub struct OffSwitch<P: GetParametersSchema, A: Animation<Parameters = P>> {
impl<A, P> Animation for OffSwitch<P, A>
where
A: Animation<Parameters = P>,
P: GetParametersSchema + Default + Clone + Serialize + DeserializeOwned,
P: GetSchema + Default + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<P>;

Expand Down Expand Up @@ -96,7 +96,7 @@ where
self.animation.animation_name()
}

fn parameter_schema(&self) -> ParametersSchema {
fn get_schema(&self) -> ConfigurationSchema {
Self::Parameters::schema()
}

Expand All @@ -118,7 +118,7 @@ where
}
}

impl<P: GetParametersSchema + Default, A: Animation<Parameters = P>> OffSwitch<P, A> {
impl<P: GetSchema + Default, A: Animation<Parameters = P>> OffSwitch<P, A> {
pub fn new(animation: A) -> Self {
Self {
animation,
Expand Down
24 changes: 11 additions & 13 deletions animation-utils/src/decorators/speed_controlled.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,38 @@
use animation_api::parameter_schema::{
GetParametersSchema, Parameter, ParameterValue, ParametersSchema,
};
use animation_api::schema::{ConfigurationSchema, GetSchema, ParameterSchema, ValueSchema};
use animation_api::Animation;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Parameters<P: GetParametersSchema> {
pub struct Parameters<P: GetSchema> {
speed_factor: f64,

#[serde(flatten)]
inner: P,
}

impl<P: GetParametersSchema> GetParametersSchema for Parameters<P> {
fn schema() -> ParametersSchema {
let mut parameters = vec![Parameter {
impl<P: GetSchema> GetSchema for Parameters<P> {
fn schema() -> ConfigurationSchema {
let mut parameters = vec![ParameterSchema {
id: "speed_factor".to_owned(),
name: "Speed Factor".to_owned(),
description: None,
value: ParameterValue::Speed,
value: ValueSchema::Speed,
}];
parameters.extend(P::schema().parameters);
ParametersSchema { parameters }
ConfigurationSchema { parameters }
}
}

pub struct SpeedControlled<P: GetParametersSchema, A: Animation<Parameters = P>> {
pub struct SpeedControlled<P: GetSchema, A: Animation<Parameters = P>> {
animation: A,
parameters: Parameters<P>,
}

impl<A, P> Animation for SpeedControlled<P, A>
where
A: Animation<Parameters = P>,
P: GetParametersSchema + Default + Clone + Serialize + DeserializeOwned,
P: GetSchema + Default + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<P>;

Expand All @@ -54,7 +52,7 @@ where
self.animation.animation_name()
}

fn parameter_schema(&self) -> ParametersSchema {
fn get_schema(&self) -> ConfigurationSchema {
Self::Parameters::schema()
}

Expand All @@ -76,7 +74,7 @@ where
}
}

impl<P: GetParametersSchema + Default, A: Animation<Parameters = P>> SpeedControlled<P, A> {
impl<P: GetSchema + Default, A: Animation<Parameters = P>> SpeedControlled<P, A> {
pub fn new(animation: A) -> Self {
Self {
animation,
Expand Down
2 changes: 1 addition & 1 deletion animation-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod decorators;
use std::f64::consts::TAU;

pub use animation_plugin_macro::plugin;
pub use animation_plugin_macro::{EnumSchema, ParameterSchema};
pub use animation_plugin_macro::{EnumSchema, Schema};
use nalgebra::{Rotation3, Unit, Vector3};
use rand::Rng;

Expand Down
Loading

0 comments on commit d2e504f

Please sign in to comment.