use alpha2_model::{
ClipId, CompositionId, DataAnchor, DataKind, DataSetId, ModelError, NodeId, TrackId,
};
use alpha2_time::{MediaTime, TimeRange};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
pub enum EditRequest {
RemoveRanges {
ranges: Ranges,
target: Target,
#[serde(default)]
ripple: Ripple,
#[serde(default)]
invert: bool,
#[serde(default)]
padding: MediaTime,
#[serde(default)]
min_duration: MediaTime,
},
SplitAt {
times: Times,
target: Target,
},
Bake {
targets: Vec<BakeTarget>,
},
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Ranges {
Data(DataRef),
List(RangeList),
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DataRef {
pub data: DataSetId,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RangeList {
pub list: Vec<RawRange>,
#[serde(default)]
pub anchor: Option<DataAnchor>,
}
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RawRange {
pub start: MediaTime,
pub end: MediaTime,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Times {
Data(DataRef),
List(TimeList),
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TimeList {
pub list: Vec<MediaTime>,
#[serde(default)]
pub anchor: Option<DataAnchor>,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum Target {
Track(TrackId),
Clips(Vec<ClipId>),
}
#[derive(Copy, Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Ripple {
None,
Track,
#[default]
Timeline,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(untagged)]
pub enum BakeTarget {
Clip {
clip: ClipId,
property: String,
},
Node {
composition: CompositionId,
node: NodeId,
property: String,
},
Track {
track: TrackId,
property: String,
},
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Moved {
pub from: TimeRange,
pub to: TimeRange,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Created {
pub id: ClipId,
pub from: ClipId,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SkipReason {
InTransition,
InFade,
MatteSource,
ReadThrough,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Skipped {
pub clip: ClipId,
pub time: MediaTime,
pub reason: SkipReason,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct EditReport {
pub removed: Vec<TimeRange>,
pub moved: Vec<Moved>,
pub created: Vec<Created>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub skipped: Vec<Skipped>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum EditError {
UnknownData(DataSetId),
WrongKind {
data: DataSetId,
kind: DataKind,
},
UnknownTarget(String),
EmptyTarget,
MissingAnchor,
InvalidRange(usize),
NothingToBake(String),
CorrectionNotBakeable(String),
UnboundedParameter(String),
MatteSource(ClipId),
ReadThrough {
clip: ClipId,
by: String,
},
InTransition {
clip: ClipId,
time: MediaTime,
},
InFade {
clip: ClipId,
time: MediaTime,
},
TooManyClips {
track: TrackId,
found: usize,
maximum: usize,
},
TooManyCues {
track: TrackId,
found: Option<usize>,
maximum: usize,
},
InvalidResult(ModelError),
}
impl fmt::Display for EditError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownData(data) => write!(formatter, "unknown data set {data}"),
Self::WrongKind { data, kind } => write!(
formatter,
"data set {data} is {kind} data, which the operation does not take"
),
Self::UnknownTarget(what) => write!(formatter, "unknown {what}"),
Self::EmptyTarget => formatter.write_str("the target names no clip"),
Self::MissingAnchor => formatter.write_str("a list needs an anchor"),
Self::InvalidRange(index) => write!(formatter, "range {index} starts after it ends"),
Self::NothingToBake(at) => write!(formatter, "{at} follows no data"),
Self::CorrectionNotBakeable(at) => {
write!(formatter, "{at} is a correction, which has no keyframes")
}
Self::UnboundedParameter(at) => write!(
formatter,
"{at} is a parameter of an effect whose range only the renderer knows"
),
Self::MatteSource(clip) => write!(
formatter,
"clip {clip} is another clip's matte, and the edit cuts the two apart"
),
Self::ReadThrough { clip, by } => write!(
formatter,
"{by} reads data through clip {clip}, and the edit cuts the two apart"
),
Self::InTransition { clip, time } => write!(
formatter,
"the edit cuts clip {clip} at {} inside one of its transitions",
time.ticks()
),
Self::InFade { clip, time } => write!(
formatter,
"the edit cuts clip {clip} at {} inside one of its fades",
time.ticks()
),
Self::TooManyClips {
track,
found,
maximum,
} => write!(
formatter,
"the edit leaves {found} clips on track {track}; a track holds {maximum}"
),
Self::TooManyCues {
track,
found: Some(found),
maximum,
} => write!(
formatter,
"the bake writes {found} cues on track {track}; a track holds {maximum}"
),
Self::TooManyCues {
track, found: None, ..
} => write!(
formatter,
"the words of track {track} derive more than {} cues or map more than {} words",
alpha2_model::MAX_DERIVED_CUES,
alpha2_model::MAX_DATA_WORDS
),
Self::InvalidResult(error) => {
write!(formatter, "the edited project is invalid: {error}")
}
}
}
}
impl std::error::Error for EditError {}
impl EditError {
#[must_use]
pub const fn kind(&self) -> &'static str {
match self {
Self::UnknownData(_) => "unknown_data",
Self::WrongKind { .. } => "wrong_kind",
Self::UnknownTarget(_) => "unknown_target",
Self::EmptyTarget => "empty_target",
Self::MissingAnchor => "missing_anchor",
Self::InvalidRange(_) => "invalid_range",
Self::NothingToBake(_) => "nothing_to_bake",
Self::CorrectionNotBakeable(_) => "correction_not_bakeable",
Self::UnboundedParameter(_) => "unbounded_parameter",
Self::MatteSource(_) => "matte_source",
Self::ReadThrough { .. } => "read_through",
Self::InTransition { .. } => "in_transition",
Self::InFade { .. } => "in_fade",
Self::TooManyClips { .. } => "too_many_clips",
Self::TooManyCues { .. } => "too_many_cues",
Self::InvalidResult(_) => "invalid_result",
}
}
}