flix_db/entity/
mod.rs

1//! Entity structs for interacting with the database
2
3pub mod content;
4pub mod info;
5#[cfg(feature = "tmdb")]
6pub mod tmdb;
7pub mod watched;
8
9#[cfg(test)]
10mod tests {
11	pub use sea_orm::DbErr;
12	use sea_orm::sqlx::error::ErrorKind;
13
14	#[derive(Debug)]
15	pub enum ErrorKindError {
16		NotRuntimeError,
17		NotSqlxError,
18		NotDatabaseError,
19	}
20
21	pub fn get_error_kind(error: DbErr) -> Result<ErrorKind, ErrorKindError> {
22		let runtime_err = match error {
23			DbErr::Conn(runtime_err) => runtime_err,
24			DbErr::Exec(runtime_err) => runtime_err,
25			DbErr::Query(runtime_err) => runtime_err,
26			_ => return Err(ErrorKindError::NotRuntimeError),
27		};
28
29		let sqlx_err = match runtime_err {
30			sea_orm::RuntimeErr::SqlxError(sqlx_err) => sqlx_err,
31			_ => return Err(ErrorKindError::NotSqlxError),
32		};
33
34		let database_err = match sqlx_err.as_ref() {
35			sea_orm::SqlxError::Database(database_err) => database_err,
36			_ => return Err(ErrorKindError::NotDatabaseError),
37		};
38
39		Ok(database_err.kind())
40	}
41
42	/// Helper macro for writing tests for `ActiveModel` structs where
43	/// toggling [sea_orm::ActiveValue] is needed
44	macro_rules! notsettable {
45		($field:ident, $value:expr $(, $($skip:ident),+)?) => {
46			if notsettable!(@skip, $field $(, $($skip),+)?) {
47				NotSet
48			} else {
49				Set($value)
50			}
51		};
52		(@skip, $field:ident $(, $skip:ident),*) => {
53			false $(|| stringify!($field) == stringify!($skip))*
54		};
55	}
56	pub(super) use notsettable;
57
58	/// Helper macro for writing tests for `ActiveModel` structs where
59	/// toggling [sea_orm::ActiveValue] is needed
60	macro_rules! noneable {
61			($field:ident, $value:expr $(, $($skip:ident),+)?) => {
62				if noneable!(@skip, $field $(, $($skip),+)?) {
63					None
64				} else {
65					Some($value)
66				}
67			};
68			(@skip, $field:ident $(, $skip:ident),*) => {
69				false $(|| stringify!($field) == stringify!($skip))*
70			};
71		}
72	pub(super) use noneable;
73}