use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SharedFile {
pub data: String,
pub name: String,
pub mime_type: String,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ShareOptions {
pub text: Option<String>,
pub title: Option<String>,
pub url: Option<String>,
pub files: Option<Vec<SharedFile>>,
}
impl ShareOptions {
pub fn has_shareable_content(&self) -> bool {
self.text.as_ref().is_some_and(|value| !value.is_empty())
|| self.url.as_ref().is_some_and(|value| !value.is_empty())
|| self.files.as_ref().is_some_and(|files| !files.is_empty())
}
pub fn combined_text(&self) -> Option<String> {
match (self.text.as_deref(), self.url.as_deref()) {
(Some(text), Some(url)) if !text.is_empty() && !url.is_empty() => {
Some(format!("{text}\n{url}"))
}
(Some(text), _) if !text.is_empty() => Some(text.to_string()),
(_, Some(url)) if !url.is_empty() => Some(url.to_string()),
_ => None,
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CanShareResult {
pub value: bool,
}
#[cfg(test)]
mod tests {
use super::{ShareOptions, SharedFile};
fn options(
text: Option<&str>,
url: Option<&str>,
files: Option<Vec<SharedFile>>,
) -> ShareOptions {
ShareOptions {
text: text.map(ToString::to_string),
title: None,
url: url.map(ToString::to_string),
files,
}
}
#[test]
fn empty_options_are_not_shareable() {
assert!(!options(None, None, None).has_shareable_content());
assert!(!options(Some(""), Some(""), Some(Vec::new())).has_shareable_content());
}
#[test]
fn text_url_or_files_are_shareable() {
let file = SharedFile {
data: "aGVsbG8=".to_string(),
name: "hello.txt".to_string(),
mime_type: "text/plain".to_string(),
};
assert!(options(Some("hello"), None, None).has_shareable_content());
assert!(options(None, Some("https://example.com"), None).has_shareable_content());
assert!(options(None, None, Some(vec![file])).has_shareable_content());
}
#[test]
fn combined_text_preserves_text_and_url() {
let data = options(Some("hello"), Some("https://example.com"), None);
assert_eq!(
data.combined_text().as_deref(),
Some("hello\nhttps://example.com")
);
}
}