Skip to content

Commit

Permalink
refactor: form_raw_parts から unsafe を外す
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Nov 1, 2024
1 parent fc64820 commit cfea0d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
32 changes: 12 additions & 20 deletions server/domain/src/form/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use types::Resolver;

use crate::{
repository::form_repository::FormRepository,
types::authorization_guard::{AuthorizationGuard, AuthorizationGuardDefinitions, Create, Read},
types::authorization_guard::{AuthorizationGuard, AuthorizationGuardDefinitions, Create},
user::models::{Role::Administrator, User},
};

Expand Down Expand Up @@ -551,36 +551,28 @@ impl Message {
/// title: Default::default(),
/// };
///
/// let message = unsafe {
/// Message::from_raw_parts(
/// MessageId::new(),
/// related_answer,
/// user,
/// "test message".to_string(),
/// Utc::now(),
/// )
/// };
/// let message = Message::from_raw_parts(
/// MessageId::new(),
/// related_answer,
/// user,
/// "test message".to_string(),
/// Utc::now(),
/// );
/// ```
///
/// # Safety
/// この関数は [`Message`] のバリデーションをスキップするため、
/// データベースからすでにバリデーションされているデータを読み出すときなど、
/// データの信頼性が保証されている場合にのみ使用してください。
pub unsafe fn from_raw_parts(
pub fn from_raw_parts(
id: MessageId,
related_answer: FormAnswer,
sender: User,
body: String,
timestamp: DateTime<Utc>,
) -> AuthorizationGuard<Self, Read> {
AuthorizationGuard::new(Self {
) -> Self {
Self {
id,
related_answer,
sender,
body,
timestamp,
})
.into_read()
}
}
}

Expand Down
19 changes: 8 additions & 11 deletions server/infra/resource/src/dto.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use chrono::{DateTime, Utc};
use domain::{
form::models::{FormSettings, ResponsePeriod},
types::authorization_guard::{AuthorizationGuard, Read},
user::models::{Role, User},
};
use uuid::Uuid;
Expand Down Expand Up @@ -290,7 +289,7 @@ pub struct MessageDto {
pub timestamp: DateTime<Utc>,
}

impl TryFrom<MessageDto> for AuthorizationGuard<domain::form::models::Message, Read> {
impl TryFrom<MessageDto> for domain::form::models::Message {
type Error = errors::domain::DomainError;

fn try_from(
Expand All @@ -302,14 +301,12 @@ impl TryFrom<MessageDto> for AuthorizationGuard<domain::form::models::Message, R
timestamp,
}: MessageDto,
) -> Result<Self, Self::Error> {
Ok(unsafe {
domain::form::models::Message::from_raw_parts(
id.into(),
related_answer.try_into()?,
sender.try_into()?,
body,
timestamp,
)
})
Ok(domain::form::models::Message::from_raw_parts(
id.into(),
related_answer.try_into()?,
sender.try_into()?,
body,
timestamp,
))
}
}
17 changes: 15 additions & 2 deletions server/infra/resource/src/repository/form_repository_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,15 @@ impl<Client: DatabaseComponents + 'static> FormRepository for Repository<Client>
.fetch_messages_answer(answers)
.await?
.into_iter()
.map(|dto| Ok(dto.try_into()?))
.map(|dto| {
Ok::<Message, DomainError>(dto.try_into()?).map(|message| {
let message: AuthorizationGuard<Message, Create> = message.into();

message.into_read()
})
})
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
}

async fn update_message_body(
Expand All @@ -471,7 +478,13 @@ impl<Client: DatabaseComponents + 'static> FormRepository for Repository<Client>
.form()
.fetch_message(message_id)
.await?
.map(|dto| Ok::<_, DomainError>(dto.try_into())?)
.map(|dto| {
Ok::<Message, DomainError>(dto.try_into()?).map(|message| {
let message: AuthorizationGuard<Message, Create> = message.into();

message.into_read()
})
})
.transpose()
.map_err(Into::into)
}
Expand Down

0 comments on commit cfea0d8

Please sign in to comment.