moving to using a filter function for getting the item
This commit is contained in:
parent
f3c3e98e16
commit
0bfa2c6089
6 changed files with 16 additions and 18 deletions
|
@ -45,8 +45,8 @@ mod test {
|
||||||
pub fn test_db_and_model() {
|
pub fn test_db_and_model() {
|
||||||
let mut image_model: Model<Image> = Model::default();
|
let mut image_model: Model<Image> = Model::default();
|
||||||
image_model.load_from_db();
|
image_model.load_from_db();
|
||||||
if let Some(image) = image_model.get_item(3) {
|
if let Some(image) = image_model.get_item(|i| i.id == 3) {
|
||||||
let test_image = test_image("ncca4".into());
|
let test_image = test_image("nccq5".into());
|
||||||
assert_eq!(test_image.title, image.title);
|
assert_eq!(test_image.title, image.title);
|
||||||
} else {
|
} else {
|
||||||
assert!(false);
|
assert!(false);
|
||||||
|
@ -61,10 +61,10 @@ mod test {
|
||||||
let new_image = test_image("A newer image".into());
|
let new_image = test_image("A newer image".into());
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
assert_eq!(&image, image_model.get_item(0).unwrap());
|
assert_eq!(&image, image_model.get_item(|i| i.id == 0).unwrap());
|
||||||
assert_ne!(
|
assert_ne!(
|
||||||
&new_image,
|
&new_image,
|
||||||
image_model.get_item(0).unwrap()
|
image_model.get_item(|i| i.id == 0).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Err(e) => assert!(
|
Err(e) => assert!(
|
||||||
|
|
|
@ -6,4 +6,4 @@ pub mod service_items;
|
||||||
pub mod slides;
|
pub mod slides;
|
||||||
pub mod songs;
|
pub mod songs;
|
||||||
pub mod videos;
|
pub mod videos;
|
||||||
pub mod file;
|
// pub mod file;
|
||||||
|
|
|
@ -36,8 +36,11 @@ impl<T> Model<T> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_item(&self, index: i32) -> Option<&T> {
|
pub fn get_item<P>(&self, f: P) -> Option<&T>
|
||||||
self.items.get(index as usize)
|
where
|
||||||
|
P: FnMut(&&T) -> bool,
|
||||||
|
{
|
||||||
|
self.items.iter().filter(f).next()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_item(&mut self, item: T, index: i32) -> Result<()> {
|
pub fn insert_item(&mut self, item: T, index: i32) -> Result<()> {
|
||||||
|
|
|
@ -88,7 +88,7 @@ mod test {
|
||||||
let mut presentation_model: Model<Presentation> =
|
let mut presentation_model: Model<Presentation> =
|
||||||
Model::default();
|
Model::default();
|
||||||
presentation_model.load_from_db();
|
presentation_model.load_from_db();
|
||||||
if let Some(presentation) = presentation_model.get_item(10) {
|
if let Some(presentation) = presentation_model.get_item(|p| p.id == 10) {
|
||||||
let test_presentation = test_presentation();
|
let test_presentation = test_presentation();
|
||||||
assert_eq!(&test_presentation, presentation);
|
assert_eq!(&test_presentation, presentation);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -112,11 +112,6 @@ impl Model<Song> {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_item(&self, index: i32) -> Option<&Song> {
|
|
||||||
self.items.iter().find(|s| s.id == index)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Song {
|
impl Song {
|
||||||
|
@ -287,7 +282,7 @@ You saved my soul"
|
||||||
pub fn test_db_and_model() {
|
pub fn test_db_and_model() {
|
||||||
let mut song_model: Model<Song> = Model::default();
|
let mut song_model: Model<Song> = Model::default();
|
||||||
song_model.load_from_db();
|
song_model.load_from_db();
|
||||||
if let Some(song) = song_model.get_item(7) {
|
if let Some(song) = song_model.get_item(|s| s.id == 7) {
|
||||||
let test_song = test_song();
|
let test_song = test_song();
|
||||||
assert_eq!(&test_song, song);
|
assert_eq!(&test_song, song);
|
||||||
} else {
|
} else {
|
||||||
|
@ -315,7 +310,7 @@ You saved my soul"
|
||||||
match song_model.update_item(song, 2) {
|
match song_model.update_item(song, 2) {
|
||||||
Ok(()) => assert_eq!(
|
Ok(()) => assert_eq!(
|
||||||
&cloned_song,
|
&cloned_song,
|
||||||
song_model.get_item(2).unwrap()
|
song_model.get_item(|s| s.id == 7).unwrap()
|
||||||
),
|
),
|
||||||
Err(e) => assert!(false, "{e}"),
|
Err(e) => assert!(false, "{e}"),
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ mod test {
|
||||||
pub fn test_db_and_model() {
|
pub fn test_db_and_model() {
|
||||||
let mut video_model: Model<Video> = Model::default();
|
let mut video_model: Model<Video> = Model::default();
|
||||||
video_model.load_from_db();
|
video_model.load_from_db();
|
||||||
if let Some(video) = video_model.get_item(3) {
|
if let Some(video) = video_model.get_item(|v| v.id == 3) {
|
||||||
let test_video = test_video("Getting started with Tokio. The ultimate starter guide to writing async Rust.".into());
|
let test_video = test_video("Getting started with Tokio. The ultimate starter guide to writing async Rust.".into());
|
||||||
assert_eq!(test_video.title, video.title);
|
assert_eq!(test_video.title, video.title);
|
||||||
} else {
|
} else {
|
||||||
|
@ -64,10 +64,10 @@ mod test {
|
||||||
let new_video = test_video("A newer video".into());
|
let new_video = test_video("A newer video".into());
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
assert_eq!(&video, video_model.get_item(0).unwrap());
|
assert_eq!(&video, video_model.get_item(|v| v.id == 0).unwrap());
|
||||||
assert_ne!(
|
assert_ne!(
|
||||||
&new_video,
|
&new_video,
|
||||||
video_model.get_item(0).unwrap()
|
video_model.get_item(|v| v.id == 0).unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Err(e) => assert!(
|
Err(e) => assert!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue