#[cfg(feature = "derive")]
pub use fav_derive::Attr;
#[allow(missing_docs)]
#[derive(Debug, PartialEq)]
pub enum Id<'a> {
I64(i64),
I32(i32),
String(&'a str),
}
#[allow(missing_docs)]
pub trait Attr {
fn id(&self) -> Id;
fn title(&self) -> &str;
fn set_id(&mut self, id: Id);
fn set_title(&mut self, title: &str);
}
impl From<i64> for Id<'_> {
fn from(id: i64) -> Self {
Id::I64(id)
}
}
impl From<i32> for Id<'_> {
fn from(id: i32) -> Self {
Id::I32(id)
}
}
impl From<&i64> for Id<'_> {
fn from(id: &i64) -> Self {
Id::I64(*id)
}
}
impl From<&i32> for Id<'_> {
fn from(id: &i32) -> Self {
Id::I32(*id)
}
}
impl<'a> From<&'a str> for Id<'a> {
fn from(id: &'a str) -> Self {
match id.parse::<i32>() {
Ok(id) => Id::I32(id),
Err(_) => match id.parse::<i64>() {
Ok(id) => Id::I64(id),
Err(_) => Id::String(id),
},
}
}
}
impl<'a> From<&'a String> for Id<'a> {
fn from(id: &'a String) -> Self {
id.as_str().into()
}
}
impl From<Id<'_>> for i32 {
fn from(value: Id) -> Self {
match value {
Id::I32(id) => id,
_ => panic!("Not i32 id"),
}
}
}
impl From<Id<'_>> for i64 {
fn from(value: Id) -> Self {
match value {
Id::I64(id) => id,
_ => panic!("Not i64 id"),
}
}
}
impl From<Id<'_>> for String {
fn from(value: Id) -> Self {
match value {
Id::String(id) => id.to_owned(),
Id::I64(id) => id.to_string(),
Id::I32(id) => id.to_string(),
}
}
}