rs_plugin_common_interfaces/url/
mod.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4use strum_macros::EnumString;
5
6#[cfg(feature = "rusqlite")]
7pub mod rusqlite;
8
9#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
10#[serde(rename_all = "snake_case")] 
11pub struct RsLink {
12	pub platform: String,
13    #[serde(rename = "type")]
14    pub kind: Option<RsLinkType>,
15    pub id: String,
16
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub file: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub user: Option<String>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub plugin: Option<String>,
23}
24
25#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, strum_macros::Display,EnumString, Default)]
26#[serde(rename_all = "camelCase")] 
27#[strum(serialize_all = "camelCase")]
28pub enum RsLinkType {
29	Post,
30    Profile,
31    Video,
32    Photo,
33    File,
34    #[default]
35    Other,
36}
37
38
39
40impl From<(String, String)> for RsLink {
41    fn from((platform, id): (String, String)) -> Self {       
42        RsLink {
43            platform,
44            kind: None,
45            id,
46            file: None,
47            user: None,
48            plugin: None,
49        }
50    }
51}
52
53pub trait ToRsLinks {
54    fn to_rs_links(self) -> Vec<RsLink>;
55}
56
57// Implement for HashMap
58impl ToRsLinks for HashMap<String, Option<String>> {
59    fn to_rs_links(self) -> Vec<RsLink> {
60        self.into_iter()
61            .filter_map(|(platform, opt_id)| {
62                opt_id.map(|id| RsLink::from((platform, id)))
63            })
64            .collect()
65    }
66}
67
68// Optional implementation for Option<HashMap>
69impl ToRsLinks for Option<HashMap<String, Option<String>>> {
70    fn to_rs_links(self) -> Vec<RsLink> {
71        match self {
72            Some(map) => map.to_rs_links(),
73            None => Vec::new(),
74        }
75    }
76}