use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
use crate::timestamp::Timestamp;
use crate::LyricsError;
#[derive(Debug, PartialOrd, PartialEq, Ord, Eq, Hash, Clone, Copy)]
pub struct TimeTag(Timestamp);
impl TimeTag {
#[inline]
pub fn new<N: Into<i64>>(timestamp: N) -> TimeTag {
TimeTag(Timestamp::new(timestamp))
}
#[allow(clippy::should_implement_trait)]
#[inline]
pub fn from_str<S: AsRef<str>>(timestamp: S) -> Result<TimeTag, LyricsError> {
let timestamp = timestamp.as_ref();
let timestamp = timestamp.strip_prefix('[').map(|s| s.trim_start()).unwrap_or(timestamp);
let timestamp = timestamp.strip_suffix(']').map(|s| s.trim_end()).unwrap_or(timestamp);
Ok(TimeTag(Timestamp::from_str(timestamp)?))
}
}
impl TimeTag {
#[inline]
pub fn get_timestamp(self) -> i64 {
self.0.get_timestamp()
}
}
impl Display for TimeTag {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
f.write_fmt(format_args!("[{}]", self.0))
}
}
impl FromStr for TimeTag {
type Err = LyricsError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
TimeTag::from_str(s)
}
}
impl From<TimeTag> for i64 {
#[inline]
fn from(tt: TimeTag) -> i64 {
tt.0.into()
}
}