Skip to main content

open_timeline_core/reduced/
timelines.rs

1// SPDX-License-Identifier: MIT
2
3//!
4//! Collection of reduced timelines
5//!
6
7use crate::{IsReducedCollection, IsReducedType, Name, OpenTimelineId, ReducedTimeline};
8use serde::{Deserialize, Serialize};
9use std::collections::BTreeSet;
10
11/// Container for a set of [`ReducedTimeline`]s
12#[rustfmt::skip]
13#[derive(derive_more::IntoIterator, Serialize, Deserialize, Default, PartialEq, Eq, Debug, Clone, PartialOrd, Ord, Hash)]
14#[into_iterator(owned, ref, ref_mut)]
15pub struct ReducedTimelines(BTreeSet<ReducedTimeline>);
16
17impl IsReducedCollection for ReducedTimelines {
18    type Item = ReducedTimeline;
19
20    fn collection(&self) -> &BTreeSet<<Self as IsReducedCollection>::Item> {
21        &self.0
22    }
23
24    fn collection_mut(&mut self) -> &mut BTreeSet<<Self as IsReducedCollection>::Item> {
25        &mut self.0
26    }
27}
28
29// TODO: these are nearly the same as those for ReducedEntities
30impl FromIterator<ReducedTimeline> for ReducedTimelines {
31    fn from_iter<T: IntoIterator<Item = ReducedTimeline>>(iter: T) -> Self {
32        ReducedTimelines(iter.into_iter().collect())
33    }
34}
35
36impl ReducedTimelines {
37    pub fn new() -> Self {
38        ReducedTimelines(BTreeSet::new())
39    }
40
41    // TODO: trait? (also in reduced_entities.rs)
42    pub fn ordered_by_name(&self) -> Vec<ReducedTimeline> {
43        let mut sorted: Vec<_> = self.0.clone().into_iter().collect();
44        sorted.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
45        sorted
46    }
47
48    // TODO: trait? (also in reduced_entities.rs)
49    pub fn ordered_by_id(&self) -> Vec<ReducedTimeline> {
50        let mut sorted: Vec<_> = self.0.clone().into_iter().collect();
51        sorted.sort_by_key(|a| a.id());
52        sorted
53    }
54
55    pub fn ids(&self) -> BTreeSet<OpenTimelineId> {
56        self.0
57            .clone()
58            .into_iter()
59            .map(|timeline| timeline.id())
60            .collect()
61    }
62
63    pub fn names(&self) -> BTreeSet<Name> {
64        self.0
65            .clone()
66            .into_iter()
67            .map(|timeline| timeline.name().to_owned())
68            .collect()
69    }
70}