use conciliator::{
Buffer,
Inline,
Paint
};
use rusqlite::{
Row,
Error as SqlError
};
use super::Category;
pub enum ActOrCat {
Activity(Activity),
Category(Category)
}
#[derive(Debug, Eq, PartialEq)]
pub struct Activity {
pub id: u64,
pub category_id: u64,
pub name: String
}
#[derive(Debug, Eq, PartialEq)]
pub struct Resolved {
pub id: u64,
pub category_id: u64,
pub path: String,
pub name: String
}
impl Activity {
pub fn resolve(self, path: String) -> Resolved {
let Self {id, category_id, name} = self;
Resolved {id, category_id, path, name}
}
}
impl<'r> TryFrom<&Row<'r>> for Activity {
type Error = SqlError;
fn try_from(value: &Row<'r>) -> Result<Self, Self::Error> {
value.try_into()
.map(|(id, name, category_id)| Self {id, name, category_id})
}
}
impl Inline for Activity {
fn inline(&self, buf: &mut Buffer) {
buf.push_beta(&self.name);
}
}
impl Inline for ActOrCat {
fn inline(&self, buffer: &mut Buffer) {
match self {
Self::Activity(act) => act.inline(buffer),
Self::Category(cat) => cat.inline(buffer)
}
}
}
impl ActOrCat {
pub(crate) fn len(&self) -> usize {
match self {
Self::Activity(act) => act.name.len(),
Self::Category(cat) => cat.name.len()
}
}
}
impl Resolved {
pub fn revert(self) -> Activity {
let Self {id, category_id, name, ..} = self;
Activity {id, category_id, name}
}
}
impl Inline for Resolved {
fn inline(&self, buffer: &mut Buffer) {
buffer.push_plain(&self.path).push_beta(&self.name);
}
}