use serde::Serialize;
#[derive(Debug, Serialize)]
pub struct RawDocument {
pub path: String,
pub content: String,
pub last_view: Option<String>,
pub view_meta: Option<String>,
pub lossy: bool,
pub encoding: String,
pub mtime_ms: i64,
pub size: i64,
pub source_sha256: Option<String>,
pub stream_token: Option<String>,
}
#[derive(Debug, Serialize)]
pub struct FileFingerprint {
pub mtime_ms: i64,
pub size: i64,
}
#[derive(Debug, Serialize)]
pub struct WriteDocumentResult {
pub encoding: String,
pub source_sha256: String,
}
pub trait DocumentBackend {
fn format_of(&self, path: &str) -> &'static str;
fn read(&self, path: &str) -> Result<RawDocument, String>;
fn write(
&self,
path: &str,
content: &str,
encoding: &str,
expected_source_sha256: Option<&str>,
) -> Result<WriteDocumentResult, String>;
fn write_bytes(&self, path: &str, content_base64: &str) -> Result<(), String>;
fn write_raw_bytes(&self, path: &str, bytes: &[u8]) -> Result<(), String>;
fn copy_document(&self, source: &str, target: &str) -> Result<(), String>;
fn fingerprint(&self, path: &str) -> Result<FileFingerprint, String>;
}
#[cfg(all(test, unix))]
pub(crate) mod testing {
use super::*;
pub struct StubDocs;
impl DocumentBackend for StubDocs {
fn format_of(&self, path: &str) -> &'static str {
match std::path::Path::new(path)
.extension()
.and_then(|e| e.to_str())
{
Some("md") => "md",
Some("png") => "image",
_ => "unknown",
}
}
fn read(&self, _path: &str) -> Result<RawDocument, String> {
Err("not supported".into())
}
fn write(
&self,
_path: &str,
_content: &str,
_encoding: &str,
_expected_source_sha256: Option<&str>,
) -> Result<WriteDocumentResult, String> {
Err("not supported".into())
}
fn write_bytes(&self, _path: &str, _content_base64: &str) -> Result<(), String> {
Err("not supported".into())
}
fn write_raw_bytes(&self, _path: &str, _bytes: &[u8]) -> Result<(), String> {
Err("not supported".into())
}
fn copy_document(&self, _source: &str, _target: &str) -> Result<(), String> {
Err("not supported".into())
}
fn fingerprint(&self, _path: &str) -> Result<FileFingerprint, String> {
Err("not supported".into())
}
}
}