From a58150cc9ca982d3b45375ce6963300cac24d24d Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 4 Apr 2023 20:45:43 -0400 Subject: [PATCH] Add `arch` field to `Package` --- src/package/arch.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/package/mod.rs | 7 ++++-- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/package/arch.rs diff --git a/src/package/arch.rs b/src/package/arch.rs new file mode 100644 index 0000000..cde2d3c --- /dev/null +++ b/src/package/arch.rs @@ -0,0 +1,56 @@ +use serde::{Deserialize, Serialize}; +use std::{fmt, str::FromStr, error::Error}; + +#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Serialize)] +#[allow(non_camel_case_types)] +pub enum Arch { + armv7l, + aarch64, + i486, + i586, + i686, + riscv64, + #[default] + x86_64, +} + +impl fmt::Display for Arch { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", match self { + Self::armv7l => "armv7l", + Self::aarch64 => "aarch64", + Self::i486 => "i486", + Self::i586 => "i586", + Self::i686 => "i686", + Self::riscv64 => "riscv64", + Self::x86_64 => "x86_64", + }) + } +} + +#[derive(Debug)] +pub struct ParseArchError; + +impl fmt::Display for ParseArchError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "error parsing architecture") + } +} + +impl Error for ParseArchError {} + +impl FromStr for Arch { + type Err = ParseArchError; + + fn from_str(s: &str) -> Result { + match s { + "i486" | "x86" => Ok(Self::i486), + "i586" => Ok(Self::i586), + "i686" => Ok(Self::i686), + "armv7l" | "arm" => Ok(Self::armv7l), + "arm64" | "aarch64" | "armv8" => Ok(Self::aarch64), + "riscv" | "riscv64" => Ok(Self::riscv64), + _ => Err(ParseArchError) + } + } +} diff --git a/src/package/mod.rs b/src/package/mod.rs index c706274..06e5ae3 100644 --- a/src/package/mod.rs +++ b/src/package/mod.rs @@ -1,3 +1,4 @@ +mod arch; mod dependency; mod specs; @@ -15,7 +16,7 @@ use { crate::tar::{Node, Owner}, }; -pub use {dependency::Dependency, specs::Specs}; +pub use {arch::Arch, dependency::Dependency, specs::Specs}; #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct User { @@ -36,6 +37,8 @@ pub struct Package { pub name: String, /// The `Version` of the package pub version: Version, + /// The architecture the package is built for + pub arch: Arch, /// The release number for this package pub release: u8, /// a single line description of the package @@ -110,7 +113,7 @@ impl Package { /// Returns the formatted full package name including version and release strings pub fn fullname(&self) -> String { - format!("{}-{}_{}", self.name, self.version, self.release) + format!("{}-{}_{}{}", self.name, self.version, self.arch, self.release) } /// Tests whether this package is an update for another