#![doc=include_str!("../README.md")]
#![doc(html_logo_url = "https://gitlab.com/john_t/gladis4/-/raw/master/logo.png")]
use std::{error::Error, fmt::Display};
#[derive(Debug, Clone)]
pub struct NotFoundError {
pub identifier: String,
pub typ: String,
}
impl Display for NotFoundError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"identifier {} of type {} was not found",
self.identifier, self.typ
)
}
}
impl Error for NotFoundError {}
#[derive(Debug, Clone)]
pub enum GladisError {
NotFound(NotFoundError),
}
impl Display for GladisError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GladisError::NotFound(e) => write!(f, "not found error: {}", e),
}
}
}
impl Error for GladisError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
GladisError::NotFound(e) => Some(e),
}
}
}
impl GladisError {
pub fn not_found<T>(identifier: T, typ: T) -> Self
where
T: ToString,
{
let identifier = identifier.to_string();
let typ = typ.to_string();
GladisError::NotFound(NotFoundError { identifier, typ })
}
}
pub type Result<T> = std::result::Result<T, GladisError>;
pub trait Gladis {
fn from_builder(builder: gtk::Builder) -> Result<Self>
where
Self: std::marker::Sized;
fn from_string(src: &str) -> Result<Self>
where
Self: std::marker::Sized,
{
let builder = gtk::Builder::from_string(src);
Gladis::from_builder(builder)
}
fn from_resource(resource_path: &str) -> Result<Self>
where
Self: std::marker::Sized,
{
let builder = gtk::Builder::from_resource(resource_path);
Gladis::from_builder(builder)
}
}
#[cfg(feature = "derive")]
#[doc(hidden)]
pub use gladis_proc_macro::Gladis;
#[cfg(test)]
mod tests {
use crate::{GladisError, NotFoundError};
#[test]
fn fmt_not_found_error() {
let err = NotFoundError {
identifier: "foo".to_string(),
typ: "bar".to_string(),
};
assert_eq!(err.to_string(), "identifier foo of type bar was not found");
}
#[test]
fn fmt_gladis_error() {
let err = GladisError::NotFound(NotFoundError {
identifier: "foo".to_string(),
typ: "bar".to_string(),
});
assert_eq!(
err.to_string(),
"not found error: identifier foo of type bar was not found"
);
}
}