zaun_community_protocol/framing/app_data.rs
1//! How one frame message travels as SimpleX application data (specification ยง5.1): content type [`DATA_TAG`], the frame text under [`FRAME_KEY`], and the fixed [`FALLBACK_TEXT`] an app without Zaun shows.
2
3use serde_json::{Map, Value};
4
5/// The content type of a frame message (`MsgContent` `type`).
6pub const DATA_TAG: &str = "zaun";
7
8/// The data key holding the frame message text.
9pub const FRAME_KEY: &str = "f";
10
11/// What an app that does not know [`DATA_TAG`] shows. Fixed: it never carries frame content.
12pub const FALLBACK_TEXT: &str = "Zaun community data (open it in a Zaun app)";
13
14/// The application data that carries the frame message `frame`.
15#[must_use]
16pub fn data(frame: &str) -> Map<String, Value> {
17 let mut json = Map::new();
18 json.insert(FRAME_KEY.to_owned(), Value::String(frame.to_owned()));
19 json
20}
21
22/// The frame message text in application data of content type `tag`, or `None` when it is not
23/// a Zaun frame. Keys other than [`FRAME_KEY`] are ignored, for later additions.
24#[must_use]
25pub fn frame<'a>(tag: &str, json: &'a Map<String, Value>) -> Option<&'a str> {
26 if tag != DATA_TAG {
27 return None;
28 }
29 json.get(FRAME_KEY).and_then(Value::as_str)
30}