piper1-rs 0.1.0

Safe Rust bindings to Piper, the TTS library
Documentation
use std::{ffi::NulError, fmt, os::raw::c_int};

/// A wrapper around [`Result`] with a [`PiperError`].
pub type PiperResult<T> = Result<T, PiperError>;

/// Global error type for all errors that Piper can return.
#[derive(Debug)]
pub enum PiperError {
	/// A string passed to Piper included a internal `\0` (NUL) character.
	InternalNullByte(NulError),
	/// Piper returned a null pointer.
	NullPointer,
	/// Piper returned a generic error code.
	GenericError,
	/// Piper returned a unknown error code.
	UnknownError(c_int),
	/// IO error
	Io(std::io::Error),
}

impl fmt::Display for PiperError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			PiperError::InternalNullByte(e) => {
				write!(f, "got a null byte in a string passed to piper: {}", e)
			}
			PiperError::NullPointer => f.write_str("piper returned a null pointer"),
			PiperError::GenericError => f.write_str("piper returned an error"),
			PiperError::UnknownError(e) => {
				write!(f, "piper returned an unknown error code: {}", e)
			}
			PiperError::Io(e) => write!(f, "IO error: {}", e),
		}
	}
}

impl std::error::Error for PiperError {}

impl From<NulError> for PiperError {
	fn from(err: NulError) -> PiperError {
		PiperError::InternalNullByte(err)
	}
}

impl From<std::io::Error> for PiperError {
	fn from(err: std::io::Error) -> PiperError {
		PiperError::Io(err)
	}
}