cat-dev 0.0.13

A library for interacting with the CAT-DEV hardware units distributed by Nintendo (i.e. a type of Wii-U DevKits).
Documentation
//! Error types specifically for interacting with MION CGI's/protos.

use crate::{
	errors::{APIError, CatBridgeError, NetworkError},
	mion::firmware::MionFirmwareAPIError,
};
use miette::Diagnostic;
use thiserror::Error;

#[cfg(any(feature = "clients", feature = "servers"))]
use crate::{
	errors::NetworkParseError,
	mion::proto::{
		cgis::MionCGIErrors,
		control::MionControlProtocolError,
		parameter::{MionParamProtocolError, MionParameterAPIError},
	},
};

/// Errors that come from MION APIs specifically.
#[derive(Error, Diagnostic, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MionAPIError {
	/// You attempted to set the default host bridge to a bridge that does not exist.
	#[error("You cannot set a default bridge that does not exist.")]
	#[diagnostic(code(cat_dev::api::mion::default_device_must_exist))]
	DefaultDeviceMustExist,
	/// You attempted to create a MION device name, but it was empty.
	///
	/// MION Identities must have at LEAST 1 byte.
	#[error("The device name cannot be empty, it must be at least one byte long.")]
	#[diagnostic(code(cat_dev::api::mion::device_name_cannot_be_empty))]
	DeviceNameCannotBeEmpty,
	/// You attempted to create a MION device name, which did not contain ASCII
	/// characters.
	///
	/// MION Identities must contain all ascii characters.
	#[error("A device name has to be completely ASCII! But it wasn't ASCII!")]
	#[diagnostic(code(cat_dev::api::mion::device_name_not_ascii))]
	DeviceNameMustBeAscii,
	/// You attempted to create a MION device name, but it was longer than 255
	/// bytes.
	///
	/// A MION device name has to be serialized into a packet, where it's length
	/// is represented as a [`u8`] which means it can only be [`u8::MAX`], aka
	/// 255 bytes.
	#[error("A Device Name can only be 255 bytes long, but you specified one: {0} bytes long.")]
	#[diagnostic(code(cat_dev::api::mion::device_name_too_long))]
	DeviceNameTooLong(usize),
	/// An error occured handling firmware.
	#[error(transparent)]
	#[diagnostic(transparent)]
	Firmware(#[from] MionFirmwareAPIError),
	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
	#[cfg(any(feature = "clients", feature = "servers"))]
	#[error(transparent)]
	#[diagnostic(transparent)]
	ParameterSpace(#[from] MionParameterAPIError),
}

impl From<MionFirmwareAPIError> for APIError {
	fn from(value: MionFirmwareAPIError) -> Self {
		Self::Mion(value.into())
	}
}
impl From<MionFirmwareAPIError> for CatBridgeError {
	fn from(value: MionFirmwareAPIError) -> Self {
		Self::API(value.into())
	}
}

#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionParameterAPIError> for APIError {
	fn from(value: MionParameterAPIError) -> Self {
		Self::Mion(value.into())
	}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionParameterAPIError> for CatBridgeError {
	fn from(value: MionParameterAPIError) -> Self {
		Self::API(value.into())
	}
}

/// Errors dealing with various MION Protocols.
#[derive(Error, Diagnostic, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MionProtocolError {
	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
	#[cfg(any(feature = "clients", feature = "servers"))]
	/// Errors related to CGI, and HTML pages.
	#[error(transparent)]
	#[diagnostic(transparent)]
	CGI(#[from] MionCGIErrors),
	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
	#[cfg(any(feature = "clients", feature = "servers"))]
	/// Errors related to the CONTROL protocol for MION.
	#[error(transparent)]
	#[diagnostic(transparent)]
	Control(#[from] MionControlProtocolError),
	#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
	#[cfg(any(feature = "clients", feature = "servers"))]
	/// Errors related to the PARAMETER SPACE protocol for MION.
	#[error(transparent)]
	#[diagnostic(transparent)]
	Params(#[from] MionParamProtocolError),
}

impl From<MionProtocolError> for NetworkError {
	fn from(value: MionProtocolError) -> Self {
		Self::Parse(value.into())
	}
}
impl From<MionProtocolError> for CatBridgeError {
	fn from(value: MionProtocolError) -> Self {
		Self::Network(value.into())
	}
}

#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionCGIErrors> for NetworkParseError {
	fn from(value: MionCGIErrors) -> Self {
		Self::Mion(value.into())
	}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionCGIErrors> for NetworkError {
	fn from(value: MionCGIErrors) -> Self {
		Self::Parse(value.into())
	}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionCGIErrors> for CatBridgeError {
	fn from(value: MionCGIErrors) -> Self {
		Self::Network(value.into())
	}
}

#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionParamProtocolError> for NetworkParseError {
	fn from(value: MionParamProtocolError) -> Self {
		Self::Mion(value.into())
	}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionParamProtocolError> for NetworkError {
	fn from(value: MionParamProtocolError) -> Self {
		Self::Parse(value.into())
	}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionParamProtocolError> for CatBridgeError {
	fn from(value: MionParamProtocolError) -> Self {
		Self::Network(value.into())
	}
}

#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionControlProtocolError> for NetworkParseError {
	fn from(value: MionControlProtocolError) -> Self {
		Self::Mion(value.into())
	}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionControlProtocolError> for NetworkError {
	fn from(value: MionControlProtocolError) -> Self {
		Self::Parse(value.into())
	}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "clients", feature = "servers"))))]
#[cfg(any(feature = "clients", feature = "servers"))]
impl From<MionControlProtocolError> for CatBridgeError {
	fn from(value: MionControlProtocolError) -> Self {
		Self::Network(value.into())
	}
}