Create initial JSON API format

This commit is contained in:
NGnius 2022-08-07 15:57:41 -04:00
parent 14ff848b0a
commit 4a272e53ee
12 changed files with 187 additions and 15 deletions

42
server/Cargo.lock generated
View file

@ -8,15 +8,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backend"
version = "0.1.0"
dependencies = [
"log",
"simplelog",
"usdpl-back",
]
[[package]]
name = "base64"
version = "0.13.0"
@ -354,6 +345,17 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
[[package]]
name = "kaylon"
version = "0.1.0"
dependencies = [
"log",
"serde",
"serde_json",
"simplelog",
"usdpl-back",
]
[[package]]
name = "libc"
version = "0.2.126"
@ -597,6 +599,20 @@ name = "serde"
version = "1.0.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc855a42c7967b7c369eb5860f7164ef1f6f81c20c7cc1141f2a604e18723b03"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f2122636b9fe3b81f1cb25099fcf2d3f542cdb1d45940d56c713158884a05da"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
@ -943,9 +959,9 @@ dependencies = [
[[package]]
name = "usdpl-back"
version = "0.5.3"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d237439986405621b9b6da350aefcfca9e2b808c10695f55f8b80ccc324b2c0"
checksum = "cbbc0781e83ba990f8239142e33173a2d2548701775f3db66702d1af4fd0319a"
dependencies = [
"bytes",
"tokio",
@ -955,9 +971,9 @@ dependencies = [
[[package]]
name = "usdpl-core"
version = "0.5.0"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dd726b9f0121d4449082e3ce73586dea0a0448494031833b7b173e4476f0ea5"
checksum = "862153581fac266458521f49e5906a71c1eee1665cb4c7d71e9586bd34b45394"
dependencies = [
"base64",
]

View file

@ -1,12 +1,15 @@
[package]
name = "backend" # TODO replace with plugin name (also in build.sh)
name = "kaylon" # TODO replace with plugin name (also in build.sh)
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
usdpl-back = { version = "0.5.3", features = ["decky"] }
usdpl-back = { version = "0.6.0", features = ["decky"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
# logging
log = "0.4"

View file

@ -0,0 +1,11 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
pub struct AboutConfig {
pub name: String,
pub version: String,
pub description: String,
pub url: Option<String>,
pub author: Option<String>,
pub license: Option<String>,
}

View file

@ -0,0 +1,13 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
#[serde(tag = "action")]
pub enum ActionConfig {
#[serde(rename = "command")]
Command(CommandAction),
}
#[derive(Serialize, Deserialize)]
pub struct CommandAction {
pub run: String,
}

13
server/src/config/base.rs Normal file
View file

@ -0,0 +1,13 @@
use serde::{Serialize, Deserialize};
use super::{ElementConfig, AboutConfig};
#[derive(Serialize, Deserialize)]
#[serde(tag = "api-version")]
pub enum BaseConfig {
#[serde(rename = "v0.0.0")]
V0 {
items: Vec<ElementConfig>,
about: AboutConfig,
},
}

View file

@ -0,0 +1,9 @@
use serde::{Serialize, Deserialize};
use super::ActionConfig;
#[derive(Serialize, Deserialize)]
pub struct ButtonConfig {
pub title: String,
pub on_click: ActionConfig,
}

View file

@ -0,0 +1,16 @@
use serde::{Serialize, Deserialize};
use super::{ButtonConfig, ToggleConfig, SliderConfig, ReadingConfig};
#[derive(Serialize, Deserialize)]
#[serde(tag = "element")]
pub enum ElementConfig {
#[serde(rename = "button")]
Button(ButtonConfig),
#[serde(rename = "toggle")]
Toggle(ToggleConfig),
#[serde(rename = "slider")]
Slider(SliderConfig),
#[serde(rename = "reading")]
Reading(ReadingConfig),
}

59
server/src/config/mod.rs Normal file
View file

@ -0,0 +1,59 @@
mod about;
mod action;
mod base;
mod button;
mod element;
mod reading;
mod slider;
mod toggle;
pub use about::AboutConfig;
pub use action::{ActionConfig, CommandAction};
pub use base::BaseConfig;
pub use button::ButtonConfig;
pub use element::ElementConfig;
pub use reading::ReadingConfig;
pub use slider::SliderConfig;
pub use toggle::ToggleConfig;
#[cfg(test)]
mod test {
use super::*;
#[test]
fn dump_test() {
let conf = BaseConfig::V0 {
items: vec![
ElementConfig::Button(ButtonConfig {
title: "Test Button".into(),
on_click: ActionConfig::Command(CommandAction{run: "echo 'hello button'".into()}),
}),
ElementConfig::Toggle(ToggleConfig {
title: "Test Toggle".into(),
description: Some("Toggle description".into()),
on_enable: ActionConfig::Command(CommandAction{run: "echo 'hello toggle 1'".into()}),
on_disable: ActionConfig::Command(CommandAction{run: "echo 'hello toggle 0'".into()}),
}),
ElementConfig::Slider(SliderConfig {
title: "Test Slider".into(),
on_set: ActionConfig::Command(CommandAction{run: "echo 'hello slider'".into()}),
}),
ElementConfig::Reading(ReadingConfig {
title: "Test Reading".into(),
period_ms: 10000,
on_period: ActionConfig::Command(CommandAction{run: "echo 'hello reading'".into()})
}),
],
about: AboutConfig {
name: "Test name".into(),
version: "v0.42.0".into(),
description: "Test description".into(),
url: Some("https://github.com/NGnius/kaylon".into()),
author: Some("NGnius <ngniusness@gmail.com>".into()),
license: Some("MIT".into()),
},
};
let output = serde_json::to_string_pretty(&conf).unwrap();
println!("JSON: {}", output);
}
}

View file

@ -0,0 +1,10 @@
use serde::{Serialize, Deserialize};
use super::ActionConfig;
#[derive(Serialize, Deserialize)]
pub struct ReadingConfig {
pub title: String,
pub period_ms: usize,
pub on_period: ActionConfig,
}

View file

@ -0,0 +1,9 @@
use serde::{Serialize, Deserialize};
use super::ActionConfig;
#[derive(Serialize, Deserialize)]
pub struct SliderConfig {
pub title: String,
pub on_set: ActionConfig,
}

View file

@ -0,0 +1,11 @@
use serde::{Serialize, Deserialize};
use super::ActionConfig;
#[derive(Serialize, Deserialize)]
pub struct ToggleConfig {
pub title: String,
pub description: Option<String>,
pub on_enable: ActionConfig,
pub on_disable: ActionConfig,
}

View file

@ -1,3 +1,5 @@
mod config;
use simplelog::{WriteLogger, LevelFilter};
use usdpl_back::Instance;