|
|
@ -47,7 +47,9 @@ pub struct ImageDetails { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The type of media attachment.
|
|
|
|
/// The type of media attachment.
|
|
|
|
#[derive(Debug, Deserialize, Clone, Copy, PartialEq)] |
|
|
|
#[derive(Debug, Deserialize, Clone, PartialEq)] |
|
|
|
|
|
|
|
// Deserialize "Other" trick from https://github.com/serde-rs/serde/issues/912#issuecomment-803097289
|
|
|
|
|
|
|
|
#[serde(remote = "MediaType")] |
|
|
|
pub enum MediaType { |
|
|
|
pub enum MediaType { |
|
|
|
/// An image.
|
|
|
|
/// An image.
|
|
|
|
#[serde(rename = "image")] |
|
|
|
#[serde(rename = "image")] |
|
|
@ -55,10 +57,52 @@ pub enum MediaType { |
|
|
|
/// A video file.
|
|
|
|
/// A video file.
|
|
|
|
#[serde(rename = "video")] |
|
|
|
#[serde(rename = "video")] |
|
|
|
Video, |
|
|
|
Video, |
|
|
|
|
|
|
|
/// An audio file.
|
|
|
|
|
|
|
|
#[serde(rename = "audio")] |
|
|
|
|
|
|
|
Audio, |
|
|
|
/// A gifv format file.
|
|
|
|
/// A gifv format file.
|
|
|
|
#[serde(rename = "gifv")] |
|
|
|
#[serde(rename = "gifv")] |
|
|
|
Gifv, |
|
|
|
Gifv, |
|
|
|
/// Unknown format.
|
|
|
|
/// Unknown format.
|
|
|
|
#[serde(rename = "unknown")] |
|
|
|
#[serde(rename = "unknown")] |
|
|
|
Unknown, |
|
|
|
Unknown, |
|
|
|
|
|
|
|
/// Anything else
|
|
|
|
|
|
|
|
#[serde(skip_deserializing)] |
|
|
|
|
|
|
|
Other(String), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::str::FromStr; |
|
|
|
|
|
|
|
use serde::de::{value, Deserializer, IntoDeserializer}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl FromStr for MediaType { |
|
|
|
|
|
|
|
type Err = value::Error; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> { |
|
|
|
|
|
|
|
Self::deserialize(s.into_deserializer()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for MediaType { |
|
|
|
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
|
|
|
|
|
|
|
where D: Deserializer<'de> |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
let s = String::deserialize(deserializer)?; |
|
|
|
|
|
|
|
let deserialized = Self::from_str(&s).unwrap_or_else(|_| { |
|
|
|
|
|
|
|
Self::Other(s) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
Ok(deserialized) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
|
|
|
|
mod tests { |
|
|
|
|
|
|
|
use crate::entities::prelude::MediaType; |
|
|
|
|
|
|
|
use serde_json::Value; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
|
|
|
fn test_deserialize_media_type() { |
|
|
|
|
|
|
|
assert_eq!(MediaType::Image, serde_json::from_value(Value::String("image".to_owned())).unwrap()); |
|
|
|
|
|
|
|
assert_eq!(MediaType::Audio, serde_json::from_value(Value::String("audio".to_owned())).unwrap()); |
|
|
|
|
|
|
|
assert_eq!(MediaType::Other("pleroma_weird_thing".to_string()), serde_json::from_value(Value::String("pleroma_weird_thing".to_owned())).unwrap()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|