looklook 0.6.0

Descriptive signal synthesiser.
// Copyright 2026 Gabriel Bjørnager Jensen.
//
// This file is part of LOOKLOOK.
//
// LOOKLOOK is free software: you can redistribute
// it and/or modify it under the terms of the GNU
// Affero General Public License as published by
// the Free Software Foundation, either version 3
// of the License, or (at your option) any later
// version.
//
// LOOKLOOK is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Af-
// fero General Public License along with LOOKLOOK.
// If not, see <https://www.gnu.org/licenses/>.

//! The [`Blob`] type.

mod dump;

use crate::config::Config;
use crate::signal::Signal;

/// A binary signal blob.
///
/// This type encodes a
#[derive(Clone, Debug)]
pub struct Blob<'a> {
	/// The blob configuration.
	config: Config,

	/// The blob data.
	data: BlobData<'a>,
}

impl<'a> Blob<'a> {
	/// Constructs a new raw blob.
	#[inline]
	#[must_use]
	pub const fn new(signal: &'a Signal, config: Config) -> Self {
		Self {
			config,
			data:   BlobData::Raw(signal.data()),
		}
	}

	/// Borrows the signal configuration.
	#[inline]
	#[must_use]
	pub const fn config(&self) -> &Config {
		&self.config
	}

	/// Overwrites the signal configuration.
	#[inline]
	pub const fn set_config(&mut self, config: Config) {
		self.config = config;
	}

	/// Tests if the blob is raw.
	#[inline]
	#[must_use]
	pub const fn is_raw(&self) -> bool {
		matches!(self.data, BlobData::Raw(_))
	}

	/// Tests if the blob is encoded.
	#[inline]
	#[must_use]
	pub const fn is_encoded(&self) -> bool {
		matches!(self.data, BlobData::Encoded(_))
	}

	/// Converts the blob into a raw (borrowed) one.
	///
	/// If the blob is already raw, its data is still
	/// replaced.
	#[inline]
	pub fn make_raw_with_data(&mut self, data: &'a [u8]) {
		self.data = BlobData::Raw(data);
	}

	/// Converts the blob into an encoded (owned) one.
	///
	/// If the blob is already encoded, nothing is done.
	#[inline]
	pub fn make_encoded(&mut self) {
		if let BlobData::Raw(data) = self.data {
			self.data = BlobData::Encoded(data.into());
		}
	}

	/// Retrieves the blob size.
	#[inline]
	#[must_use]
	pub const fn size(&self) -> usize {
		match self.data {
			BlobData::Raw(data) => {
				data.len()
			}

			BlobData::Encoded(ref data) => {
				data.len()
			}
		}
	}

	/// Borrows the blob data.
	#[inline]
	#[must_use]
	pub const fn data(&self) -> &[u8] {
		match self.data {
			BlobData::Raw(data) => {
				data
			}

			BlobData::Encoded(ref data) => {
				data
			}
		}
	}
}

/// Blob data.
#[derive(Clone, Debug)]
enum BlobData<'a> {
	/// Raw (borrowed) data.
	Raw(&'a [u8]),

	/// Encoded (owned) data.
	Encoded(Box<[u8]>),
}