Skip to content
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

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

68 changes: 68 additions & 0 deletions Cargo.lock

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

11 changes: 10 additions & 1 deletion animation-template-native/manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"id": "io.rustmas.template.native",
"display_name": "Animation Template (Native)",
"plugin_type": "native"
"plugin_type": "native",
"author": "Mariusz Różycki <mar.rozycki@gmail.com>",
"api_version": "0.9",
"version": "1.0",
"tags": [
"2d",
"3d",
"audio"
]
}
11 changes: 10 additions & 1 deletion animation-template-wasm/manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"id": "io.rustmas.template.wasm",
"display_name": "Animation Template (Wasm)",
"plugin_type": "wasm"
"plugin_type": "wasm",
"author": "Mariusz Różycki <mar.rozycki@gmail.com>",
"api_version": "0.9",
"version": "1.0",
"tags": [
"2d",
"3d",
"audio"
]
}
11 changes: 11 additions & 0 deletions animation-wrapper/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ pub enum PluginType {
Wasm,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum PluginApiVersion {
#[serde(rename = "0.9")]
V0_9,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PluginManifest {
pub id: String,
pub display_name: String,
pub author: String,
#[serde(default)]
pub plugin_type: PluginType,
pub api_version: PluginApiVersion,
pub version: String,
pub tags: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
26 changes: 3 additions & 23 deletions animation-wrapper/src/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use tar::Archive;

use crate::config::{PluginConfig, PluginManifest};
use crate::config::PluginManifest;

#[derive(Debug, thiserror::Error)]
pub enum PluginUnwrapError {
Expand Down Expand Up @@ -43,28 +43,8 @@ fn manifest_from_crab(path: &Path) -> Result<PluginManifest, PluginUnwrapError>
Ok(serde_json::from_reader(entry_reader)?)
}

fn animation_id_from_crab(path: &Path) -> Result<String, PluginUnwrapError> {
Ok(path
.file_name()
.ok_or(PluginUnwrapError::InvalidFilename)?
.to_string_lossy()
.trim_end_matches(".crab")
.to_string())
}

pub fn unwrap_plugin<P: AsRef<Path>>(path: &P) -> Result<PluginConfig, PluginUnwrapError> {
fn inner(path: &Path) -> Result<PluginConfig, PluginUnwrapError> {
let animation_id = animation_id_from_crab(path)?;
let manifest = manifest_from_crab(path)?;
let path = path.to_owned();

Ok(PluginConfig {
animation_id,
manifest,
path,
})
}
inner(path.as_ref())
pub fn unwrap_plugin<P: AsRef<Path>>(path: P) -> Result<PluginManifest, PluginUnwrapError> {
manifest_from_crab(path.as_ref())
}

pub fn reader_from_crab<P: AsRef<Path>>(path: &P) -> Result<impl Read, PluginUnwrapError> {
Expand Down
36 changes: 32 additions & 4 deletions animator/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,17 @@ impl AnimationFactory {
})?
.filter_map(|d| d.ok())
.filter(|d| d.file_name().to_str().is_some_and(|d| d.ends_with(".crab")))
.filter_map(|d| unwrap::unwrap_plugin(&d.path()).ok())
.map(|p| (p.animation_id.clone(), p));
.filter_map(|d| Some(d.path().to_owned()).zip(unwrap::unwrap_plugin(d.path()).ok()))
.map(|(path, manifest)| {
(
manifest.id.clone(),
PluginConfig {
animation_id: manifest.id.clone(),
manifest,
path,
},
)
});

valid_plugins.extend(crab_plugins);

Expand All @@ -125,9 +134,14 @@ impl AnimationFactory {
.and_then(|f| f.to_str())
.is_some_and(|f| f.ends_with(".crab"))
{
unwrap::unwrap_plugin(&path).map_err(|e| {
let manifest = unwrap::unwrap_plugin(path).map_err(|e| {
AnimationFactoryError::InvalidPlugin(PluginConfigError::InvalidCrab(e))
})?
})?;
PluginConfig {
animation_id: manifest.id.clone(),
manifest,
path: path.to_owned(),
}
} else {
PluginConfig::new(path).map_err(AnimationFactoryError::InvalidPlugin)?
};
Expand All @@ -142,6 +156,20 @@ impl AnimationFactory {
}
}

pub async fn install(&self, path: &Path) -> Result<PluginConfig, AnimationFactoryError> {
let manifest = unwrap::unwrap_plugin(path)
.map_err(|e| AnimationFactoryError::InvalidPlugin(PluginConfigError::InvalidCrab(e)))?;

let new_path = self.plugin_dir.join(format!("{}.crab", manifest.id));
tokio::fs::rename(path, &new_path).await?;

Ok(PluginConfig {
animation_id: manifest.id.clone(),
manifest,
path: new_path,
})
}

pub fn points(&self) -> &[(f64, f64, f64)] {
&self.points
}
Expand Down
4 changes: 4 additions & 0 deletions webapi-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ serde = "1.0.193"
thiserror = "1.0.60"
url = "2.5.0"

web-sys = { version = "0.3.60", features = ["FormData"], optional = true }
gloo-net = { version = "0.6.0", optional = true }

[features]
default = []
visualizer = []
js = ["dep:web-sys", "dep:gloo-net"]
34 changes: 33 additions & 1 deletion webapi-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::collections::HashMap;

use gloo_net::http::Request;
use serde::{Serialize, de::DeserializeOwned};
use url::Url;

use web_sys::FormData;
pub use webapi_model::{
Animation, Configuration, GetEventGeneratorSchemaResponse, GetParametersResponse,
GetPointsResponse, ListAnimationsResponse, ParameterValue, SwitchAnimationRequest,
};
use webapi_model::{
ApiResponse, Event, SendEventRequest, SetAnimationParametersRequest,
ApiResponse, Event, RemoveAnimationRequest, SendEventRequest, SetAnimationParametersRequest,
SetEventGeneratorParametersRequest, SwitchAnimationResponse,
};

Expand Down Expand Up @@ -137,6 +139,36 @@ impl RustmasApiClient {
.animation)
}

pub async fn remove_animation(&self, animation_id: String) -> Result<Vec<Animation>> {
Ok(self
.post::<ListAnimationsResponse>(
"animations/remove/",
&RemoveAnimationRequest { animation_id },
)
.await?
.animations)
}

#[cfg(feature = "js")]
pub async fn install_animation(&self, form_data: FormData) -> Result<Vec<Animation>> {
Ok(Request::post(&self.url("animations/install/"))
.body(form_data)
.map_err(|e| GatewayError::InvalidRequest {
reason: e.to_string(),
})?
.send()
.await
.map_err(|e| GatewayError::ApiError {
reason: e.to_string(),
})?
.json::<ListAnimationsResponse>()
.await
.map_err(|e| GatewayError::InvalidResponse {
reason: e.to_string(),
})?
.animations)
}

pub async fn turn_off(&self) -> Result<()> {
self.post("animations/turn_off/", &()).await
}
Expand Down
5 changes: 5 additions & 0 deletions webapi-model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ pub struct SwitchAnimationRequest {
pub params: Option<HashMap<String, ParameterValue>>,
}

#[derive(Serialize, Deserialize)]
pub struct RemoveAnimationRequest {
pub animation_id: String,
}

#[derive(Serialize, Deserialize)]
pub struct SwitchAnimationResponse {
pub animation: Configuration,
Expand Down
1 change: 1 addition & 0 deletions webapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ actix-web-actors = "4.2.0"
actix = "0.13.0"
actix-cors = "0.6.4"
actix-web = "4"
actix-multipart = "0.7.2"
async-stream = "0.3.5"
config = "0.14.1"
futures-core = "0.3.28"
Expand Down
Loading