-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Replace dyn Trait with Enums (#1462)
Addresses part of #667 This PR refactors the codebase to replace the use of dyn Trait with enums for Sender, Archiver, State, Storage, and ConfigProvider
- Loading branch information
Showing
89 changed files
with
780 additions
and
582 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,96 @@ | ||
pub mod disk; | ||
pub mod s3; | ||
|
||
use crate::configs::server::{DiskArchiverConfig, S3ArchiverConfig}; | ||
use crate::server_error::ArchiverError; | ||
use async_trait::async_trait; | ||
use derive_more::Display; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::{Debug, Formatter}; | ||
use std::fmt::Debug; | ||
use std::future::Future; | ||
use std::str::FromStr; | ||
|
||
use crate::archiver::disk::DiskArchiver; | ||
use crate::archiver::s3::S3Archiver; | ||
|
||
pub const COMPONENT: &str = "ARCHIVER"; | ||
|
||
#[derive(Debug, Serialize, Deserialize, PartialEq, Default, Display, Copy, Clone)] | ||
#[serde(rename_all = "lowercase")] | ||
pub enum ArchiverKind { | ||
pub enum ArchiverKindType { | ||
#[default] | ||
#[display("disk")] | ||
Disk, | ||
#[display("s3")] | ||
S3, | ||
} | ||
|
||
impl FromStr for ArchiverKind { | ||
impl FromStr for ArchiverKindType { | ||
type Err = String; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s.to_lowercase().as_str() { | ||
"disk" => Ok(ArchiverKind::Disk), | ||
"s3" => Ok(ArchiverKind::S3), | ||
"disk" => Ok(ArchiverKindType::Disk), | ||
"s3" => Ok(ArchiverKindType::S3), | ||
_ => Err(format!("Unknown archiver kind: {}", s)), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
pub trait Archiver: Sync + Send { | ||
async fn init(&self) -> Result<(), ArchiverError>; | ||
async fn is_archived( | ||
pub trait Archiver: Send { | ||
fn init(&self) -> impl Future<Output = Result<(), ArchiverError>> + Send; | ||
fn is_archived( | ||
&self, | ||
file: &str, | ||
base_directory: Option<String>, | ||
) -> Result<bool, ArchiverError>; | ||
async fn archive( | ||
) -> impl Future<Output = Result<bool, ArchiverError>> + Send; | ||
fn archive( | ||
&self, | ||
files: &[&str], | ||
base_directory: Option<String>, | ||
) -> Result<(), ArchiverError>; | ||
) -> impl Future<Output = Result<(), ArchiverError>> + Send; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ArchiverKind { | ||
Disk(DiskArchiver), | ||
S3(S3Archiver), | ||
} | ||
|
||
impl Debug for dyn Archiver { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "Archiver") | ||
impl ArchiverKind { | ||
pub fn get_disk_arhiver(config: DiskArchiverConfig) -> Self { | ||
Self::Disk(DiskArchiver::new(config)) | ||
} | ||
|
||
pub fn get_s3_archiver(config: S3ArchiverConfig) -> Result<Self, ArchiverError> { | ||
let archiver = S3Archiver::new(config)?; | ||
Ok(Self::S3(archiver)) | ||
} | ||
|
||
pub async fn init(&self) -> Result<(), ArchiverError> { | ||
match self { | ||
Self::Disk(a) => a.init().await, | ||
Self::S3(a) => a.init().await, | ||
} | ||
} | ||
|
||
pub async fn is_archived( | ||
&self, | ||
file: &str, | ||
base_directory: Option<String>, | ||
) -> Result<bool, ArchiverError> { | ||
match self { | ||
Self::Disk(d) => d.is_archived(file, base_directory).await, | ||
Self::S3(d) => d.is_archived(file, base_directory).await, | ||
} | ||
} | ||
|
||
pub async fn archive( | ||
&self, | ||
files: &[&str], | ||
base_directory: Option<String>, | ||
) -> Result<(), ArchiverError> { | ||
match self { | ||
Self::Disk(d) => d.archive(files, base_directory).await, | ||
Self::S3(d) => d.archive(files, base_directory).await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.