Refactor and add new enum PkgFmtDecomposed

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-11 18:40:21 +10:00
parent c33f195d5f
commit bd68613448
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
3 changed files with 78 additions and 29 deletions

74
src/fmt.rs Normal file
View file

@ -0,0 +1,74 @@
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString, EnumVariantNames};
/// Binary format enumeration
#[derive(
Debug, Copy, Clone, PartialEq, Serialize, Deserialize, Display, EnumString, EnumVariantNames,
)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum PkgFmt {
/// Download format is TAR (uncompressed)
Tar,
/// Download format is TGZ (TAR + GZip)
Tgz,
/// Download format is TAR + XZ
Txz,
/// Download format is TAR + Zstd
Tzstd,
/// Download format is Zip
Zip,
/// Download format is raw / binary
Bin,
}
impl Default for PkgFmt {
fn default() -> Self {
Self::Tgz
}
}
impl PkgFmt {
/// If self is one of the tar based formats,
/// return Some.
pub fn decompose(self) -> PkgFmtDecomposed {
match self {
PkgFmt::Tar => PkgFmtDecomposed::Tar(TarBasedFmt::Tar),
PkgFmt::Tgz => PkgFmtDecomposed::Tar(TarBasedFmt::Tgz),
PkgFmt::Txz => PkgFmtDecomposed::Tar(TarBasedFmt::Txz),
PkgFmt::Tzstd => PkgFmtDecomposed::Tar(TarBasedFmt::Tzstd),
PkgFmt::Bin => PkgFmtDecomposed::Bin,
PkgFmt::Zip => PkgFmtDecomposed::Zip,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PkgFmtDecomposed {
Tar(TarBasedFmt),
Bin,
Zip,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum TarBasedFmt {
/// Download format is TAR (uncompressed)
Tar,
/// Download format is TGZ (TAR + GZip)
Tgz,
/// Download format is TAR + XZ
Txz,
/// Download format is TAR + Zstd
Tzstd,
}
impl From<TarBasedFmt> for PkgFmt {
fn from(fmt: TarBasedFmt) -> Self {
match fmt {
TarBasedFmt::Tar => PkgFmt::Tar,
TarBasedFmt::Tgz => PkgFmt::Tgz,
TarBasedFmt::Txz => PkgFmt::Txz,
TarBasedFmt::Tzstd => PkgFmt::Tzstd,
}
}
}

View file

@ -12,7 +12,7 @@ use tokio::{
}; };
use super::{extracter::*, readable_rx::*}; use super::{extracter::*, readable_rx::*};
use crate::{BinstallError, PkgFmt}; use crate::{BinstallError, PkgFmt, TarBasedFmt};
pub(crate) enum Content { pub(crate) enum Content {
/// Data to write to file /// Data to write to file

View file

@ -1,7 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString, EnumVariantNames};
pub mod drivers; pub mod drivers;
pub use drivers::*; pub use drivers::*;
@ -18,6 +17,9 @@ pub mod fetchers;
mod target; mod target;
pub use target::*; pub use target::*;
mod fmt;
pub use fmt::*;
/// Default package path template (may be overridden in package Cargo.toml) /// Default package path template (may be overridden in package Cargo.toml)
pub const DEFAULT_PKG_URL: &str = pub const DEFAULT_PKG_URL: &str =
"{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.{ archive-format }"; "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.{ archive-format }";
@ -25,33 +27,6 @@ pub const DEFAULT_PKG_URL: &str =
/// Default binary name template (may be overridden in package Cargo.toml) /// Default binary name template (may be overridden in package Cargo.toml)
pub const DEFAULT_BIN_DIR: &str = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }"; pub const DEFAULT_BIN_DIR: &str = "{ name }-{ target }-v{ version }/{ bin }{ binary-ext }";
/// Binary format enumeration
#[derive(
Debug, Copy, Clone, PartialEq, Serialize, Deserialize, Display, EnumString, EnumVariantNames,
)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum PkgFmt {
/// Download format is TAR (uncompressed)
Tar,
/// Download format is TGZ (TAR + GZip)
Tgz,
/// Download format is TAR + XZ
Txz,
/// Download format is TAR + Zstd
Tzstd,
/// Download format is Zip
Zip,
/// Download format is raw / binary
Bin,
}
impl Default for PkgFmt {
fn default() -> Self {
Self::Tgz
}
}
/// `binstall` metadata container /// `binstall` metadata container
/// ///
/// Required to nest metadata under `package.metadata.binstall` /// Required to nest metadata under `package.metadata.binstall`