Simplify some utility functions

This commit is contained in:
2021-10-18 14:37:29 +02:00
parent 3a35343a34
commit c9a59f4731
2 changed files with 20 additions and 26 deletions

View File

@@ -38,21 +38,16 @@ use sqlx::{
use url::Url;
use uuid::Uuid;
fn into_user_limit(
#[cfg(feature = "sqlite")]
row: &SqliteRow,
index: &str
) -> SqlxResult<Vec<String>> {
let row_value = row.try_get(index)?;
from_sql_formatted_array(row_value, index).map(|i| i.collect())
}
#[inline]
fn into_server_limit(
fn into_limit<T>(
#[cfg(feature = "sqlite")]
row: &SqliteRow,
index: &str
) -> SqlxResult<Vec<Url>> {
) -> SqlxResult<Vec<T>>
where
T: FromStr,
<T as FromStr>::Err: Into<Box<dyn std::error::Error + Send + Sync>>,
{
Ok(from_sql_formatted_array(row.try_get(index)?, index)?.collect())
}
@@ -67,7 +62,6 @@ where
}
}
#[inline]
fn from_sql_formatted_array<T>(raw: String, index: &str) -> SqlxResult<impl Iterator<Item = T>>
where
T: FromStr,
@@ -263,15 +257,15 @@ impl PrivacyPreferences {
) -> SqlxResult<Self> {
Ok(PrivacyPreferences {
discovery: row.try_get("discovery")?,
discovery_user_limit: into_user_limit(row, "discovery_user_limit")?,
discovery_server_limit: into_server_limit(row, "discovery_server_limit")?,
discovery_user_limit: into_limit(row, "discovery_user_limit")?,
discovery_server_limit: into_limit(row, "discovery_server_limit")?,
last_seen: row.try_get("last_seen")?,
last_seen_user_limit: into_user_limit(row, "last_seen_user_limit")?,
last_seen_server_limit: into_server_limit(row, "last_seen_server_limit")?,
last_seen_user_limit: into_limit(row, "last_seen_user_limit")?,
last_seen_server_limit: into_limit(row, "last_seen_server_limit")?,
last_seen_course: row.try_get("last_seen_course")?,
info: row.try_get("info")?,
info_user_limit: into_user_limit(row, "info_user_limit")?,
info_server_limit: into_server_limit(row, "info_server_limit")?,
info_user_limit: into_limit(row, "info_user_limit")?,
info_server_limit: into_limit(row, "info_server_limit")?,
})
}
}
@@ -283,7 +277,7 @@ impl FromRow<'_, SqliteRow> for PrivacyPreferences {
}
}
#[derive(Clone, Copy, Debug, Default, GraphQLObject, Decode, Encode, FromRow)]
#[derive(Clone, Copy, Debug, Default, GraphQLObject, Type, FromRow)]
pub struct NotificationPreferences {
lock_details: bool,
do_not_disturb: bool,
@@ -403,7 +397,7 @@ impl Query {
security_preferences,
external_servers_preferences: ExternalServersPreferences {
external_servers: external_servers_preferences.try_get("external_servers")?,
external_servers_limit: into_server_limit(&external_servers_preferences, "external_servers_limit")?,
external_servers_limit: into_limit(&external_servers_preferences, "external_servers_limit")?,
privacy_preferences: external_servers_privacy_preferences,
},
})