//! `link.open` — what one of the app's own links opens (`cottonbooks://…`,//! the iPhone's `urlScheme`), decided here so no shell parses a link.//!//! The link's host and path are read as one route, as the iPhone's//! `Route.deepLink` reads them (`cottonbooks://lists/book/<id>` and//! `cottonbooks:///lists/book/<id>` are one link)://!//! - `/lists/<table>/<id>`, or a route a kept table names as its//! `record_route` with the record's id in it, opens on that record://! `{ visit: { table, id } }`.//! - A bare key of a tool the app holds opens that tool: `{ tool }`.//! - Nothing after the scheme opens the app as it stands: `{}`.//! - Anything else says, in the kit's words, that nothing here opens it://! `{ said }`. A table this app does not keep is such a link.//!//! Which scheme is the app's is the build's (the shell takes only its own//! links here); the query (a database named) is not read, every app being//! over the one personal database.use cotton_core::definitions::TableDefinition;use cotton_core::kit::AppKit;use cotton_core::rules::shell_lines::LineSet;use serde_json::{json, Value};usecrate::handle::CottonHandle;usecrate::methods::shell_deck::definitions;usecrate::methods::MethodError;/// The route every record opens on whatever its table (the phone's `/lists/[table]/[id]`).const LIST_RECORD: &str = "/lists/[table]/[id]";/// A link's host and path as one route's segments, its scheme and query/// dropped. Ids and keys are written plain, so nothing is decoded.pubfn segments(url: &str) -> Vec<String> {let rest = url.split_once(':').map_or(url, |(_, rest)| rest);let rest = rest.split(['?', '#']).next().unwrap_or_default(); rest.split('/').filter(|segment| !segment.is_empty()).map(str::to_string).collect()}/// Whether `segments` are the route `pattern`, and its `[name]` slots as they are filled.fn fills(pattern: &str, segments: &[String]) -> Option<Vec<(String, String)>> {let parts: Vec<&str> = pattern.split('/').filter(|part| !part.is_empty()).collect();if parts.len() != segments.len() {returnNone; }letmut filled = Vec::new();for (part, segment) in parts.iter().zip(segments) {match part.strip_prefix('[').and_then(|slot| slot.strip_suffix(']')) {Some(slot) => filled.push((slot.to_string(), segment.clone())),Noneif part == segment => {}None => returnNone, } }Some(filled)}/// What a link of the app keeping `kit` opens, its definitions `tables`.pubfn opens(kit: Option<&AppKit>, tables: &[TableDefinition], url: &str) -> Value {let segments = segments(url);if segments.is_empty() {return json!({}); }let keeps = |table: &str| kit.is_none_or(|kit| kit.deals_table(table)) && tables.iter().any(|definition| definition.id == table);ifletSome(filled) = fills(LIST_RECORD, &segments) {let slot = |name: &str| filled.iter().find(|(slot, _)| slot == name).map(|(_, value)| value.as_str()).unwrap_or_default();if keeps(slot("table")) {return json!({ "visit": { "table": slot("table"), "id": slot("id") } }); } }for definition in tables.iter().filter(|definition| keeps(&definition.id)) {letSome(route) = definition.record_route.as_deref() else { continue };ifletSome(filled) = fills(route, &segments) {ifletSome((_, id)) = filled.iter().find(|(slot, _)| slot == "id") {return json!({ "visit": { "table": definition.id, "id": id } }); } } }iflet [key] = segments.as_slice() {if kit.is_none_or(|kit| kit.keeps_tool(key)) {return json!({ "tool": key }); } }let name = segments.join("/");let said = LineSet::shipped("boot").and_then(|lines| lines.say("deep_link.app_opens_nothing_named", &[("name", &name)])).unwrap_or_default(); json!({ "said": said })}/// `link.open { url }`.pubfn open_method(handle: &CottonHandle, request: &Value) -> Result<Value, MethodError> {let url = request["url"].as_str().ok_or_else(|| MethodError::new("bad_request", "link.open takes a url"))?;let client = handle.client();let kit = AppKit::at(client.layout().root()).map_err(|why| MethodError::new("unreadable", why))?;Ok(opens(kit.as_ref(), &definitions(&client)?, url))}#[cfg(test)]#[path = "link_open_tests.rs"]mod tests;