use serde::{ser::SerializeStruct, Serialize, Serializer}; pub type Result = std::result::Result; #[derive(Debug, thiserror::Error)] pub enum Error { #[error(transparent)] Git(#[from] gigit_git::Error), #[error("no repository is open with id {0}")] UnknownRepository(String), #[error("the repository worker stopped unexpectedly")] WorkerStopped, #[error("could not start a thread for the repository")] SpawnThread(#[source] std::io::Error), } impl Error { /// A stable tag the frontend can branch on, so it never has to match on /// human-readable message text. fn kind(&self) -> &'static str { match self { Self::Git(_) => "git", Self::UnknownRepository(_) => "unknownRepository", Self::WorkerStopped => "workerStopped", Self::SpawnThread(_) => "spawnThread", } } } /// Tauri requires command errors to be serializable. Errors cross as a kind and /// a message rather than a bare string, so the UI can react to the kind and /// still have something to show. impl Serialize for Error { fn serialize(&self, serializer: S) -> std::result::Result { let mut state = serializer.serialize_struct("Error", 2)?; state.serialize_field("kind", self.kind())?; state.serialize_field("message", &self.to_string())?; state.end() } }