Skip to content

Commit

Permalink
Fix make optional some profile fields (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
SHAcollision authored Aug 22, 2024
1 parent 9c96ee5 commit 04d2562
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/models/user/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ pub struct UserLink {
url: String,
}

impl UserLink {
pub fn new() -> Self {
Self {
title: String::new(),
url: String::new(),
}
}
}

#[async_trait]
impl RedisOps for UserDetails {
async fn prefix() -> String {
Expand Down Expand Up @@ -54,20 +45,21 @@ impl Collection for UserDetails {
#[derive(Serialize, Deserialize, ToSchema, Default, Clone, Debug)]
pub struct UserDetails {
pub name: String,
bio: String,
pub bio: Option<String>,
pub id: String,
#[serde(deserialize_with = "deserialize_user_links")]
links: Vec<UserLink>,
status: String,
pub links: Option<Vec<UserLink>>,
pub status: Option<String>,
pub image: Option<String>,
pub indexed_at: i64,
}

fn deserialize_user_links<'de, D>(deserializer: D) -> Result<Vec<UserLink>, D::Error>
fn deserialize_user_links<'de, D>(deserializer: D) -> Result<Option<Vec<UserLink>>, D::Error>
where
D: Deserializer<'de>,
{
let s: String = String::deserialize(deserializer)?;
let urls: Vec<UserLink> = serde_json::from_str(&s).map_err(serde::de::Error::custom)?;
let urls: Option<Vec<UserLink>> = serde_json::from_str(&s).map_err(serde::de::Error::custom)?;
Ok(urls)
}

Expand Down Expand Up @@ -119,8 +111,26 @@ mod tests {
assert_eq!(user_details[0].as_ref().unwrap().name, "Aldert");
assert_eq!(user_details[5].as_ref().unwrap().name, "Flavio");

assert_eq!(user_details[5].as_ref().unwrap().links.len(), 4);
assert_eq!(user_details[0].as_ref().unwrap().links.len(), 2);
assert_eq!(
user_details[5]
.as_ref()
.unwrap()
.links
.as_ref()
.unwrap()
.len(),
4
);
assert_eq!(
user_details[0]
.as_ref()
.unwrap()
.links
.as_ref()
.unwrap()
.len(),
2
);

for (i, details) in user_details.iter().enumerate() {
if let Some(details) = details {
Expand Down

0 comments on commit 04d2562

Please sign in to comment.