adding a lot more funcitonality to core library

This commit is contained in:
Chris Cochrun 2024-10-09 13:39:48 -05:00
parent ba51c56169
commit 3c87385895
7 changed files with 378 additions and 105 deletions

View file

@ -1,3 +1,5 @@
use color_eyre::eyre::Result;
use crate::images::Image;
use crate::presentations::Presentation;
use crate::songs::Song;
@ -6,10 +8,10 @@ use crate::videos::Video;
use super::kinds::ServiceItemKind;
#[derive(Debug, Default, PartialEq)]
struct ServiceItem {
id: i32,
database_id: i32,
kind: ServiceItemKind,
pub struct ServiceItem {
pub id: i32,
pub database_id: i32,
pub kind: ServiceItemKind,
}
#[derive(Debug, Default, PartialEq)]
@ -17,47 +19,96 @@ struct ServiceItemModel {
items: Vec<ServiceItem>,
}
impl From<Song> for ServiceItem {
fn from(_song: Song) -> Self {
impl From<&Song> for ServiceItem {
fn from(song: &Song) -> Self {
Self {
kind: ServiceItemKind::Song,
database_id: song.id,
..Default::default()
}
}
}
impl From<Video> for ServiceItem {
fn from(_video: Video) -> Self {
impl From<&Video> for ServiceItem {
fn from(video: &Video) -> Self {
Self {
kind: ServiceItemKind::Video,
database_id: video.id,
..Default::default()
}
}
}
impl From<Image> for ServiceItem {
fn from(_image: Image) -> Self {
impl From<&Image> for ServiceItem {
fn from(image: &Image) -> Self {
Self {
kind: ServiceItemKind::Image,
database_id: image.id,
..Default::default()
}
}
}
impl From<Presentation> for ServiceItem {
fn from(presentation: Presentation) -> Self {
let preskind = presentation.get_kind();
impl From<&Presentation> for ServiceItem {
fn from(presentation: &Presentation) -> Self {
Self {
kind: ServiceItemKind::Presentation(preskind.clone()),
kind: ServiceItemKind::Presentation(presentation.kind.clone()),
database_id: presentation.id,
..Default::default()
}
}
}
impl ServiceItemModel {
fn add_item(&mut self, item: impl Into<ServiceItem>) -> Result<()> {
let service_item: ServiceItem = item.into();
self.items.push(service_item);
Ok(())
}
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use crate::presentations::PresKind;
use super::*;
use pretty_assertions::{assert_eq, assert_ne};
fn test_song() -> Song {
Song {
title: "Death Was Arrested".to_string(),
..Default::default()
}
}
fn test_presentation() -> Presentation {
Presentation {
id: 0,
title: "20240327T133649--12-isaiah-and-jesus__lesson_project_tfc".into(),
path: PathBuf::from(
"~/docs/notes/lessons/20240327T133649--12-isaiah-and-jesus__lesson_project_tfc.html",
),
kind: PresKind::Html,
}
}
#[test]
pub fn test_service_item() {
assert_eq!(true, true)
let song = test_song();
let service_item = ServiceItem::from(&song);
let pres = test_presentation();
let pres_item = ServiceItem::from(&pres);
let mut service_model = ServiceItemModel::default();
match service_model.add_item(&song) {
Ok(_) => {
assert_eq!(ServiceItemKind::Song, service_model.items[0].kind);
assert_eq!(ServiceItemKind::Presentation(PresKind::Html), pres_item.kind);
assert_eq!(service_item, service_model.items[0]);
},
Err(e) => panic!("Problem adding item: {:?}", e),
}
}
}