use rosc::OscMessage;
use uuid::Uuid;
use super::args::{parse_index, parse_string};
use super::unknown_address;
use crate::action::UiAction;
use crate::storage::asset::animation::Animation;
use crate::storage::asset::animation::argument::ArgumentKindId;
use crate::storage::asset_id::AssetId;
pub(super) fn parse_animation_message(msg: &OscMessage, path: &[&str]) -> Result<UiAction, ()> {
match path {
["asset", "animation", animation_id, "shader_code"] => {
Ok(UiAction::SetAnimationShaderCode(
parse_animation_id_from_path(animation_id)?,
parse_string(msg, 0)?,
))
}
["asset", "animation", animation_id, "argument", "add"] => Ok(
UiAction::AddAnimationArgument(parse_animation_id_from_path(animation_id)?),
),
[
"asset",
"animation",
animation_id,
"argument",
argument_index,
"remove",
] => Ok(UiAction::RemoveAnimationArgument(
parse_animation_id_from_path(animation_id)?,
parse_index(argument_index)?,
)),
[
"asset",
"animation",
animation_id,
"argument",
argument_index,
"name",
] => Ok(UiAction::SetAnimationArgumentName(
parse_animation_id_from_path(animation_id)?,
parse_index(argument_index)?,
parse_string(msg, 0)?,
)),
[
"asset",
"animation",
animation_id,
"argument",
argument_index,
"kind",
] => Ok(UiAction::SetAnimationArgumentKind(
parse_animation_id_from_path(animation_id)?,
parse_index(argument_index)?,
parse_argument_kind(msg, 0)?,
)),
_ => unknown_address(msg),
}
}
fn parse_animation_id_from_path(animation_id: &str) -> Result<AssetId<Animation>, ()> {
let uuid = Uuid::parse_str(animation_id).map_err(|_| ())?;
Ok(AssetId::from_uuid(uuid))
}
fn parse_argument_kind(msg: &OscMessage, index: usize) -> Result<ArgumentKindId, ()> {
parse_string(msg, index)?.parse().map_err(|_| ())
}