rightkit_document_workspace/
documents.rs1use serde::Serialize;
5
6#[derive(Debug, Serialize)]
7pub struct RawDocument {
8 pub path: String,
9 pub content: String,
10 pub last_view: Option<String>,
11 pub view_meta: Option<String>,
13 pub lossy: bool,
16 pub encoding: String,
18 pub mtime_ms: i64,
20 pub size: i64,
22 pub source_sha256: Option<String>,
24 pub stream_token: Option<String>,
26}
27
28#[derive(Debug, Serialize)]
31pub struct FileFingerprint {
32 pub mtime_ms: i64,
33 pub size: i64,
34}
35
36#[derive(Debug, Serialize)]
37pub struct WriteDocumentResult {
38 pub encoding: String,
39 pub source_sha256: String,
40}
41
42pub trait DocumentBackend {
45 fn format_of(&self, path: &str) -> &'static str;
47 fn read(&self, path: &str) -> Result<RawDocument, String>;
49 fn write(
51 &self,
52 path: &str,
53 content: &str,
54 encoding: &str,
55 expected_source_sha256: Option<&str>,
56 ) -> Result<WriteDocumentResult, String>;
57 fn write_bytes(&self, path: &str, content_base64: &str) -> Result<(), String>;
59 fn write_raw_bytes(&self, path: &str, bytes: &[u8]) -> Result<(), String>;
61 fn copy_document(&self, source: &str, target: &str) -> Result<(), String>;
63 fn fingerprint(&self, path: &str) -> Result<FileFingerprint, String>;
65}
66
67#[cfg(all(test, unix))]
68pub(crate) mod testing {
69 use super::*;
70
71 pub struct StubDocs;
73
74 impl DocumentBackend for StubDocs {
75 fn format_of(&self, path: &str) -> &'static str {
76 match std::path::Path::new(path)
77 .extension()
78 .and_then(|e| e.to_str())
79 {
80 Some("md") => "md",
81 Some("png") => "image",
82 _ => "unknown",
83 }
84 }
85 fn read(&self, _path: &str) -> Result<RawDocument, String> {
86 Err("not supported".into())
87 }
88 fn write(
89 &self,
90 _path: &str,
91 _content: &str,
92 _encoding: &str,
93 _expected_source_sha256: Option<&str>,
94 ) -> Result<WriteDocumentResult, String> {
95 Err("not supported".into())
96 }
97 fn write_bytes(&self, _path: &str, _content_base64: &str) -> Result<(), String> {
98 Err("not supported".into())
99 }
100 fn write_raw_bytes(&self, _path: &str, _bytes: &[u8]) -> Result<(), String> {
101 Err("not supported".into())
102 }
103 fn copy_document(&self, _source: &str, _target: &str) -> Result<(), String> {
104 Err("not supported".into())
105 }
106 fn fingerprint(&self, _path: &str) -> Result<FileFingerprint, String> {
107 Err("not supported".into())
108 }
109 }
110}