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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
//! Control multiple instances and sequences at a time.
//!
//! Groups can be created with [`AudioManager::add_group`](crate::manager::AudioManager::add_group).
//! [`Sound`](crate::sound::Sound)s, [`Arrangement`](crate::arrangement::Arrangement)s
//! and [`Sequence`](crate::sequence::Sequence)s can be assigned
//! to any number of groups when they're created.
//! Groups themselves can also be assigned to groups.
//!
//! The [`pause`](handle::GroupHandle::pause), [`resume`](handle::GroupHandle::resume), and
//! [`stop`](handle::GroupHandle::stop) functions on [`GroupHandle`](handle::GroupHandle)s will
//! affect all instances that have the specified group anywhere in their ancestry.

pub(crate) mod groups;
pub mod handle;
mod set;

use handle::GroupHandle;
pub use set::GroupSet;
use uuid::Uuid;

/// A unique identifier for a group.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(
	feature = "serde_support",
	derive(serde::Serialize, serde::Deserialize),
	serde(transparent)
)]
pub struct GroupId {
	uuid: Uuid,
}

impl GroupId {
	pub(crate) fn new() -> Self {
		Self {
			uuid: Uuid::new_v4(),
		}
	}
}

impl From<&GroupHandle> for GroupId {
	fn from(handle: &GroupHandle) -> Self {
		handle.id()
	}
}

/// Settings for a group.
#[derive(Debug, Clone)]
#[cfg_attr(
	feature = "serde_support",
	derive(serde::Serialize, serde::Deserialize),
	serde(default)
)]
pub struct GroupSettings {
	/// The unique identifier for the group.
	pub id: Option<GroupId>,
	/// The groups this group belongs to.
	pub groups: GroupSet,
}

impl GroupSettings {
	/// Creates a new `GroupSettings` with the default settings.
	pub fn new() -> Self {
		Self::default()
	}

	/// Sets the unique identifier for the group.
	pub fn id(self, id: impl Into<GroupId>) -> Self {
		Self {
			id: Some(id.into()),
			..Default::default()
		}
	}

	/// Sets the groups this group belongs to.
	pub fn groups(self, groups: impl Into<GroupSet>) -> Self {
		Self {
			groups: groups.into(),
			..Default::default()
		}
	}
}

impl Default for GroupSettings {
	fn default() -> Self {
		Self {
			id: None,
			groups: GroupSet::new(),
		}
	}
}

#[derive(Debug, Clone)]
pub(crate) struct Group {
	groups: GroupSet,
}

impl Group {
	pub fn new(settings: GroupSettings) -> Self {
		Self {
			groups: settings.groups,
		}
	}

	pub fn groups(&self) -> &GroupSet {
		&self.groups
	}
}