use crate::{CommentKind, Commented, EditError, Expr, Feature, Step, Value};
pub trait Document {
fn to_source(&self) -> String;
fn to_value(&self) -> Value;
fn features(&self) -> &'static [Feature];
fn apply(&mut self, expr: &Expr) -> Result<(), EditError>;
fn has_comments(&self) -> bool;
fn to_commented(&self) -> Option<Commented> {
None
}
fn source_slice(&self, path: &[Step]) -> Vec<String> {
let _ = path;
Vec::new()
}
fn set_comment(
&mut self,
path: &[Step],
kind: CommentKind,
text: &str,
) -> Result<Vec<String>, EditError> {
let _ = (path, kind, text);
Err(EditError::new(
"editing comments (`#`) isn't supported for this format yet",
))
}
fn delete_comment(&mut self, path: &[Step], kind: CommentKind) -> Result<(), EditError> {
let _ = (path, kind);
Err(EditError::new(
"deleting comments (`#`) isn't supported for this format yet",
))
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Bare;
impl Document for Bare {
fn to_source(&self) -> String {
String::new()
}
fn to_value(&self) -> Value {
Value::Null
}
fn features(&self) -> &'static [Feature] {
&[]
}
fn apply(&mut self, _expr: &Expr) -> Result<(), EditError> {
Ok(())
}
fn has_comments(&self) -> bool {
false
}
}
#[test]
fn trait_defaults() {
let mut d = Bare;
assert!(d.to_commented().is_none());
assert!(d.source_slice(&[]).is_empty());
assert!(d.set_comment(&[], CommentKind::Head, "x").is_err());
assert!(d.delete_comment(&[], CommentKind::Head).is_err());
}
}