use std::collections::BTreeSet;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use crate::curation::FoldRecord;
use sceno::LinkKey;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
pub const VIEW_INTENT_DIR: &str = "views";
pub const VIEW_INTENT_SCHEMA: &str = "mere.view-intent/v2";
pub const VIEW_INTENT_SCHEMA_V1: &str = "mere.view-intent/v1";
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct HiddenRelationRecord {
pub from_id: Uuid,
pub to_id: Uuid,
pub relation_tag: u32,
}
impl HiddenRelationRecord {
pub fn new(from_id: Uuid, to_id: Uuid, relation_tag: u32) -> Self {
Self {
from_id,
to_id,
relation_tag,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct ViewIntent {
#[serde(default)]
pub hidden_relations: BTreeSet<HiddenRelationRecord>,
#[serde(default)]
pub hidden_links: BTreeSet<LinkKey>,
#[serde(default)]
pub hidden_assertions: BTreeSet<String>,
#[serde(default)]
pub folds: Vec<FoldRecord>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub retained_folds: Vec<serde_json::Value>,
#[serde(default)]
pub strategy: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub commands: Option<hest::menu::CommandChoices>,
}
impl ViewIntent {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.hidden_relations.is_empty()
&& self.hidden_links.is_empty()
&& self.hidden_assertions.is_empty()
&& self.folds.is_empty()
&& self.retained_folds.is_empty()
&& self.strategy.is_none()
&& self.commands.is_none()
}
}
pub fn pane_intent_filename(pane_id: u64) -> String {
format!("{pane_id}.json")
}
pub fn view_intent_path(session_dir: &Path, frame_id_str: &str, pane_id: u64) -> PathBuf {
session_dir
.join(VIEW_INTENT_DIR)
.join(frame_id_str)
.join(pane_intent_filename(pane_id))
}
pub fn save_view_intent(
session_dir: &Path,
frame_id_str: &str,
pane_id: u64,
intent: &ViewIntent,
) -> io::Result<()> {
let target = view_intent_path(session_dir, frame_id_str, pane_id);
let json = serde_json::to_string_pretty(intent)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
muniment::atomic_file::write_bytes_with_backup(&target, json.as_bytes())
}
pub fn load_view_intent(
session_dir: &Path,
frame_id_str: &str,
pane_id: u64,
) -> io::Result<Option<ViewIntent>> {
let path = view_intent_path(session_dir, frame_id_str, pane_id);
if !path.exists() {
return Ok(None);
}
let text = fs::read_to_string(&path)?;
let intent: ViewIntent =
serde_json::from_str(&text).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Ok(Some(intent))
}
pub fn view_intent_exists(session_dir: &Path, frame_id_str: &str, pane_id: u64) -> bool {
view_intent_path(session_dir, frame_id_str, pane_id).exists()
}
mod attention;
pub use attention::{AttentionState, CameraSnapshot};
#[cfg(test)]
mod tests;