Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
zecakeh committed Oct 10, 2022
1 parent 5d6821f commit 58b7be5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
29 changes: 17 additions & 12 deletions crates/ruma-common/src/events/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use std::{
use ruma_macros::{EventContent, OrdAsRefStr, PartialEqAsRefStr, PartialOrdAsRefStr, StringEnum};
use serde::{Deserialize, Serialize};

use crate::{EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, PrivOwnedStr, UserId};
use crate::{
EventId, IdParseError, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, PrivOwnedStr,
UserId,
};

/// The content of an `m.receipt` event.
///
Expand Down Expand Up @@ -106,7 +109,7 @@ pub struct Receipt {
pub ts: Option<MilliSecondsSinceUnixEpoch>,

/// The thread this receipt applies to.
#[serde(rename = "thread_id", skip_serializing_if = "crate::serde::is_default")]
#[serde(rename = "thread_id", default, skip_serializing_if = "crate::serde::is_default")]
pub thread: ReceiptThread,
}

Expand All @@ -122,7 +125,7 @@ impl Receipt {
/// The [thread a receipt applies to].
///
/// This type can hold an arbitrary string. To build this with a custom value, convert it from an
/// `Option<string>` with `::from()` / `.into()`. [`ReceiptThread::Unthreaded`] can be constructed
/// `Option<String>` with `::from()` / `.into()`. [`ReceiptThread::Unthreaded`] can be constructed
/// from `None`.
///
/// To check for values that are not available as a documented variant here, use its string
Expand Down Expand Up @@ -168,21 +171,23 @@ impl ReceiptThread {
}
}

impl<T> From<Option<T>> for ReceiptThread
impl<T> TryFrom<Option<T>> for ReceiptThread
where
T: AsRef<str> + Into<Box<str>>,
{
fn from(s: Option<T>) -> Self {
match s {
type Error = IdParseError;

fn try_from(s: Option<T>) -> Result<Self, Self::Error> {
let res = match s {
None => Self::Unthreaded,
Some(s) => match s.as_ref() {
"main" => Self::Main,
s_ref => match EventId::parse(s_ref) {
Ok(event_id) => Self::Thread(event_id),
Err(_) => Self::_Custom(PrivOwnedStr(s.into())),
},
s_ref if s_ref.starts_with('s') => Self::Thread(EventId::parse(s_ref)?),
_ => Self::_Custom(PrivOwnedStr(s.into())),
},
}
};

Ok(res)
}
}

Expand All @@ -209,7 +214,7 @@ mod tests {
Receipt::new(MilliSecondsSinceUnixEpoch(1_664_702_144_365_u64.try_into().unwrap()));
assert_eq!(to_json_value(receipt.clone()).unwrap(), json!({ "ts": 1_664_702_144_365_u64 }));

receipt.thread = ReceiptThread::from(Some("io.ruma.unknown"));
receipt.thread = ReceiptThread::try_from(Some("io.ruma.unknown")).unwrap();
assert_eq!(
to_json_value(receipt).unwrap(),
json!({ "ts": 1_664_702_144_365_u64, "thread_id": "io.ruma.unknown" })
Expand Down
6 changes: 2 additions & 4 deletions crates/ruma-common/src/events/receipt/receipt_thread_serde.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::borrow::Cow;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

use super::ReceiptThread;
Expand All @@ -18,7 +16,7 @@ impl<'de> Deserialize<'de> for ReceiptThread {
where
D: Deserializer<'de>,
{
let s = Option::<Cow<'_, str>>::deserialize(deserializer)?;
Ok(s.into())
let s = crate::serde::deserialize_cow_str(deserializer)?;
Self::try_from(Some(s)).map_err(serde::de::Error::custom)
}
}

0 comments on commit 58b7be5

Please sign in to comment.