use serde_json::Value as Json;
use serde_json::json;
use crate::HeadlessError;
use crate::Position;
const VERSION: u64 = 1;
#[derive(Clone, Debug, PartialEq)]
pub struct Snapshot {
content: Json,
selection: Option<(Position, Position)>,
}
impl Snapshot {
pub(crate) fn new(content: Json, selection: Option<(Position, Position)>) -> Self {
Self { content, selection }
}
#[must_use]
pub fn content(&self) -> &Json {
&self.content
}
#[must_use]
pub fn selection(&self) -> Option<(&Position, &Position)> {
self.selection.as_ref().map(|(anchor, head)| (anchor, head))
}
#[must_use]
pub fn to_json(&self) -> Json {
let selection = self.selection.as_ref().map(
|(anchor, head)| json!({ "anchor": position_to_json(anchor), "head": position_to_json(head) }),
);
json!({ "version": VERSION, "content": self.content, "selection": selection })
}
pub fn from_json(json: &Json) -> Result<Self, HeadlessError> {
let invalid = |reason: &str| HeadlessError::Snapshot(reason.to_owned());
if json.get("version").and_then(Json::as_u64) != Some(VERSION) {
return Err(invalid("expected version 1"));
}
let content = json
.get("content")
.filter(|content| content.is_object())
.ok_or_else(|| invalid("expected a `content` document"))?;
let selection = match json.get("selection") {
None | Some(Json::Null) => None,
Some(selection) => {
let side = |name| {
selection
.get(name)
.and_then(position_from_json)
.ok_or_else(|| invalid("expected `anchor` and `head` positions"))
};
Some((side("anchor")?, side("head")?))
}
};
Ok(Self {
content: content.clone(),
selection,
})
}
}
pub(crate) fn position_to_json(position: &Position) -> Json {
json!({ "path": position.path, "offset": position.offset })
}
pub(crate) fn position_from_json(json: &Json) -> Option<Position> {
let path = json
.get("path")?
.as_array()?
.iter()
.map(|index| index.as_u64().and_then(|index| u32::try_from(index).ok()))
.collect::<Option<Vec<u32>>>()?;
let offset = u32::try_from(json.get("offset")?.as_u64()?).ok()?;
Some(Position { path, offset })
}