use crate::prelude::*; use std::ffi::OsString; use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; use crate::drive_structure::drive_id::DriveId; #[tarpc::service] pub trait GDriverService { async fn is_online() -> bool; async fn get_settings() -> StdResult; async fn get_file_by_name( name: OsString, parent: DriveId, ) -> StdResult; async fn get_file_by_path(path: PathBuf) -> StdResult; async fn write_local_change(id: DriveId) -> StdResult<(), WriteLocalChangeError>; async fn get_metadata_for_file(id: DriveId) -> StdResult<(), GetMetadataError>; async fn download_content_for_file(id: DriveId) -> StdResult<(), GetContentError>; async fn list_files_in_directory(id: DriveId) -> StdResult<(), GetFileListError>; async fn mark_file_as_deleted(id: DriveId) -> StdResult<(), MarkFileAsDeletedError>; async fn mark_file_for_keeping_local( id: DriveId, ) -> StdResult<(), MarkFileForKeepingLocalError>; async fn unmark_file_for_keeping_local( id: DriveId, ) -> StdResult<(), UnmarkFileForKeepingLocalError>; /// Returns true if the file was had remote changes and was updated async fn update_changes_for_file(id: DriveId) -> StdResult; async fn update_changes() -> StdResult<(), UpdateChangesError>; async fn do_something2(req: BackendActionRequest) -> StdResult, BackendActionError>; } #[tarpc::service] pub trait GDriverClient { async fn report_task_result(id: TaskId, result: TaskResult); } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct TaskId(pub String); #[derive(Debug, Serialize, Deserialize)] pub enum AsyncResponse { Immediate(T), Pending(TaskId), } #[derive(Debug, Serialize, Deserialize)] pub enum TaskResult { Success(String), Error(String), } #[derive(Debug, Serialize, Deserialize)] pub enum BackendActionRequest { ShutdownGracefully, UpdateChanges, Ping, RunLong, StartLong, } #[derive(Debug, Serialize, Deserialize)] pub struct GDriverSettings { metadata_path: PathBuf, cache_path: PathBuf, downloaded_path: PathBuf, } impl GDriverSettings { pub fn metadata_path(&self) -> &Path { &self.metadata_path } pub fn cache_path(&self) -> &Path { &self.cache_path } pub fn downloaded_path(&self) -> &Path { &self.downloaded_path } pub fn get_metadata_file_path(&self, id: &DriveId) -> PathBuf { self.metadata_path.join(id.as_ref()).with_extension("meta") } pub fn get_downloaded_file_path(&self, id: &DriveId) -> PathBuf { self.downloaded_path.join(id.as_ref()) } pub fn get_cache_file_path(&self, id: &DriveId) -> PathBuf { self.cache_path.join(id.as_ref()) } } impl Default for GDriverSettings { fn default() -> Self { let p = directories::ProjectDirs::from("com", "OMGeeky", "gdriver2").expect( "Getting the Project dir needs to work (on all platforms) otherwise nothing will work as expected. \ This is where all files will be stored, so there is not much use for this app without it.", ); Self { metadata_path: p.data_dir().join("meta"), downloaded_path: p.data_dir().join("downloads"), cache_path: p.cache_dir().to_path_buf(), } } } use errors::*; pub mod errors { use super::*; use std::error::Error; use std::fmt::{Display, Formatter}; #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum GDriverServiceError { #[error("Error getting the settings: {0}")] GetSettings(#[from] GetSettingsError), #[error("Backend Action had an Error: {0}")] BackendAction(#[from] BackendActionError), #[error("Could not get File by Path: {0}")] GetFileByPath(#[from] GetFileByPathError), #[error("Could not update changes: {0}")] UpdateChanges(#[from] UpdateChangesError), #[error("Could not write local change: {0}")] WriteLocalChange(#[from] WriteLocalChangeError), #[error("Could not get metadata: {0}")] GetMetadata(#[from] GetMetadataError), #[error("Could not get content: {0}")] GetContent(#[from] GetContentError), #[error("Could not get file list: {0}")] GetFileList(#[from] GetFileListError), #[error("Could not mark file as deleted: {0}")] MarkFileAsDeleted(#[from] MarkFileAsDeletedError), #[error("Could not mark file for keeping: {0}")] MarkFileForKeepingLocal(#[from] MarkFileForKeepingLocalError), #[error("Could not unmark file for keeping: {0}")] UnmarkFileForKeepingLocal(#[from] UnmarkFileForKeepingLocalError), } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum GetSettingsError { #[error("Unknown Error getting the settings")] Unknown, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum BackendActionError { #[error("Unknown Error")] Unknown, #[error("Could not complete Error")] CouldNotComplete, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum GetFileByPathError { #[error("Other")] Other, #[error("Google drive error {0:?}")] GoogleDrive(#[from] GoogleDrive3Error), // GoogleDrive, #[error("Could not find any file by that name und the specified parent")] NotFound, #[error("The drive returned invalid data")] InvalidData, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub struct GoogleDrive3Error { message: String, dbg_message: String, } impl From for GoogleDrive3Error { fn from(value: google_drive3::Error) -> Self { let message = value.to_string(); let dbg_message = format!("{:?}", value); Self { message, dbg_message, } } } impl Display for GoogleDrive3Error { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "Google Drive Error") } } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum UpdateChangesError {} #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum WriteLocalChangeError { #[error("Remote has changed")] RemoteChanged, #[error("Unknown Id")] UnknownId, #[error("Not Allowed")] NotAllowed, #[error("Other")] Other, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum GetMetadataError { #[error("Other")] Other, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum GetContentError { #[error("Other")] Other, } //#[derive(Debug, Serialize, Deserialize)] //pub enum GetContentError {} #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum GetFileListError { #[error("Other")] Other, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum MarkFileAsDeletedError { #[error("Other")] Other, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum MarkFileForKeepingLocalError { #[error("Other")] Other, } #[derive(Debug, Serialize, Deserialize, thiserror::Error)] pub enum UnmarkFileForKeepingLocalError { #[error("Other")] Other, } }