From 12c632d2ddde4ff1f3f8738e3d00b2281cd28e13 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 21 Apr 2021 22:43:17 +0200 Subject: [PATCH] try to use separate struct for primary language --- crates/db_schema/src/lib.rs | 75 +++---------------- crates/db_schema/src/source/comment.rs | 6 +- crates/db_schema/src/source/local_user.rs | 6 +- crates/db_schema/src/source/post.rs | 6 +- .../db_schema/src/source/private_message.rs | 6 +- 5 files changed, 23 insertions(+), 76 deletions(-) diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index 2bbaea516..38b885c1c 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -11,9 +11,10 @@ use diesel::{ serialize::{Output, ToSql}, sql_types::Text, }; -use language_tags::LanguageTag; -use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use language_tags::{LanguageTag, ValidationError}; +use serde::{Deserialize, Serialize}; use std::{ + convert::TryFrom, fmt, fmt::{Display, Formatter}, io::Write, @@ -122,69 +123,15 @@ pub fn naive_now() -> NaiveDateTime { } #[repr(transparent)] -#[derive(Clone, PartialEq, Debug, AsExpression, FromSqlRow)] +#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, Queryable)] #[sql_type = "Text"] -pub struct DbLanguage(LanguageTag); +pub struct PrimaryLanguageTag(String); -impl ToSql for DbLanguage -where - String: ToSql, -{ - fn to_sql(&self, out: &mut Output) -> diesel::serialize::Result { - self.0.to_string().to_sql(out) - } -} - -// TODO: hopefully the crate will add serde support -// https://github.com/pyfisch/rust-language-tags/issues/22 -impl Serialize for DbLanguage { - fn serialize(&self, _serializer: S) -> Result<::Ok, ::Error> - where - S: Serializer, - { - todo!() - } -} - -impl<'de> Deserialize<'de> for DbLanguage { - fn deserialize(_deserializer: D) -> Result>::Error> - where - D: Deserializer<'de>, - { - todo!() - } -} - -impl FromSql for DbLanguage -where - String: FromSql, -{ - fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result { - let str = String::from_sql(bytes)?; - Ok(DbLanguage(LanguageTag::parse(&str)?)) - } -} - -impl DbLanguage { - pub fn into_inner(self) -> LanguageTag { - self.0 - } -} - -impl Display for DbLanguage { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - self.to_owned().into_inner().fmt(f) - } -} - -impl From for LanguageTag { - fn from(url: DbLanguage) -> Self { - url.0 - } -} - -impl From for DbLanguage { - fn from(lang: LanguageTag) -> Self { - DbLanguage(lang) +impl TryFrom for PrimaryLanguageTag { + type Error = ValidationError; + + fn try_from(tag: LanguageTag) -> Result { + let canonical = tag.canonicalize()?; + Ok(PrimaryLanguageTag(canonical.primary_language().to_string())) } } diff --git a/crates/db_schema/src/source/comment.rs b/crates/db_schema/src/source/comment.rs index 71e074a7b..12543ce8b 100644 --- a/crates/db_schema/src/source/comment.rs +++ b/crates/db_schema/src/source/comment.rs @@ -2,10 +2,10 @@ use crate::{ schema::{comment, comment_alias_1, comment_like, comment_saved}, source::post::Post, CommentId, - DbLanguage, DbUrl, PersonId, PostId, + PrimaryLanguageTag, }; use serde::Serialize; @@ -32,7 +32,7 @@ pub struct Comment { pub deleted: bool, pub ap_id: DbUrl, pub local: bool, - pub language: DbLanguage, + pub language: PrimaryLanguageTag, } #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] @@ -67,7 +67,7 @@ pub struct CommentForm { pub deleted: Option, pub ap_id: Option, pub local: Option, - pub language: Option, + pub language: Option, } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] diff --git a/crates/db_schema/src/source/local_user.rs b/crates/db_schema/src/source/local_user.rs index 47eac1df6..61d013234 100644 --- a/crates/db_schema/src/source/local_user.rs +++ b/crates/db_schema/src/source/local_user.rs @@ -1,4 +1,4 @@ -use crate::{schema::local_user, DbLanguage, LocalUserId, PersonId}; +use crate::{schema::local_user, LocalUserId, PersonId, PrimaryLanguageTag}; use serde::Serialize; #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] @@ -17,7 +17,7 @@ pub struct LocalUser { pub send_notifications_to_email: bool, pub validator_time: chrono::NaiveDateTime, pub show_scores: bool, - pub discussion_languages: Vec, + pub discussion_languages: Vec, } // TODO redo these, check table defaults @@ -35,7 +35,7 @@ pub struct LocalUserForm { pub show_avatars: Option, pub send_notifications_to_email: Option, pub show_scores: Option, - pub discussion_languages: Option>, + pub discussion_languages: Option>, } /// A local user view that removes password encrypted diff --git a/crates/db_schema/src/source/post.rs b/crates/db_schema/src/source/post.rs index 1d3f64552..eeee7cbaa 100644 --- a/crates/db_schema/src/source/post.rs +++ b/crates/db_schema/src/source/post.rs @@ -1,10 +1,10 @@ use crate::{ schema::{post, post_like, post_read, post_saved}, CommunityId, - DbLanguage, DbUrl, PersonId, PostId, + PrimaryLanguageTag, }; use serde::Serialize; @@ -30,7 +30,7 @@ pub struct Post { pub thumbnail_url: Option, pub ap_id: DbUrl, pub local: bool, - pub language: DbLanguage, + pub language: PrimaryLanguageTag, } #[derive(Insertable, AsChangeset, Default)] @@ -54,7 +54,7 @@ pub struct PostForm { pub thumbnail_url: Option, pub ap_id: Option, pub local: Option, - pub language: Option, + pub language: Option, } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] diff --git a/crates/db_schema/src/source/private_message.rs b/crates/db_schema/src/source/private_message.rs index 9e52fdc38..b35d97438 100644 --- a/crates/db_schema/src/source/private_message.rs +++ b/crates/db_schema/src/source/private_message.rs @@ -1,4 +1,4 @@ -use crate::{schema::private_message, DbLanguage, DbUrl, PersonId, PrivateMessageId}; +use crate::{schema::private_message, DbUrl, PersonId, PrimaryLanguageTag, PrivateMessageId}; use serde::Serialize; #[derive(Clone, Queryable, Associations, Identifiable, PartialEq, Debug, Serialize)] @@ -14,7 +14,7 @@ pub struct PrivateMessage { pub updated: Option, pub ap_id: DbUrl, pub local: bool, - pub language: DbLanguage, + pub language: PrimaryLanguageTag, } #[derive(Insertable, AsChangeset, Default)] @@ -29,5 +29,5 @@ pub struct PrivateMessageForm { pub updated: Option, pub ap_id: Option, pub local: Option, - pub language: Option, + pub language: Option, }