use std::fmt;
use std::io;
use semq_sys as sys;
use crate::build;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Which {
Content,
State,
}
impl Which {
pub fn name(self) -> &'static str {
match self {
Which::Content => "content",
Which::State => "state",
}
}
fn from_raw(which: u32) -> Option<Which> {
match which {
sys::SEMQ_WHICH_CONTENT => Some(Which::Content),
sys::SEMQ_WHICH_STATE => Some(Which::State),
_ => None,
}
}
}
impl fmt::Display for Which {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("{message}")]
InvalidInput {
row: Option<u64>,
field: Option<u64>,
message: String,
},
#[error("{message}")]
Incompatible { field: Option<u64>, message: String },
#[error("{message}")]
FormatError {
row: Option<u64>,
section: Option<u64>,
message: String,
},
#[error("{message}")]
IntegrityError {
which: Option<Which>,
message: String,
},
#[error("{message}")]
Unsupported {
operation: &'static str,
message: String,
},
#[error(
"{message} [operation={operation} status={status} sdk={sdk_version} \
core={core_version} build={build_id}]"
)]
Native {
operation: &'static str,
status: u32,
row: Option<u64>,
field: Option<u64>,
message: String,
sdk_version: &'static str,
core_version: &'static str,
build_id: &'static str,
},
}
impl Error {
pub(crate) fn invalid(message: impl Into<String>) -> Error {
Error::InvalidInput {
row: None,
field: None,
message: message.into(),
}
}
pub(crate) fn invalid_at(
row: Option<u64>,
field: Option<u64>,
message: impl Into<String>,
) -> Error {
Error::InvalidInput {
row,
field,
message: message.into(),
}
}
pub(crate) fn native(
operation: &'static str,
status: u32,
row: Option<u64>,
field: Option<u64>,
message: impl Into<String>,
) -> Error {
Error::Native {
operation,
status,
row,
field,
message: message.into(),
sdk_version: build::SDK_VERSION,
core_version: build::core_version(),
build_id: build::build_id(),
}
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> io::Error {
let kind = match err {
Error::InvalidInput { .. } | Error::Incompatible { .. } => io::ErrorKind::InvalidInput,
Error::FormatError { .. } | Error::IntegrityError { .. } => io::ErrorKind::InvalidData,
Error::Unsupported { .. } => io::ErrorKind::Unsupported,
Error::Native { .. } => io::ErrorKind::Other,
};
io::Error::new(kind, err)
}
}
pub(crate) fn new_error() -> sys::semq_error_t {
sys::semq_error_t {
status: sys::SEMQ_OK,
which: sys::SEMQ_WHICH_NONE,
row: sys::SEMQ_NONE,
field: sys::SEMQ_NONE,
message: [0; 128],
}
}
pub(crate) fn check(
status: sys::semq_status_t,
err: &sys::semq_error_t,
operation: &'static str,
) -> Result<()> {
if status == sys::SEMQ_OK {
return Ok(());
}
let mut message = message_of(err);
if message.is_empty() {
message = build::status_name(status).to_owned();
}
let row = opt(err.row);
let field = opt(err.field);
Err(match status {
sys::SEMQ_ERR_INVALID_INPUT => Error::InvalidInput {
row,
field,
message,
},
sys::SEMQ_ERR_INCOMPATIBLE => Error::Incompatible { field, message },
sys::SEMQ_ERR_FORMAT => Error::FormatError {
row,
section: field,
message,
},
sys::SEMQ_ERR_INTEGRITY => Error::IntegrityError {
which: Which::from_raw(err.which),
message,
},
sys::SEMQ_ERR_UNSUPPORTED => Error::Unsupported { operation, message },
_ => Error::native(operation, status, row, field, message),
})
}
fn opt(value: u64) -> Option<u64> {
(value != sys::SEMQ_NONE).then_some(value)
}
fn message_of(err: &sys::semq_error_t) -> String {
let bytes: Vec<u8> = err
.message
.iter()
.take_while(|&&c| c != 0)
.map(|&c| {
#[allow(clippy::unnecessary_cast)]
let byte = c as u8;
byte
})
.collect();
String::from_utf8_lossy(&bytes).into_owned()
}