Cotton code
57 lines · 2.7 KB · RustRaw
//! `share.door` — which of the app's doors takes a file shared into it, decided//! here so no shell carries the rule.//!//! Android's share sheet and Open with hand an app a file with the media type//! its provider gives and the name it came under. The app takes it through the//! same door its Plus would (Books' Add a file or Bring Goodreads in, Music's//! and Cinemas' Add files), so the file lands as one picked would. The door is//! the first, in the kit's order of its tables and each table's doors, whose//! types take the file ([`cotton_core::definitions::file_type_takes`]); a//! folder door takes no file. The answer is `{ table, door }`, the door as//! `shell.plus` draws it, or `{}` when no door of this app takes it (the share//! goes on to the app's own branch). Cotton, keeping the whole object, takes//! its shares by its own drain and is answered `{}`.use cotton_core::definitions::{file_type_takes, TableDefinition};use cotton_core::kit::AppKit;use serde_json::{json, Value};use crate::handle::CottonHandle;use crate::methods::shell_deck::{definitions, plus_of};use crate::methods::MethodError;/// The type a folder door names: it takes a tree, never a shared file.const FOLDER: &str = "public.folder";/// The table and door (as `shell.plus` draws it) that take a file of `mime`/// named `name` in the app keeping `kit`.pub fn door_for(kit: Option<&AppKit>, tables: &[TableDefinition], mime: Option<&str>, name: Option<&str>) -> Option<(String, Value)> {    let kit = kit?;    for table in &kit.tables {        let Some(definition) = tables.iter().find(|definition| &definition.id == table) else { continue };        let Some(create) = definition.create.as_ref() else { continue };        let taking = create.doors.iter().find(|door| {            door.files.as_ref().is_some_and(|files| files.types.iter().any(|kind| kind != FOLDER && file_type_takes(kind, mime, name)))        });        if let Some(door) = taking {            let drawn = plus_of(definition)["doors"].as_array()?.iter().find(|drawn| drawn["id"] == door.id.as_str())?.clone();            return Some((table.clone(), drawn));        }    }    None}pub fn door_method(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {    let client = handle.client();    let kit = AppKit::at(client.layout().root()).map_err(|why| MethodError::new("unreadable", why))?;    let Some(kit) = kit else { return Ok(json!({})) };    let tables = definitions(&client)?;    Ok(match door_for(Some(&kit), &tables, request["mime"].as_str(), request["fileName"].as_str()) {        Some((table, door)) => json!({ "table": table, "door": door }),        None => json!({}),    })}#[cfg(test)]#[path = "share_door_tests.rs"]mod tests;