tauri_plugin_vnidrop_share/
models.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Deserialize, Serialize, Clone)]
9#[serde(rename_all = "camelCase")]
10pub struct SharedFile {
11 pub data: String,
12 pub name: String,
13 pub mime_type: String,
14}
15
16#[derive(Debug, Deserialize, Serialize, Clone)]
47#[serde(rename_all = "camelCase")]
48pub struct ShareOptions {
49 pub text: Option<String>,
51 pub title: Option<String>,
53 pub url: Option<String>,
55 pub files: Option<Vec<SharedFile>>,
57}
58
59impl ShareOptions {
60 pub fn has_shareable_content(&self) -> bool {
62 self.text.as_ref().is_some_and(|value| !value.is_empty())
63 || self.url.as_ref().is_some_and(|value| !value.is_empty())
64 || self.files.as_ref().is_some_and(|files| !files.is_empty())
65 }
66
67 pub fn combined_text(&self) -> Option<String> {
69 match (self.text.as_deref(), self.url.as_deref()) {
70 (Some(text), Some(url)) if !text.is_empty() && !url.is_empty() => {
71 Some(format!("{text}\n{url}"))
72 }
73 (Some(text), _) if !text.is_empty() => Some(text.to_string()),
74 (_, Some(url)) if !url.is_empty() => Some(url.to_string()),
75 _ => None,
76 }
77 }
78}
79
80#[derive(Debug, Deserialize, Serialize, Clone)]
86#[serde(rename_all = "camelCase")]
87pub struct CanShareResult {
88 pub value: bool,
89}
90
91#[cfg(test)]
92mod tests {
93 use super::{ShareOptions, SharedFile};
94
95 fn options(
96 text: Option<&str>,
97 url: Option<&str>,
98 files: Option<Vec<SharedFile>>,
99 ) -> ShareOptions {
100 ShareOptions {
101 text: text.map(ToString::to_string),
102 title: None,
103 url: url.map(ToString::to_string),
104 files,
105 }
106 }
107
108 #[test]
109 fn empty_options_are_not_shareable() {
110 assert!(!options(None, None, None).has_shareable_content());
111 assert!(!options(Some(""), Some(""), Some(Vec::new())).has_shareable_content());
112 }
113
114 #[test]
115 fn text_url_or_files_are_shareable() {
116 let file = SharedFile {
117 data: "aGVsbG8=".to_string(),
118 name: "hello.txt".to_string(),
119 mime_type: "text/plain".to_string(),
120 };
121
122 assert!(options(Some("hello"), None, None).has_shareable_content());
123 assert!(options(None, Some("https://example.com"), None).has_shareable_content());
124 assert!(options(None, None, Some(vec![file])).has_shareable_content());
125 }
126
127 #[test]
128 fn combined_text_preserves_text_and_url() {
129 let data = options(Some("hello"), Some("https://example.com"), None);
130 assert_eq!(
131 data.combined_text().as_deref(),
132 Some("hello\nhttps://example.com")
133 );
134 }
135}