moving to generic based models instead of traits
This commit is contained in:
parent
8f065380aa
commit
7a8e6c41cd
4 changed files with 150 additions and 140 deletions
|
@ -31,53 +31,6 @@ const VERSE_KEYWORDS: [&'static str; 24] = [
|
|||
"Other 2", "Other 3", "Other 4",
|
||||
];
|
||||
|
||||
impl Modeling for Model<Song> {
|
||||
type Item = Song;
|
||||
fn add_item(&mut self, item: Self::Item) -> Result<()> {
|
||||
self.items.push(item);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_to_db(&mut self, _item: Self::Item) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn update_item(
|
||||
&mut self,
|
||||
item: Self::Item,
|
||||
index: i32,
|
||||
) -> Result<()> {
|
||||
if let Some(current_song) = self.items.get_mut(index as usize)
|
||||
{
|
||||
let _old_song = replace(current_song, item);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(eyre!(
|
||||
"Song doesn't exist in model. Id was {}",
|
||||
index
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_item(&self, index: i32) -> Option<&Self::Item> {
|
||||
self.items.get(index as usize)
|
||||
}
|
||||
|
||||
fn remove_item(&mut self, index: i32) -> Result<()> {
|
||||
self.items.remove(index as usize);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn insert_item(
|
||||
&mut self,
|
||||
item: Self::Item,
|
||||
index: i32,
|
||||
) -> Result<()> {
|
||||
self.items.insert(index as usize, item);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Model<Song> {
|
||||
pub fn load_from_db(&mut self) {
|
||||
// static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3";
|
||||
|
@ -277,7 +230,7 @@ You saved my soul"
|
|||
|
||||
#[test]
|
||||
pub fn test_db_and_model() {
|
||||
let mut song_model = Model::default();
|
||||
let mut song_model: Model<Song> = Model::default();
|
||||
song_model.load_from_db();
|
||||
if let Some(song) = song_model.get_item(3) {
|
||||
let test_song = test_song();
|
||||
|
@ -291,7 +244,7 @@ You saved my soul"
|
|||
pub fn test_update() {
|
||||
let song = test_song();
|
||||
let cloned_song = song.clone();
|
||||
let mut song_model = Model::default();
|
||||
let mut song_model: Model<Song> = Model::default();
|
||||
song_model.load_from_db();
|
||||
|
||||
match song_model.update_item(song, 2) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue