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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
//! Base types and traits.
// Helpers.
use std::time::Duration;
#[cfg(feature = "audio")]
use rodio::{OutputStream, OutputStreamHandle, Sink};
/// Global result type.
pub type Res<T> = anyhow::Result<T>;
/// Global error type.
pub type Err = anyhow::Error;
/// Global void type.
pub type Void = Res<()>;
// Traits.
/// A trait for types that have a static name.
pub trait HasStaticName {
/// Returns the static name of the type.
fn static_name(&self) -> &'static str;
}
/// A trait for types that have a computed name.
pub trait HasName {
/// Returns the computed name of the type.
fn name(&self) -> String;
}
/// A trait for types that have a computed name.
pub trait HasPreciseName {
/// Returns the computed name of the type.
fn precise_name(&self) -> String;
}
/// A trait for types that have a description.
pub trait HasDescription {
/// Returns the description of the type.
fn description(&self) -> &'static str;
}
/// A trait for types that can be parsed from a string.
pub trait Parsable {
/// Parses the type from a string.
fn parse(symbol: &str) -> Res<Self>
where
Self: Sized;
}
/// A struct for holding the types for a [`Playable`].
#[cfg(feature = "audio")]
pub struct PlaybackHandle {
_stream: OutputStream,
_stream_handle: OutputStreamHandle,
_sinks: Vec<Sink>,
}
#[cfg(feature = "audio")]
impl PlaybackHandle {
/// Creates a new [`PlayableResult`].
pub fn new(stream: OutputStream, stream_handle: OutputStreamHandle, sinks: Vec<Sink>) -> Self {
Self {
_stream: stream,
_stream_handle: stream_handle,
_sinks: sinks,
}
}
}
/// A trait for types that can be "played" via the system's audio output.
/// ```rust, no_run
/// use std::time::Duration;
///
/// use klib::core::base::Playable;
/// use klib::core::{named_pitch::NamedPitch, note::Note, octave::Octave};
///
/// let handle = Note::new(NamedPitch::A, Octave::Four).play(
/// Duration::ZERO,
/// Duration::from_secs(1),
/// Duration::ZERO,
/// );
/// std::thread::sleep(Duration::from_secs(1));
/// ```
#[cfg(feature = "audio")]
pub trait Playable {
/// Plays the [`Playable`].
#[must_use = "Dropping the PlayableResult will stop the playback."]
fn play(&self, delay: Duration, length: Duration, fade_in: Duration) -> Res<PlaybackHandle>;
}