use serde::{Deserialize, Serialize};
use crate::InteractionAnchor;
pub const BOUND_RESOURCE_PREFIX: &str = "tilo-ui-bound:v1:";
#[must_use]
pub fn logical_resource_id(id: &str) -> std::borrow::Cow<'_, str> {
let decoded = id.strip_prefix(BOUND_RESOURCE_PREFIX).and_then(|rest| {
let hex = rest.rsplit(':').next()?;
if hex.is_empty() || hex.len() % 2 != 0 {
return None;
}
let bytes = (0..hex.len())
.step_by(2)
.map(|at| u8::from_str_radix(hex.get(at..at.checked_add(2)?)?, 16).ok())
.collect::<Option<Vec<u8>>>()?;
String::from_utf8(bytes)
.ok()
.filter(|key| !key.is_empty() && !key.chars().any(char::is_control))
});
decoded.map_or(std::borrow::Cow::Borrowed(id), std::borrow::Cow::Owned)
}
pub const MAX_EXCERPT_CHARS: usize = 1000;
pub const MAX_EXCERPT_BYTES: usize = 4 * 1024;
pub const MAX_PLACE_ITEM_BYTES: usize = 512;
pub const MAX_SELECTION_ANCHOR_BYTES: usize = 8 * 1024;
pub const MAX_SELECTED_ITEMS: usize = 64;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectionPlace {
pub item: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub at: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub start: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub end: Option<usize>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SelectionSpan {
Item,
Caret(usize),
Range { start: usize, end: usize },
}
impl SelectionPlace {
#[must_use]
pub fn item(item: impl Into<String>) -> Self {
Self {
item: item.into(),
at: None,
start: None,
end: None,
}
}
#[must_use]
pub fn caret(item: impl Into<String>, at: usize) -> Self {
Self {
at: Some(at),
..Self::item(item)
}
}
#[must_use]
pub fn range(item: impl Into<String>, start: usize, end: usize) -> Self {
Self {
start: Some(start.min(end)),
end: Some(start.max(end)),
..Self::item(item)
}
}
pub fn validated_span(&self) -> Result<SelectionSpan, String> {
if self.item.is_empty() || self.item.len() > MAX_PLACE_ITEM_BYTES {
return Err("a selection place names one item of bounded length".into());
}
if self.item.chars().any(char::is_control) {
return Err("a selection place item contains control characters".into());
}
match (self.at, self.start, self.end) {
(None, None, None) => Ok(SelectionSpan::Item),
(Some(at), None, None) => Ok(SelectionSpan::Caret(at)),
(None, Some(start), Some(end)) if start <= end => {
Ok(SelectionSpan::Range { start, end })
}
_ => Err(
"a place is an item, a caret `at` in it, or an ordered range `start..end` of it"
.into(),
),
}
}
pub fn validate(&self) -> Result<(), String> {
self.validated_span().map(|_| ())
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SelectionAnchor {
pub place: Option<SelectionPlace>,
pub items: Vec<SelectionPlace>,
pub interaction: Option<InteractionAnchor>,
pub excerpt: Option<String>,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct SelectionAnchorWire {
#[serde(default, skip_serializing_if = "Option::is_none")]
place: Option<SelectionPlace>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
items: Vec<SelectionPlace>,
#[serde(default, skip_serializing_if = "Option::is_none")]
interaction: Option<InteractionAnchor>,
#[serde(default, skip_serializing_if = "Option::is_none")]
excerpt: Option<String>,
}
impl Serialize for SelectionAnchor {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let wire = SelectionAnchorWire {
place: self.place.clone(),
items: self.items.clone(),
interaction: self.interaction.clone(),
excerpt: self.excerpt.clone(),
};
if serializer.is_human_readable() {
wire.serialize(serializer)
} else {
serde_json::to_string(&wire)
.map_err(serde::ser::Error::custom)?
.serialize(serializer)
}
}
}
impl<'de> Deserialize<'de> for SelectionAnchor {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let wire = if deserializer.is_human_readable() {
SelectionAnchorWire::deserialize(deserializer)?
} else {
let text = String::deserialize(deserializer)?;
serde_json::from_str(&text).map_err(serde::de::Error::custom)?
};
Ok(Self {
place: wire.place,
items: wire.items,
interaction: wire.interaction,
excerpt: wire.excerpt,
})
}
}
impl SelectionAnchor {
#[must_use]
pub fn of_text(excerpt: &str) -> Self {
Self {
excerpt: excerpt_of(excerpt),
..Self::default()
}
}
#[must_use]
pub fn of_items<I, L>(items: impl IntoIterator<Item = (I, L)>) -> Self
where
I: Into<String>,
L: AsRef<str>,
{
let mut places = Vec::new();
let mut labels = Vec::new();
let mut total = 0_usize;
for (id, label) in items {
total = total.saturating_add(1);
if places.len() == MAX_SELECTED_ITEMS {
continue;
}
let id = id.into();
let label = label.as_ref().trim();
labels.push(if label.is_empty() {
id.clone()
} else {
label.to_owned()
});
places.push(SelectionPlace::item(id));
}
let budget = MAX_EXCERPT_CHARS.saturating_sub(32);
let mut listed = String::new();
let mut shown = 0_usize;
for label in &labels {
let line = format!("- {label}");
let used = listed
.chars()
.count()
.saturating_add(line.chars().count())
.saturating_add(1);
if used > budget {
break;
}
if !listed.is_empty() {
listed.push('\n');
}
listed.push_str(&line);
shown = shown.saturating_add(1);
}
let more = total.saturating_sub(shown);
if more > 0 {
if !listed.is_empty() {
listed.push('\n');
}
listed.push_str("- and ");
listed.push_str(&more.to_string());
listed.push_str(" more");
}
Self {
items: places,
excerpt: excerpt_of(&listed),
..Self::default()
}
}
#[must_use]
pub fn with_excerpt(mut self, excerpt: &str) -> Self {
self.excerpt = excerpt_of(excerpt);
self
}
#[must_use]
pub fn at(mut self, place: SelectionPlace) -> Self {
self.place = Some(place);
self
}
#[must_use]
pub fn spanning(mut self, region: &str, mut ranges: Vec<crate::InteractionTextRange>) -> Self {
if ranges.len() > 1 {
ranges.truncate(crate::interaction::MAX_ANCHOR_ITEMS);
self.interaction = Some(InteractionAnchor::Text {
region: region.to_owned(),
ranges,
});
}
self
}
#[must_use]
pub fn spanned_ranges(&self) -> Option<&[crate::InteractionTextRange]> {
match &self.interaction {
Some(InteractionAnchor::Text { ranges, .. }) if ranges.len() > 1 => Some(ranges),
_ => None,
}
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.place.is_none()
&& self.items.is_empty()
&& self.interaction.is_none()
&& self.excerpt.is_none()
}
#[must_use]
pub fn non_empty(self) -> Option<Self> {
(!self.is_empty()).then_some(self)
}
pub fn validate(&self) -> Result<(), String> {
if let Some(place) = &self.place {
place.validate()?;
}
if self.items.len() > MAX_SELECTED_ITEMS {
return Err("a selection names too many items".into());
}
for item in &self.items {
item.validate()?;
}
if let Some(excerpt) = &self.excerpt
&& (excerpt.len() > MAX_EXCERPT_BYTES
|| excerpt.chars().count() > MAX_EXCERPT_CHARS + 1
|| excerpt
.chars()
.any(|c| c.is_control() && !matches!(c, '\n' | '\t')))
{
return Err("a selection excerpt is oversized or contains control characters".into());
}
if let Some(interaction) = &self.interaction {
let encoded = serde_json::to_vec(interaction).map_err(|error| error.to_string())?;
if encoded.len() > MAX_SELECTION_ANCHOR_BYTES {
return Err("a selection's interaction anchor is oversized".into());
}
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectionSubject {
pub app_id: String,
pub app_label: String,
pub resource: crate::ActionResource,
pub anchor: SelectionAnchor,
}
impl SelectionSubject {
pub fn validate(&self) -> Result<(), String> {
for (value, maximum, name) in [
(self.app_id.as_str(), 256, "app"),
(self.app_label.as_str(), 512, "app label"),
(self.resource.id.as_str(), 2048, "resource id"),
(self.resource.kind.as_str(), 128, "resource kind"),
(self.resource.label.as_str(), 512, "resource label"),
] {
if value.len() > maximum || value.chars().any(char::is_control) {
return Err(format!("a selection's {name} is oversized or malformed"));
}
}
if self.resource.id.trim().is_empty() || self.app_id.trim().is_empty() {
return Err("a selection names its application and resource".into());
}
if self.anchor.is_empty() {
return Err("a selection says what was selected".into());
}
self.anchor.validate()
}
#[must_use]
pub fn is_in(&self, id: &str) -> bool {
id == self.resource.id || logical_resource_id(id) == self.resource.id
}
}
#[must_use]
pub fn excerpt_of(text: &str) -> Option<String> {
let text = text.trim();
if text.is_empty() {
return None;
}
let mut kept = text
.chars()
.filter(|c| !c.is_control() || matches!(c, '\n' | '\t'));
let mut excerpt: String = kept.by_ref().take(MAX_EXCERPT_CHARS).collect();
let mut cut = kept.next().is_some();
while excerpt.len() > MAX_EXCERPT_BYTES.saturating_sub('…'.len_utf8()) {
excerpt.pop();
cut = true;
}
if cut {
excerpt.push('…');
}
Some(excerpt)
}
#[cfg(test)]
mod tests {
use super::*;
fn anchor() -> SelectionAnchor {
SelectionAnchor {
place: Some(SelectionPlace::range("block-1", 4, 9)),
items: Vec::new(),
interaction: Some(InteractionAnchor::Objects {
ids: vec!["block-1".into()],
}),
excerpt: Some("quick".into()),
}
}
tilo_test_env::scenario! {
fn a_selection_names_its_place_and_travels_bounded;
phase a_selection_is_in_its_resource_under_a_window_alias_too;
{
let body = "notes.notebook/pages/p1/body";
let alias = format!(
"{BOUND_RESOURCE_PREFIX}2a4120d7-2f63-4578-aed8-11cb91ed3628:\
c0fe4ad7-b7d0-46c3-ab7e-78d12d5d86f4:6e6f7465732e6e6f7465626f6f6b2f70616765732f70312f626f6479"
);
assert_eq!(logical_resource_id(&alias), body);
assert_eq!(logical_resource_id(body), body);
assert_eq!(
logical_resource_id(&format!("{BOUND_RESOURCE_PREFIX}a:b:zz")),
format!("{BOUND_RESOURCE_PREFIX}a:b:zz"),
"a malformed alias is its own id"
);
let subject = SelectionSubject {
app_id: "so.tilo.notes".into(),
app_label: "Notes".into(),
resource: crate::ActionResource {
id: body.into(),
kind: "tilo.rich-text".into(),
label: "Links".into(),
},
anchor: anchor(),
};
assert!(subject.is_in(body));
assert!(subject.is_in(&alias));
assert!(!subject.is_in("notes.notebook/pages/p2/body"));
}
phase a_selection_of_several_items_names_each_and_lists_them;
{
let anchor = SelectionAnchor::of_items(
(0..MAX_SELECTED_ITEMS + 2).map(|n| (format!("file:{n}"), format!("report-{n}.pdf"))),
);
assert_eq!(anchor.items.len(), MAX_SELECTED_ITEMS);
assert_eq!(anchor.items[1], SelectionPlace::item("file:1"));
let excerpt = anchor.excerpt.as_deref().unwrap();
assert!(excerpt.starts_with("- report-0.pdf\n- report-1.pdf\n"));
assert!(excerpt.ends_with(" more"), "{excerpt}");
assert!(!excerpt.contains('…'), "the list is never cut mid-name");
let listed = excerpt
.lines()
.filter(|line| line.starts_with("- report-"))
.count();
assert!(
excerpt.ends_with(&format!("- and {} more", MAX_SELECTED_ITEMS + 2 - listed)),
"{excerpt}"
);
assert!(anchor.validate().is_ok());
let json = serde_json::to_value(&anchor).unwrap();
assert_eq!(json["items"][0], serde_json::json!({"item": "file:0"}));
assert_eq!(
serde_json::from_value::<SelectionAnchor>(json).unwrap(),
anchor
);
let encoded = postcard::to_allocvec(&anchor).unwrap();
assert_eq!(
postcard::from_bytes::<SelectionAnchor>(&encoded).unwrap(),
anchor
);
assert!(
serde_json::to_value(SelectionAnchor::of_text("x"))
.unwrap()
.get("items")
.is_none(),
"a selection inside one item writes what it wrote before"
);
let too_many = SelectionAnchor {
items: vec![SelectionPlace::item("f"); MAX_SELECTED_ITEMS + 1],
..SelectionAnchor::default()
};
assert!(too_many.validate().is_err());
}
phase place_travels_as_the_action_place_parameter_does;
{
let place = SelectionPlace::range("block-1", 9, 4);
assert_eq!(
serde_json::to_value(&place).unwrap(),
serde_json::json!({"item": "block-1", "start": 4, "end": 9})
);
assert_eq!(
serde_json::to_value(SelectionPlace::caret("b", 2)).unwrap(),
serde_json::json!({"item": "b", "at": 2})
);
assert!(
crate::validate_json_schema(
&serde_json::to_value(&place).unwrap(),
&crate::place_schema()
)
.is_ok()
);
}
phase malformed_places_are_refused;
{
assert!(SelectionPlace::item("").validate().is_err());
let both = SelectionPlace {
at: Some(1),
..SelectionPlace::range("b", 1, 2)
};
assert!(both.validate().is_err());
let open = SelectionPlace {
start: Some(3),
..SelectionPlace::item("b")
};
assert!(open.validate().is_err());
let backwards = SelectionPlace {
start: Some(3),
end: Some(1),
..SelectionPlace::item("b")
};
assert!(backwards.validate().is_err());
assert!(
serde_json::from_value::<SelectionPlace>(serde_json::json!({"item":"b","x":1}))
.is_err()
);
}
phase anchor_round_trips_through_json_and_postcard;
{
let anchor = anchor();
let json = serde_json::to_value(&anchor).unwrap();
assert_eq!(json["place"]["start"], 4);
assert_eq!(json["interaction"]["kind"], "objects");
assert_eq!(
serde_json::from_value::<SelectionAnchor>(json).unwrap(),
anchor
);
let bytes = postcard::to_allocvec(&anchor).unwrap();
assert_eq!(
postcard::from_bytes::<SelectionAnchor>(&bytes).unwrap(),
anchor
);
}
phase absent_parts_are_omitted_and_older_readers_default_them;
{
let only_text = SelectionAnchor::of_text(" hello ");
assert_eq!(
serde_json::to_value(&only_text).unwrap(),
serde_json::json!({"excerpt": "hello"})
);
assert_eq!(
serde_json::from_value::<SelectionAnchor>(serde_json::json!({})).unwrap(),
SelectionAnchor::default()
);
assert!(SelectionAnchor::of_text(" ").non_empty().is_none());
}
phase excerpts_are_bounded_and_cut_visibly;
{
let long = "word ".repeat(400);
let excerpt = excerpt_of(&long).unwrap();
assert_eq!(excerpt.chars().count(), MAX_EXCERPT_CHARS + 1);
assert!(excerpt.ends_with('…'));
assert!(SelectionAnchor::of_text(&long).validate().is_ok());
let wide = "界".repeat(MAX_EXCERPT_CHARS * 2);
let excerpt = excerpt_of(&wide).unwrap();
assert!(excerpt.len() <= MAX_EXCERPT_BYTES);
assert!(excerpt.ends_with('…'));
assert_eq!(excerpt_of("a\u{7}b\nc").as_deref(), Some("ab\nc"));
let forged = SelectionAnchor {
excerpt: Some("x".repeat(MAX_EXCERPT_BYTES + 1)),
..SelectionAnchor::default()
};
assert!(forged.validate().is_err());
}
}
}