From 27b95c9186bd45edd4c982a7cc89cf69cf2c5d46 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Wed, 16 Oct 2024 12:51:21 +0100 Subject: All backend stuff lives in backend folder --- backend/src/utils/find_by_id_or.rs | 53 -------------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 backend/src/utils/find_by_id_or.rs (limited to 'backend/src/utils/find_by_id_or.rs') diff --git a/backend/src/utils/find_by_id_or.rs b/backend/src/utils/find_by_id_or.rs deleted file mode 100644 index f5add30..0000000 --- a/backend/src/utils/find_by_id_or.rs +++ /dev/null @@ -1,53 +0,0 @@ -pub fn find_by_id_or( - arr: &[(Id, Option)], - id: Id, - default: Val, -) -> Val { - arr.iter() - .find(|(elem_id, _)| *elem_id == id) - .map_or(default, |row| match row { - (_, Some(quantity)) => *quantity, - (_, _) => default, - }) -} - -#[cfg(test)] -mod test { - use super::find_by_id_or; - - #[test] - fn when_no_elements_then_return_default() { - let arr = []; - - let result = find_by_id_or(&arr, 1, 42); - - assert_eq!(result, 42); - } - - #[test] - fn when_id_not_in_elements_then_return_default() { - let arr = [(0, Some(100)), (2, Some(200))]; - - let result = find_by_id_or(&arr, 1, 42); - - assert_eq!(result, 42); - } - - #[test] - fn when_value_is_none_then_return_default() { - let arr = [(1, None)]; - - let result = find_by_id_or(&arr, 1, 42); - - assert_eq!(result, 42); - } - - #[test] - fn when_value_is_some_then_return_value() { - let arr = [(1, Some(67))]; - - let result = find_by_id_or(&arr, 1, 42); - - assert_eq!(result, 67); - } -} -- cgit v1.2.3