use alpha2_diagnose::advise::{Misplacement, Misplacements};
use alpha2_diagnose::{Diagnostic, Node, Number, Path};
use super::place::Locator;
use super::{catalog, references};
use crate::ModelError;
pub static MISPLACEMENTS: Misplacements = Misplacements(&[
Misplacement {
definition: "Clip",
field: "effects",
help: "a clip has no effects list: picture effects are `effect` nodes of a composition \
the clip shows through `composition`, where a `clip` node is the clip's picture; \
sound effects go in `audio.effects`",
},
Misplacement {
definition: "Clip",
field: "effect",
help: "a clip has no effect field: picture effects are `effect` nodes of a composition \
the clip shows through `composition`, where a `clip` node is the clip's picture; \
sound effects go in `audio.effects`",
},
Misplacement {
definition: "Track",
field: "effects",
help: "a track's sound effects go in `audio.effects`; picture effects are `effect` nodes \
of the composition each clip shows through `composition`",
},
]);
const KEYFRAME_SEARCH_LIMIT: usize = 100_000;
pub fn advise(diagnostic: &mut Diagnostic, error: &ModelError, locator: &Locator<'_>) {
references::advise(diagnostic, error, locator);
catalog::advise(diagnostic, error, locator);
if let Some((found, maximum)) = limit(error) {
let mut entries = vec![(
"maximum".to_owned(),
Node::number(Number::unsigned(maximum)),
)];
if let Some(found) = found {
entries.insert(
0,
("found".to_owned(), Node::number(Number::unsigned(found))),
);
}
merge_data(diagnostic, entries);
}
if diagnostic.code.starts_with("model.invalid_")
&& let Some(line) = keyframes_out_of_order(locator.tree(), &diagnostic.path)
{
diagnostic.help.push(line);
}
}
pub(super) fn merge_data(diagnostic: &mut Diagnostic, added: Vec<(String, Node)>) {
let mut entries: Vec<(String, Node)> = diagnostic
.data
.take()
.as_ref()
.and_then(Node::as_map)
.map(|entries| {
entries
.iter()
.map(|entry| (entry.key.clone(), entry.value.clone()))
.collect()
})
.unwrap_or_default();
entries.extend(added);
diagnostic.data = Some(Node::map(entries));
}
fn count(value: usize) -> u64 {
u64::try_from(value).unwrap_or(u64::MAX)
}
fn limit(error: &ModelError) -> Option<(Option<u64>, u64)> {
use ModelError as E;
Some(match error {
E::TooManyDataSets { found, maximum }
| E::TooManyAudioBuses { found, maximum }
| E::TooManyCompositionNodes { found, maximum, .. }
| E::TooManyEffectResources { found, maximum }
| E::TooManyCompositions { found, maximum }
| E::TooManySources { found, maximum }
| E::TooManyTracks { found, maximum }
| E::TooManyClips { found, maximum }
| E::TooManyAudioEffects { found, maximum, .. }
| E::TooManyClipsOnTrack { found, maximum, .. }
| E::TooManyCaptionCues { found, maximum, .. }
| E::TooManyTransitions { found, maximum, .. } => (Some(count(*found)), count(*maximum)),
E::TooManyLoopPasses { found, maximum, .. } => (Some(*found), *maximum),
E::FrameDrawsTooManyNodes { found, maximum, .. } => (Some(*found), count(*maximum)),
E::TooMuchAudioDelay {
found_ms,
maximum_ms,
..
} => (Some(*found_ms), u64::from(*maximum_ms)),
E::CompositionNestedTooDeep { maximum, .. } | E::TooManyInlinedNodes { maximum } => {
(None, count(*maximum))
}
_ => return None,
})
}
fn keyframes_out_of_order(tree: &Node, path: &Path) -> Option<String> {
let mut stack = vec![(path.clone(), tree.at(path)?)];
let mut visited = 0_usize;
while let Some((at, node)) = stack.pop() {
visited += 1;
if visited > KEYFRAME_SEARCH_LIMIT {
return None;
}
if let Some(entries) = node.as_map() {
for entry in entries.iter().rev() {
let place = at.key(entry.key.clone());
if entry.key == "keyframes"
&& let Some(line) = unordered(&place, &entry.value)
{
return Some(line);
}
stack.push((place, &entry.value));
}
} else if let Some(items) = node.as_list() {
for (index, item) in items.iter().enumerate().rev() {
stack.push((at.index(index), item));
}
}
}
None
}
fn unordered(place: &Path, keyframes: &Node) -> Option<String> {
let times: Vec<i128> = keyframes
.as_list()?
.iter()
.map(|keyframe| keyframe.get("time")?.as_number()?.as_i128())
.collect::<Option<_>>()?;
let index = times.windows(2).position(|pair| pair[0] >= pair[1])?;
Some(format!(
"{} at tick {} and {} at tick {} are out of order; keyframe times must increase",
place.index(index),
times[index],
place.index(index + 1),
times[index + 1]
))
}