1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::{Framerate, TimecodeParseError};
use std::convert::TryFrom;
use std::fmt::Debug;

/// The result type of [PremiereTicksSource::to_ticks].
pub type PremiereTicksSourceResult = Result<i64, TimecodeParseError>;

/// Types implementing this trait can be converted into the number of Adobe Premiere Pro Ticks that
/// have elapsed since a timecode value of 00:00:00:00.
pub trait PremiereTicksSource: Debug {
    fn to_ticks(&self, rate: Framerate) -> PremiereTicksSourceResult;
}

impl<T> PremiereTicksSource for &T
where
    T: PremiereTicksSource,
{
    /// Returns the number of Adobe Premiere Pro ticks this value represents.
    fn to_ticks(&self, rate: Framerate) -> PremiereTicksSourceResult {
        (*self).to_ticks(rate)
    }
}

impl PremiereTicksSource for &dyn PremiereTicksSource {
    fn to_ticks(&self, rate: Framerate) -> PremiereTicksSourceResult {
        (*self).to_ticks(rate)
    }
}

impl PremiereTicksSource for i64 {
    fn to_ticks(&self, _: Framerate) -> PremiereTicksSourceResult {
        Ok(*self)
    }
}

impl PremiereTicksSource for u64 {
    fn to_ticks(&self, _: Framerate) -> PremiereTicksSourceResult {
        let i64_val = match i64::try_from(*self) {
            Ok(converted) => converted,
            Err(err) => {
                return Err(TimecodeParseError::Conversion(format!(
                    "error converting u64 to i64 : {}",
                    err.to_string()
                )))
            }
        };

        Ok(i64_val)
    }
}