Make Sensitive generic over the inner type

asonix/better-errors
Aode (lion) 2021-11-30 00:04:55 -06:00
parent 5f986ec969
commit c4b2f45dec
8 changed files with 110 additions and 104 deletions

View File

@ -9,13 +9,13 @@ pub struct CreateComment {
pub post_id: PostId,
pub parent_id: Option<CommentId>,
pub form_id: Option<String>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetComment {
pub id: CommentId,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -23,14 +23,14 @@ pub struct EditComment {
pub content: String,
pub comment_id: CommentId,
pub form_id: Option<String>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteComment {
pub comment_id: CommentId,
pub deleted: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -38,21 +38,21 @@ pub struct RemoveComment {
pub comment_id: CommentId,
pub removed: bool,
pub reason: Option<String>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MarkCommentAsRead {
pub comment_id: CommentId,
pub read: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SaveComment {
pub comment_id: CommentId,
pub save: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -66,7 +66,7 @@ pub struct CommentResponse {
pub struct CreateCommentLike {
pub comment_id: CommentId,
pub score: i16,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -78,7 +78,7 @@ pub struct GetComments {
pub community_id: Option<CommunityId>,
pub community_name: Option<String>,
pub saved_only: Option<bool>,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -90,7 +90,7 @@ pub struct GetCommentsResponse {
pub struct CreateCommentReport {
pub comment_id: CommentId,
pub reason: String,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -102,7 +102,7 @@ pub struct CommentReportResponse {
pub struct ResolveCommentReport {
pub report_id: CommentReportId,
pub resolved: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -113,7 +113,7 @@ pub struct ListCommentReports {
pub unresolved_only: Option<bool>,
/// if no community is given, it returns reports for all communities moderated by the auth user
pub community_id: Option<CommunityId>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]

View File

@ -12,7 +12,7 @@ pub struct GetCommunity {
pub id: Option<CommunityId>,
/// Example: star_trek , or star_trek@xyz.tld
pub name: Option<String>,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -30,7 +30,7 @@ pub struct CreateCommunity {
pub icon: Option<String>,
pub banner: Option<String>,
pub nsfw: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -44,7 +44,7 @@ pub struct ListCommunities {
pub sort: Option<String>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
@ -60,7 +60,7 @@ pub struct BanFromCommunity {
pub remove_data: Option<bool>,
pub reason: Option<String>,
pub expires: Option<i64>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -74,7 +74,7 @@ pub struct AddModToCommunity {
pub community_id: CommunityId,
pub person_id: PersonId,
pub added: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -90,14 +90,14 @@ pub struct EditCommunity {
pub icon: Option<String>,
pub banner: Option<String>,
pub nsfw: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteCommunity {
pub community_id: CommunityId,
pub deleted: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -106,21 +106,21 @@ pub struct RemoveCommunity {
pub removed: bool,
pub reason: Option<String>,
pub expires: Option<i64>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct FollowCommunity {
pub community_id: CommunityId,
pub follow: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockCommunity {
pub community_id: CommunityId,
pub block: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -133,5 +133,5 @@ pub struct BlockCommunityResponse {
pub struct TransferCommunity {
pub community_id: CommunityId,
pub person_id: PersonId,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}

View File

@ -145,7 +145,7 @@ pub fn check_validator_time(
}
pub async fn get_local_user_view_from_jwt_opt(
jwt: Option<&Sensitive>,
jwt: Option<&Sensitive<String>>,
pool: &DbPool,
secret: &Secret,
) -> Result<Option<LocalUserView>, LemmyError> {
@ -156,7 +156,7 @@ pub async fn get_local_user_view_from_jwt_opt(
}
pub async fn get_local_user_settings_view_from_jwt(
jwt: &Sensitive,
jwt: &Sensitive<String>,
pool: &DbPool,
secret: &Secret,
) -> Result<LocalUserSettingsView, LemmyError> {
@ -180,7 +180,7 @@ pub async fn get_local_user_settings_view_from_jwt(
}
pub async fn get_local_user_settings_view_from_jwt_opt(
jwt: Option<&Sensitive>,
jwt: Option<&Sensitive<String>>,
pool: &DbPool,
secret: &Secret,
) -> Result<Option<LocalUserSettingsView>, LemmyError> {

View File

@ -13,18 +13,18 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Login {
pub username_or_email: Sensitive,
pub password: Sensitive,
pub username_or_email: Sensitive<String>,
pub password: Sensitive<String>,
}
use lemmy_db_schema::newtypes::{CommunityId, PersonId, PersonMentionId, PrivateMessageId};
#[derive(Debug, Serialize, Deserialize)]
pub struct Register {
pub username: String,
pub password: Sensitive,
pub password_verify: Sensitive,
pub password: Sensitive<String>,
pub password_verify: Sensitive<String>,
pub show_nsfw: bool,
pub email: Option<Sensitive>,
pub email: Option<Sensitive<String>>,
pub captcha_uuid: Option<String>,
pub captcha_answer: Option<String>,
pub honeypot: Option<String>,
@ -56,7 +56,7 @@ pub struct SaveUserSettings {
pub avatar: Option<String>,
pub banner: Option<String>,
pub display_name: Option<String>,
pub email: Option<Sensitive>,
pub email: Option<Sensitive<String>>,
pub bio: Option<String>,
pub matrix_user_id: Option<String>,
pub show_avatars: Option<bool>,
@ -65,20 +65,20 @@ pub struct SaveUserSettings {
pub show_bot_accounts: Option<bool>,
pub show_read_posts: Option<bool>,
pub show_new_post_notifs: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ChangePassword {
pub new_password: Sensitive,
pub new_password_verify: Sensitive,
pub old_password: Sensitive,
pub auth: Sensitive,
pub new_password: Sensitive<String>,
pub new_password_verify: Sensitive<String>,
pub old_password: Sensitive<String>,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LoginResponse {
pub jwt: Sensitive,
pub jwt: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -91,7 +91,7 @@ pub struct GetPersonDetails {
pub limit: Option<i64>,
pub community_id: Option<CommunityId>,
pub saved_only: Option<bool>,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -114,14 +114,14 @@ pub struct GetPersonMentionsResponse {
#[derive(Debug, Serialize, Deserialize)]
pub struct MarkAllAsRead {
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AddAdmin {
pub person_id: PersonId,
pub added: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -136,7 +136,7 @@ pub struct BanPerson {
pub remove_data: Option<bool>,
pub reason: Option<String>,
pub expires: Option<i64>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -149,7 +149,7 @@ pub struct BanPersonResponse {
pub struct BlockPerson {
pub person_id: PersonId,
pub block: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -164,7 +164,7 @@ pub struct GetReplies {
pub page: Option<i64>,
pub limit: Option<i64>,
pub unread_only: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -173,14 +173,14 @@ pub struct GetPersonMentions {
pub page: Option<i64>,
pub limit: Option<i64>,
pub unread_only: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MarkPersonMentionAsRead {
pub person_mention_id: PersonMentionId,
pub read: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -190,13 +190,13 @@ pub struct PersonMentionResponse {
#[derive(Debug, Serialize, Deserialize)]
pub struct DeleteAccount {
pub password: Sensitive,
pub auth: Sensitive,
pub password: Sensitive<String>,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PasswordReset {
pub email: Sensitive,
pub email: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -204,37 +204,37 @@ pub struct PasswordResetResponse {}
#[derive(Debug, Serialize, Deserialize)]
pub struct PasswordChange {
pub token: Sensitive,
pub password: Sensitive,
pub password_verify: Sensitive,
pub token: Sensitive<String>,
pub password: Sensitive<String>,
pub password_verify: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CreatePrivateMessage {
pub content: String,
pub recipient_id: PersonId,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EditPrivateMessage {
pub private_message_id: PrivateMessageId,
pub content: String,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DeletePrivateMessage {
pub private_message_id: PrivateMessageId,
pub deleted: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MarkPrivateMessageAsRead {
pub private_message_id: PrivateMessageId,
pub read: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -242,7 +242,7 @@ pub struct GetPrivateMessages {
pub unread_only: Option<bool>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -258,7 +258,7 @@ pub struct PrivateMessageResponse {
#[derive(Debug, Serialize, Deserialize)]
pub struct GetReportCount {
pub community_id: Option<CommunityId>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -270,7 +270,7 @@ pub struct GetReportCountResponse {
#[derive(Debug, Serialize, Deserialize)]
pub struct GetUnreadCount {
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]

View File

@ -20,7 +20,7 @@ pub struct CreatePost {
pub body: Option<String>,
pub honeypot: Option<String>,
pub nsfw: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -31,7 +31,7 @@ pub struct PostResponse {
#[derive(Debug, Serialize, Deserialize)]
pub struct GetPost {
pub id: PostId,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -52,7 +52,7 @@ pub struct GetPosts {
pub community_id: Option<CommunityId>,
pub community_name: Option<String>,
pub saved_only: Option<bool>,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
@ -64,7 +64,7 @@ pub struct GetPostsResponse {
pub struct CreatePostLike {
pub post_id: PostId,
pub score: i16,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -74,14 +74,14 @@ pub struct EditPost {
pub url: Option<Url>,
pub body: Option<String>,
pub nsfw: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct DeletePost {
pub post_id: PostId,
pub deleted: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -89,42 +89,42 @@ pub struct RemovePost {
pub post_id: PostId,
pub removed: bool,
pub reason: Option<String>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MarkPostAsRead {
pub post_id: PostId,
pub read: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct LockPost {
pub post_id: PostId,
pub locked: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct StickyPost {
pub post_id: PostId,
pub stickied: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct SavePost {
pub post_id: PostId,
pub save: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CreatePostReport {
pub post_id: PostId,
pub reason: String,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -136,7 +136,7 @@ pub struct PostReportResponse {
pub struct ResolvePostReport {
pub report_id: PostReportId,
pub resolved: bool,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -147,7 +147,7 @@ pub struct ListPostReports {
pub unresolved_only: Option<bool>,
/// if no community is given, it returns reports for all communities moderated by the auth user
pub community_id: Option<CommunityId>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]

View File

@ -39,7 +39,7 @@ pub struct Search {
pub listing_type: Option<String>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
@ -54,7 +54,7 @@ pub struct SearchResponse {
#[derive(Serialize, Deserialize, Debug)]
pub struct ResolveObject {
pub q: String,
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Debug, Serialize, Deserialize, Default)]
@ -98,7 +98,7 @@ pub struct CreateSite {
pub open_registration: Option<bool>,
pub enable_nsfw: Option<bool>,
pub community_creation_admin_only: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -112,12 +112,12 @@ pub struct EditSite {
pub open_registration: Option<bool>,
pub enable_nsfw: Option<bool>,
pub community_creation_admin_only: Option<bool>,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetSite {
pub auth: Option<Sensitive>,
pub auth: Option<Sensitive<String>>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
@ -148,12 +148,12 @@ pub struct MyUserInfo {
#[derive(Debug, Serialize, Deserialize)]
pub struct TransferSite {
pub person_id: PersonId,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GetSiteConfig {
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@ -164,7 +164,7 @@ pub struct GetSiteConfigResponse {
#[derive(Debug, Serialize, Deserialize)]
pub struct SaveSiteConfig {
pub config_hjson: String,
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize)]

View File

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct UserJoin {
pub auth: Sensitive,
pub auth: Sensitive<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]

View File

@ -5,55 +5,61 @@ use std::{
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Deserialize, serde::Serialize)]
#[serde(transparent)]
pub struct Sensitive(String);
pub struct Sensitive<T>(T);
impl Sensitive {
pub fn new(string: String) -> Self {
Sensitive(string)
impl<T> Sensitive<T> {
pub fn new(item: T) -> Self {
Sensitive(item)
}
pub fn into_inner(this: Self) -> String {
pub fn into_inner(this: Self) -> T {
this.0
}
}
impl std::fmt::Debug for Sensitive {
impl<T> std::fmt::Debug for Sensitive<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sensitive").finish()
}
}
impl AsRef<String> for Sensitive {
fn as_ref(&self) -> &String {
impl<T> AsRef<T> for Sensitive<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl AsRef<str> for Sensitive {
impl AsRef<str> for Sensitive<String> {
fn as_ref(&self) -> &str {
&self.0
}
}
impl AsRef<[u8]> for Sensitive {
impl AsRef<[u8]> for Sensitive<String> {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl AsMut<String> for Sensitive {
fn as_mut(&mut self) -> &mut String {
impl AsRef<[u8]> for Sensitive<Vec<u8>> {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl<T> AsMut<T> for Sensitive<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl AsMut<str> for Sensitive {
impl AsMut<str> for Sensitive<String> {
fn as_mut(&mut self) -> &mut str {
&mut self.0
}
}
impl Deref for Sensitive {
impl Deref for Sensitive<String> {
type Target = str;
fn deref(&self) -> &Self::Target {
@ -61,31 +67,31 @@ impl Deref for Sensitive {
}
}
impl DerefMut for Sensitive {
impl DerefMut for Sensitive<String> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<String> for Sensitive {
fn from(s: String) -> Self {
Sensitive(s)
impl<T> From<T> for Sensitive<T> {
fn from(t: T) -> Self {
Sensitive(t)
}
}
impl From<&str> for Sensitive {
impl From<&str> for Sensitive<String> {
fn from(s: &str) -> Self {
Sensitive(s.into())
}
}
impl Borrow<String> for Sensitive {
fn borrow(&self) -> &String {
impl<T> Borrow<T> for Sensitive<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl Borrow<str> for Sensitive {
impl Borrow<str> for Sensitive<String> {
fn borrow(&self) -> &str {
&self.0
}