mod error;
mod sys;
use std::path::Path;
pub use error::{Error, Result};
use robius_common::FileLocation;
#[derive(Clone, Debug, Default)]
pub struct ShareSheet {
options: ShareOptions,
}
impl ShareSheet {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn set_title(mut self, title: impl Into<String>) -> Self {
self.options.title = Some(title.into());
self
}
#[must_use]
pub fn set_subject(mut self, subject: impl Into<String>) -> Self {
self.options.subject = Some(subject.into());
self
}
#[must_use]
pub fn add_text(mut self, text: impl Into<String>) -> Self {
self.options.items.push(ShareItem::Text(text.into()));
self
}
#[must_use]
pub fn add_url(mut self, url: impl Into<String>) -> Self {
self.options.items.push(ShareItem::Url(url.into()));
self
}
#[must_use]
pub fn add_file<P: AsRef<Path>>(mut self, path: P) -> Self {
self.options.items.push(ShareItem::File(SharedFile::from_path(path)));
self
}
#[must_use]
pub fn add_file_with_mime_type<P: AsRef<Path>>(
mut self,
path: P,
mime_type: impl Into<String>,
) -> Self {
self.options.items.push(ShareItem::File(
SharedFile::from_path(path).set_mime_type(mime_type),
));
self
}
#[must_use]
pub fn add_file_uri(mut self, uri: impl Into<String>) -> Self {
self.options.items.push(ShareItem::File(SharedFile::from_uri(uri)));
self
}
#[must_use]
pub fn add_file_uri_with_mime_type(
mut self,
uri: impl Into<String>,
mime_type: impl Into<String>,
) -> Self {
self.options.items.push(ShareItem::File(
SharedFile::from_uri(uri).set_mime_type(mime_type),
));
self
}
pub fn share(self) -> Result<()> {
self.options.validate()?;
sys::share(self.options)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SharedFile {
location: FileLocation,
mime_type: Option<String>,
}
impl SharedFile {
pub fn from_path<P: AsRef<Path>>(path: P) -> Self {
Self {
location: FileLocation::from_path(path),
mime_type: None,
}
}
pub fn from_uri(uri: impl Into<String>) -> Self {
Self {
location: FileLocation::from_uri(uri),
mime_type: None,
}
}
#[must_use]
pub fn set_mime_type(mut self, mime_type: impl Into<String>) -> Self {
self.mime_type = Some(mime_type.into());
self
}
pub fn path(&self) -> Option<&Path> {
self.location.path()
}
pub fn uri(&self) -> Option<&str> {
self.location.uri()
}
pub fn mime_type(&self) -> Option<&str> {
self.mime_type.as_deref()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ShareItem {
Text(String),
Url(String),
File(SharedFile),
}
#[derive(Clone, Debug, Default)]
pub(crate) struct ShareOptions {
pub(crate) title: Option<String>,
pub(crate) subject: Option<String>,
pub(crate) items: Vec<ShareItem>,
}
impl ShareOptions {
fn validate(&self) -> Result<()> {
if self.items.is_empty() {
return Err(Error::Empty);
}
for item in &self.items {
match item {
ShareItem::Text(text) if text.trim().is_empty() => return Err(Error::InvalidItem),
ShareItem::Url(url) if url.trim().is_empty() => return Err(Error::InvalidItem),
ShareItem::File(file) => match &file.location {
FileLocation::Path(path) if path.as_os_str().is_empty() => {
return Err(Error::InvalidItem);
}
FileLocation::Uri(uri) if uri.trim().is_empty() => {
return Err(Error::InvalidItem);
}
_ if file.mime_type.as_deref().is_some_and(|m| m.trim().is_empty()) => {
return Err(Error::InvalidItem);
}
_ => {}
},
_ => {}
}
}
Ok(())
}
}
pub(crate) fn text_items(options: &ShareOptions) -> Vec<&str> {
options
.items
.iter()
.filter_map(|item| match item {
ShareItem::Text(text) | ShareItem::Url(text) => Some(text.as_str()),
ShareItem::File(_) => None,
})
.collect()
}
#[cfg_attr(not(target_os = "android"), allow(dead_code))]
pub(crate) fn file_items(options: &ShareOptions) -> impl Iterator<Item = &SharedFile> {
options.items.iter().filter_map(|item| match item {
ShareItem::File(file) => Some(file),
_ => None,
})
}
#[cfg_attr(not(target_os = "android"), allow(dead_code))]
pub(crate) fn shared_text(options: &ShareOptions) -> Option<String> {
let mut text = String::new();
for item in text_items(options) {
if !text.is_empty() {
text.push('\n');
}
text.push_str(item);
}
(!text.is_empty()).then_some(text)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_payload_is_invalid() {
assert!(matches!(
ShareSheet::new().options.validate(),
Err(Error::Empty)
));
}
#[test]
fn empty_text_url_path_uri_and_mime_type_are_invalid() {
let invalid_payloads = [
ShareSheet::new().add_text(""),
ShareSheet::new().add_url(""),
ShareSheet::new().add_file(""),
ShareSheet::new().add_file_uri(""),
ShareSheet::new().add_file_with_mime_type("share.txt", ""),
ShareSheet::new().add_file_uri_with_mime_type("content://robius/share.txt", ""),
];
for payload in invalid_payloads {
assert!(matches!(payload.options.validate(), Err(Error::InvalidItem)));
}
}
#[test]
fn text_and_urls_are_combined_in_order() {
let sheet = ShareSheet::new()
.add_text("hello")
.add_url("https://robius.rs/")
.add_text("goodbye");
assert_eq!(
shared_text(&sheet.options).as_deref(),
Some("hello\nhttps://robius.rs/\ngoodbye"),
);
}
#[test]
fn file_items_preserve_paths_uris_and_mime_types() {
let sheet = ShareSheet::new()
.add_file_with_mime_type("share.txt", "text/plain")
.add_file_uri_with_mime_type("content://robius/share.png", "image/png");
let files = file_items(&sheet.options).collect::<Vec<_>>();
assert_eq!(files.len(), 2);
assert_eq!(files[0].path(), Some(Path::new("share.txt")));
assert_eq!(files[0].uri(), None);
assert_eq!(files[0].mime_type(), Some("text/plain"));
assert_eq!(files[1].path(), None);
assert_eq!(files[1].uri(), Some("content://robius/share.png"));
assert_eq!(files[1].mime_type(), Some("image/png"));
}
#[test]
fn valid_builder_options_pass_validation() {
let sheet = ShareSheet::new()
.set_title("Share")
.set_subject("Robius")
.add_text("hello")
.add_url("https://robius.rs/")
.add_file("share.txt")
.add_file_uri("content://robius/share.txt");
assert!(sheet.options.validate().is_ok());
}
}