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
|
@ -1,4 +1,6 @@
|
|||
use color_eyre::eyre::Result;
|
||||
use std::mem::replace;
|
||||
|
||||
use color_eyre::eyre::{eyre, Result};
|
||||
use sqlx::{Connection, SqliteConnection};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -6,34 +8,43 @@ pub struct Model<T> {
|
|||
pub items: Vec<T>,
|
||||
pub db: SqliteConnection,
|
||||
}
|
||||
impl<T> Model<T> {
|
||||
pub fn add_item(&mut self, item: T) -> Result<()> {
|
||||
self.items.push(item);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// impl<T> Modeling for Model<T> {
|
||||
// type Item = T;
|
||||
// fn add_item(&mut self, item: Self::Item) -> Result<()> {
|
||||
// self.items.push(item);
|
||||
// Ok(())
|
||||
// }
|
||||
pub fn add_to_db(&mut self, item: T) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
// fn add_to_db(&mut self, item: Self::Item) -> Result<()> {
|
||||
// todo!()
|
||||
// }
|
||||
pub fn update_item(&mut self, item: T, index: i32) -> Result<()> {
|
||||
if let Some(current_item) = self.items.get_mut(index as usize)
|
||||
{
|
||||
let _old_item = replace(current_item, item);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(eyre!(
|
||||
"Item doesn't exist in model. Id was {}",
|
||||
index
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
// fn update_item(&mut self, item: Self::Item, index: i32) -> Result<()> {
|
||||
// todo!()
|
||||
// }
|
||||
pub fn remove_item(&mut self, index: i32) -> Result<()> {
|
||||
self.items.remove(index as usize);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// fn remove_item(&mut self, index: i32) -> Result<()> {
|
||||
// todo!()
|
||||
// }
|
||||
pub fn get_item(&self, index: i32) -> Option<&T> {
|
||||
self.items.get(index as usize)
|
||||
}
|
||||
|
||||
// fn get_item(&self, index: i32) -> Option<&Self::Item> {
|
||||
// todo!()
|
||||
// }
|
||||
|
||||
// fn insert_item(&mut self, item: Self::Item, index: i32) -> Result<()> {
|
||||
// todo!()
|
||||
// }
|
||||
// }
|
||||
pub fn insert_item(&mut self, item: T, index: i32) -> Result<()> {
|
||||
self.items.insert(index as usize, item);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Model<T> {
|
||||
fn default() -> Self {
|
||||
|
@ -72,21 +83,6 @@ pub trait Modeling {
|
|||
.expect("problems")
|
||||
})
|
||||
}
|
||||
|
||||
fn add_item(&mut self, item: Self::Item) -> Result<()>;
|
||||
fn add_to_db(&mut self, item: Self::Item) -> Result<()>;
|
||||
fn update_item(
|
||||
&mut self,
|
||||
item: Self::Item,
|
||||
index: i32,
|
||||
) -> Result<()>;
|
||||
fn remove_item(&mut self, index: i32) -> Result<()>;
|
||||
fn get_item(&self, index: i32) -> Option<&Self::Item>;
|
||||
fn insert_item(
|
||||
&mut self,
|
||||
item: Self::Item,
|
||||
index: i32,
|
||||
) -> Result<()>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue