Skip to main content

LyricsAccess

Trait LyricsAccess 

pub trait LyricsAccess<T>: Sized {
    // Required methods
    fn to_unsynced(self) -> String;
    fn active_tag_index(&self, timestamp: Duration) -> Option<usize>;
    fn active_tag(&self, timestamp: Duration) -> Option<&T>;
    fn is_active(&self, timestamp: Duration) -> bool;
    fn check_timestamp_order(&self) -> Result<(), TimestampError>;
    fn is_enhanced_lrc(&self) -> bool;
}
Expand description

Accessor trait for synced lyrics.

Required Methods§

fn to_unsynced(self) -> String

Returns unsynced lyrics without timestamps or additional metadata.

If you want to remove LRC data from lyrics without having to parse them first, consider using the [strip_tags] function.

§Examples
use lrc_rs::LyricsAccess;

let lyrics = SyncedLyrics::try_new(vec![
    LineTag::new(Duration::default(), "First line".to_string()),
    LineTag::new(Duration::from_secs_f32(1.1), "Second line".to_string()),
    LineTag::new(Duration::from_secs_f32(2.7), "Third line".to_string()),
])
.unwrap();
let expected = "First line
Second line
Third line";

assert_eq!(lyrics.to_unsynced(), expected);

fn active_tag_index(&self, timestamp: Duration) -> Option<usize>

Returns the index of the active tag or None if no tag is active for the given timestamp.

WARNING: this method may return wrong results if the segment timestamp order is not correct.

§Examples
use lrc_rs::LyricsAccess;

let lyrics = SyncedLyrics::try_new(vec![
    LineTag::new(Duration::from_secs(1), String::new()),
    LineTag::new(Duration::from_secs(2), String::new()),
    LineTag::new(Duration::from_secs(3), String::new()),
])
.unwrap();

assert_eq!(lyrics.active_tag_index(Duration::default()), None);
assert_eq!(lyrics.active_tag_index(Duration::from_secs_f32(0.5)), None);
assert_eq!(lyrics.active_tag_index(Duration::from_secs(1)), Some(0));
assert_eq!(lyrics.active_tag_index(Duration::from_secs(2)), Some(1));
assert_eq!(lyrics.active_tag_index(Duration::from_secs(u64::MAX)), Some(2));

fn active_tag(&self, timestamp: Duration) -> Option<&T>

Returns the active tag or None if no tag is active for the given timestamp.

WARNING: this method may return wrong results if the segment timestamp order is not correct.

§Examples
use lrc_rs::LyricsAccess;

let lyrics = SyncedLyrics::try_new(vec![
    LineTag::new(Duration::from_secs(1), String::new()),
    LineTag::new(Duration::from_secs(2), String::new()),
    LineTag::new(Duration::from_secs(3), String::new()),
])
.unwrap();

assert_eq!(lyrics.active_tag(Duration::default()), None);
assert_eq!(lyrics.active_tag(Duration::from_secs_f32(0.5)), None);
assert_eq!(lyrics.active_tag(Duration::from_secs(1)), lyrics.lines.get(0));
assert_eq!(lyrics.active_tag(Duration::from_secs(2)), lyrics.lines.get(1));
assert_eq!(lyrics.active_tag(Duration::from_secs(u64::MAX)), lyrics.lines.get(2));

fn is_active(&self, timestamp: Duration) -> bool

Returns whether the element is active for the given timestamp.

WARNING: this method may return wrong results if the segment timestamp order is not correct.

An element is considered active when timestamp is greater than or equal to its earliest timestamp.

fn check_timestamp_order(&self) -> Result<(), TimestampError>

Checks if timed tag timestamps are ordered correctly.

§Examples
use lrc_rs::LyricsAccess;

let line = LineTag {
    timestamp: Duration::from_secs(1),
    segments: vec![
        // This must be later than or equal to the line timestamp
        SegmentTag::new(Duration::default(), String::new())
    ]
};

assert_eq!(
    line.check_timestamp_order(),
    Err(
        lrc_rs::TimestampError {
            line: None,
            segment: Some(0),
            expected:
                lrc_rs::TimestampConstraint::GreaterThanOrEqual(Duration::from_secs(1)),
            actual: Duration::default(),
        }
    )
);

fn is_enhanced_lrc(&self) -> bool

Returns whether the element contains tags indicating that it uses the enhanced LRC format.

Enhanced LRC is indicated by either:

  • a LineTag containing multiple segments; or
  • a [SegmentTag] whose timestamp is later than its line’s timestamp.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§