use serde_json::{Map, Value};
pub const MAX_ARRAY_INDEX: usize = 10_000;
const FORBIDDEN: &[&str] = &["__proto__", "constructor", "prototype"];
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{message}")]
pub struct DataError {
pub message: String,
pub path: String,
}
fn error(path: &str, message: String) -> DataError {
DataError {
message,
path: path.to_string(),
}
}
pub fn segments(path: &str) -> Result<Vec<String>, DataError> {
let mut out = Vec::new();
for raw in path.split('/').filter(|s| !s.is_empty()) {
let segment = raw.replace("~1", "/").replace("~0", "~");
if FORBIDDEN.contains(&segment.as_str()) {
return Err(error(
path,
format!("Forbidden path segment '{segment}' in path '{path}'."),
));
}
out.push(segment);
}
Ok(out)
}
pub fn pointer(segments: &[String]) -> String {
if segments.is_empty() {
return "/".into();
}
segments
.iter()
.map(|s| format!("/{}", s.replace('~', "~0").replace('/', "~1")))
.collect()
}
pub fn normalize(path: &str) -> Result<String, DataError> {
Ok(pointer(&segments(path)?))
}
pub fn resolve(context: &str, path: &str) -> String {
if path.starts_with('/') {
return path.to_string();
}
let base = context.trim_end_matches('/');
if path.is_empty() || path == "." {
return if base.is_empty() {
"/".into()
} else {
base.to_string()
};
}
format!("{base}/{path}")
}
fn index(segment: &str) -> Option<usize> {
let canonical = !segment.is_empty()
&& segment.bytes().all(|b| b.is_ascii_digit())
&& (segment == "0" || !segment.starts_with('0'));
if canonical {
segment.parse().ok()
} else {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WatchId(usize);
#[derive(Debug, Clone, PartialEq)]
pub struct DataModel {
root: Value,
watches: Vec<(WatchId, Vec<String>)>,
next: usize,
}
impl Default for DataModel {
fn default() -> Self {
Self::new(Value::Object(Map::new()))
}
}
impl DataModel {
pub fn new(root: Value) -> Self {
Self {
root,
watches: Vec::new(),
next: 0,
}
}
pub fn root(&self) -> &Value {
&self.root
}
pub fn get(&self, path: &str) -> Result<Option<&Value>, DataError> {
Ok(lookup(&self.root, &segments(path)?))
}
pub fn watch(&mut self, path: &str) -> Result<WatchId, DataError> {
let id = WatchId(self.next);
self.next += 1;
self.watches.push((id, segments(path)?));
Ok(id)
}
pub fn dispose(&mut self) {
self.watches.clear();
}
pub fn set(&mut self, path: &str, value: Value) -> Result<Vec<WatchId>, DataError> {
let parts = segments(path)?;
let before = self.snapshot();
write(&mut self.root, &parts, path, Some(value))?;
Ok(self.changed(before))
}
pub fn delete(&mut self, path: &str) -> Result<Vec<WatchId>, DataError> {
let parts = segments(path)?;
let before = self.snapshot();
if parts.is_empty() {
self.root = Value::Object(Map::new());
} else if lookup_parent(&self.root, &parts).is_some() {
write(&mut self.root, &parts, path, None)?;
}
Ok(self.changed(before))
}
fn snapshot(&self) -> Vec<Option<Value>> {
self.watches
.iter()
.map(|(_, path)| lookup(&self.root, path).cloned())
.collect()
}
fn changed(&self, before: Vec<Option<Value>>) -> Vec<WatchId> {
self.watches
.iter()
.zip(before)
.filter(|((_, path), old)| lookup(&self.root, path) != old.as_ref())
.map(|((id, _), _)| *id)
.collect()
}
}
fn lookup<'a>(root: &'a Value, parts: &[String]) -> Option<&'a Value> {
let mut current = root;
for part in parts {
current = match current {
Value::Object(map) => map.get(part)?,
Value::Array(items) => items.get(index(part)?)?,
_ => return None,
};
}
(!current.is_null()).then_some(current)
}
fn lookup_parent<'a>(root: &'a Value, parts: &[String]) -> Option<&'a Value> {
let (_, parents) = parts.split_last()?;
let mut current = root;
for part in parents {
current = match current {
Value::Object(map) => map.get(part)?,
Value::Array(items) => items.get(index(part)?)?,
_ => return None,
};
}
matches!(current, Value::Object(_) | Value::Array(_)).then_some(current)
}
fn container_for(next: &str) -> Value {
if index(next).is_some() {
Value::Array(Vec::new())
} else {
Value::Object(Map::new())
}
}
fn write(
root: &mut Value,
parts: &[String],
path: &str,
value: Option<Value>,
) -> Result<(), DataError> {
let Some((last, parents)) = parts.split_last() else {
root.clone_from(&value.unwrap_or_else(|| Value::Object(Map::new())));
return Ok(());
};
let primitive = |segment: &str| {
error(
path,
format!("Cannot set path '{path}': segment '{segment}' is a primitive value."),
)
};
if !matches!(root, Value::Object(_) | Value::Array(_) | Value::Null) {
return Err(error(
path,
format!("Cannot set path '{path}': the data model root is a primitive value."),
));
}
if root.is_null() {
*root = container_for(&parts[0]);
}
let mut current = root;
for (position, part) in parents.iter().enumerate() {
let next = &parts[position + 1];
let slot = match current {
Value::Object(map) => map.entry(part.clone()).or_insert(Value::Null),
Value::Array(items) => {
let i = array_index(part, path)?;
if i >= items.len() {
items.resize(i + 1, Value::Null);
}
&mut items[i]
}
_ => return Err(primitive(part)),
};
if slot.is_null() {
if let Some(i) = index(next) {
if i > MAX_ARRAY_INDEX {
return Err(too_large(path, next));
}
}
*slot = container_for(next);
} else if !matches!(slot, Value::Object(_) | Value::Array(_)) {
return Err(primitive(next));
}
current = slot;
}
match current {
Value::Object(map) => match value {
Some(value) => {
map.insert(last.clone(), value);
}
None => {
map.remove(last);
}
},
Value::Array(items) => {
let i = array_index(last, path)?;
match value {
Some(value) => {
if i >= items.len() {
items.resize(i + 1, Value::Null);
}
items[i] = value;
}
None => {
if let Some(slot) = items.get_mut(i) {
*slot = Value::Null;
}
}
}
}
_ => return Err(primitive(last)),
}
Ok(())
}
fn array_index(segment: &str, path: &str) -> Result<usize, DataError> {
let Some(i) = index(segment) else {
return Err(error(
path,
format!("Cannot use non-numeric segment '{segment}' on an array in path '{path}'."),
));
};
if i > MAX_ARRAY_INDEX {
return Err(too_large(path, segment));
}
Ok(i)
}
fn too_large(path: &str, segment: &str) -> DataError {
error(
path,
format!(
"Cannot set path '{path}': array index '{segment}' exceeds maximum supported index ({MAX_ARRAY_INDEX})."
),
)
}