| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::path::PathBuf; |
| 7 | |
| 8 | use crate::domain::{OrgName, PasswordHash, RepoName}; |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | pub trait PasswordHasher: Send + Sync { |
| 16 | |
| 17 | fn hash(&self, plaintext: &str) -> Result<PasswordHash, PasswordError>; |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | fn verify(&self, plaintext: &str, hash: &PasswordHash) -> Result<bool, PasswordError>; |
| 25 | } |
| 26 | |
| 27 | |
| 28 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 29 | pub struct PasswordError(String); |
| 30 | |
| 31 | impl PasswordError { |
| 32 | pub fn new(message: impl Into<String>) -> Self { |
| 33 | Self(message.into()) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl std::fmt::Display for PasswordError { |
| 38 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 39 | write!(f, "password hashing failed: {}", self.0) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl std::error::Error for PasswordError {} |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | pub trait GitStorage: Send + Sync { |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | fn init_bare( |
| 65 | &self, |
| 66 | handle: &OrgName, |
| 67 | name: &RepoName, |
| 68 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | fn remove( |
| 76 | &self, |
| 77 | handle: &OrgName, |
| 78 | name: &RepoName, |
| 79 | ) -> impl Future<Output = Result<(), GitStorageError>> + Send; |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | fn repo_path(&self, handle: &OrgName, name: &RepoName) -> PathBuf; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | #[derive(Debug)] |
| 90 | pub enum GitStorageError { |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | AlreadyExists, |
| 97 | |
| 98 | Backend(Box<dyn std::error::Error + Send + Sync>), |
| 99 | } |
| 100 | |
| 101 | impl GitStorageError { |
| 102 | pub fn backend(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self { |
| 103 | Self::Backend(error.into()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl std::fmt::Display for GitStorageError { |
| 108 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 109 | match self { |
| 110 | Self::AlreadyExists => f.write_str("a repository already exists at that path"), |
| 111 | Self::Backend(error) => write!(f, "git storage failure: {error}"), |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl std::error::Error for GitStorageError { |
| 117 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
| 118 | match self { |
| 119 | Self::AlreadyExists => None, |
| 120 | Self::Backend(error) => Some(&**error), |
| 121 | } |
| 122 | } |
| 123 | } |