use crate::objects::object::ObjectType;
use sqrite::sql_interface::{FromSql, FromSqlError, ToSql, ToSqlError};
use sqrite::value::Value;
use std::fmt::{Display, Formatter};
#[derive(Copy, Clone, Debug, Hash, Ord, PartialOrd, Eq, PartialEq)]
pub struct TypedId(i64);
impl FromSql for TypedId {
fn from_sql(value: Value) -> Result<Self, FromSqlError> {
let value: i64 = FromSql::from_sql(value)?;
if value & (0x7F << 56) == 0 {
return Err(FromSqlError::Custom(String::from(
"typed id is missing type information",
)));
}
Ok(TypedId(value))
}
}
impl Display for TypedId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl TypedId {
pub fn is_null(&self) -> bool {
self.0 == 0
}
pub fn get_object_type(&self) -> ObjectType {
let v = (self.0 >> 56) as i8;
ObjectType::from_int(v).expect(&format!("TypedId has invalid type: {}", v))
}
}
impl ToSql for TypedId {
fn to_sql(&self) -> Result<Value, ToSqlError> {
Ok(Value::Integer(self.0))
}
}