From bb936aaf0fc3a74b32c1bf8f3bebd7993bd03049 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Tue, 26 Apr 2022 03:29:40 -0700 Subject: [PATCH] Make Platform a newtype Summary: Just a bit of extra type safety. The previous diff caused a bit of a regression by making things stringly-typed. Reviewed By: lblasa Differential Revision: D35902107 fbshipit-source-id: a599106f899ec3d205663b5791420aee29f3eeae --- packer/src/main.rs | 4 ++-- packer/src/types.rs | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packer/src/main.rs b/packer/src/main.rs index 931836d6c..1cdd58dae 100644 --- a/packer/src/main.rs +++ b/packer/src/main.rs @@ -178,7 +178,7 @@ fn pack_platform_glob( let full_path = path::Path::new(&base_dir).join(&path); if !full_path.exists() { bail!(error::Error::MissingPackFile( - platform.to_string(), + platform.clone(), pack_type, full_path, )); @@ -204,7 +204,7 @@ fn pack_platform_exact( let full_path = path::Path::new(&base_dir).join(f); if !full_path.exists() { bail!(error::Error::MissingPackFile( - platform.to_string(), + platform.clone(), pack_type, full_path, )); diff --git a/packer/src/types.rs b/packer/src/types.rs index c88ca662b..ce4656e78 100644 --- a/packer/src/types.rs +++ b/packer/src/types.rs @@ -8,8 +8,22 @@ use std::fmt::{self, Display}; use std::str; -// TODO: Make this a newtype. -pub type Platform = String; +#[derive(Eq, PartialEq, Debug, PartialOrd, Ord, Clone, serde::Deserialize)] +pub struct Platform(pub String); + +impl str::FromStr for Platform { + type Err = &'static str; + + fn from_str(s: &str) -> Result { + Ok(Platform(s.to_string())) + } +} + +impl Display for Platform { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} #[derive( Debug,