use {
crate::{
makepad_live_id::*,
makepad_micro_serde::{SerBin, DeBin, DeBinErr},
},
};
#[derive(Clone, Debug, SerBin, DeBin)]
pub enum FileRequest {
LoadFileTree{ with_data: bool },
OpenFile{
path: String,
id: u64
},
CreateSnapshot{
root: String,
message: String,
},
LoadSnapshotImage{
root: String,
hash: String,
},
LoadSnapshot{
root: String,
hash: String,
},
SaveSnapshotImage{
root: String,
hash: String,
data: Vec<u8>,
},
SaveFile{
path: String,
data: String,
id: u64,
patch: bool
},
Search{
id: u64,
set: Vec<SearchItem>
}
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct SearchItem{
pub needle: String,
pub prefixes: Option<Vec<String>>,
pub pre_word_boundary: bool,
pub post_word_boundary: bool
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub enum FileClientMessage {
Response(FileResponse),
Notification(FileNotification),
}
#[derive(Clone, Debug, SerBin, DeBin, PartialEq)]
pub enum SaveKind{
Save,
Patch,
Observation
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct SaveFileResponse{
pub path: String,
pub old_data: String,
pub new_data: String,
pub kind: SaveKind,
pub id: u64,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct OpenFileResponse{
pub path: String,
pub data: String,
pub id: u64,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct LoadSnapshotImageResponse{
pub root: String,
pub hash: String,
pub data: Vec<u8>,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct LoadSnapshotImageError{
pub root: String,
pub hash: String,
pub error: FileError,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct SaveSnapshotImageResponse{
pub root: String,
pub hash: String,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct CreateSnapshotResponse{
pub root: String,
pub hash: String,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct LoadSnapshotResponse{
pub root: String,
pub hash: String,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct CreateSnapshotError{
pub root: String,
pub error:String,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct LoadSnapshotError{
pub root: String,
pub error:String,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub enum FileResponse {
LoadFileTree(Result<FileTreeData, FileError>),
OpenFile(Result<OpenFileResponse, FileError>),
SaveFile(Result<SaveFileResponse, FileError>),
LoadSnapshotImage(Result<LoadSnapshotImageResponse, LoadSnapshotImageError>),
SaveSnapshotImage(Result<SaveSnapshotImageResponse, FileError>),
CreateSnapshot(Result<CreateSnapshotResponse, CreateSnapshotError>),
LoadSnapshot(Result<LoadSnapshotResponse, LoadSnapshotError>),
SearchInProgress(u64)
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct FileTreeData {
pub root_path: String,
pub root: FileNodeData,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct GitLog{
pub root: String,
pub commits: Vec<GitCommit>,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct GitCommit {
pub hash: String,
pub message: String,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub enum FileNodeData {
Directory { git_log: Option<GitLog>, entries: Vec<DirectoryEntry> },
File { data: Option<Vec<u8>> },
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct DirectoryEntry {
pub name: String,
pub node: FileNodeData,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub struct SearchResult{
pub file_name: String,
pub line: usize,
pub column_byte: usize,
pub result_line: String,
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub enum FileNotification {
FileChangedOnDisk(SaveFileResponse),
SearchResults{
id: u64,
results: Vec<SearchResult>
}
}
#[derive(Clone, Debug, SerBin, DeBin)]
pub enum FileError {
Unknown(String),
RootNotFound(String),
CannotOpen(String),
}
#[derive(Clone, Debug, Default, Eq, Hash, Copy, PartialEq, FromLiveId)]
pub struct TextFileId(pub LiveId);
impl SerBin for TextFileId {
fn ser_bin(&self, s: &mut Vec<u8>) {
self.0.0.ser_bin(s);
}
}
impl DeBin for TextFileId {
fn de_bin(o: &mut usize, d: &[u8]) -> Result<Self, DeBinErr> {
Ok(TextFileId(LiveId(DeBin::de_bin(o, d)?)))
}
}