-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use struct and impl for package family name
- Loading branch information
1 parent
5eaad75
commit b7d6323
Showing
5 changed files
with
168 additions
and
26 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use core::fmt; | ||
use core::fmt::Display; | ||
use core::str::FromStr; | ||
|
||
const PUBLISHER_ID_LENGTH: usize = 13; | ||
|
||
/// A 13-character long publisher ID | ||
/// | ||
/// [Publisher Id](https://learn.microsoft.com/windows/apps/desktop/modernize/package-identity-overview#publisher-id) | ||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)] | ||
pub struct PublisherId(pub heapless::String<PUBLISHER_ID_LENGTH>); | ||
|
||
impl Display for PublisherId { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.0) | ||
} | ||
} | ||
|
||
impl FromStr for PublisherId { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if s.len() != PUBLISHER_ID_LENGTH { | ||
return Err("expected publisher id length of 13"); | ||
} | ||
Ok(PublisherId(heapless::String::from_str(s).unwrap())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::publisher_id::PublisherId; | ||
use core::str::FromStr; | ||
|
||
#[test] | ||
fn test_publisher_id_from_str() { | ||
assert!(PublisherId::from_str("zj75k085cmj1a").is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_publisher_id_too_short() { | ||
assert!(PublisherId::from_str(&"1".repeat(3)).is_err()); | ||
} | ||
|
||
#[test] | ||
fn test_publisher_id_too_long() { | ||
assert!(PublisherId::from_str(&"1".repeat(20)).is_err()); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::publisher_id::PublisherId; | ||
use crate::PackageFamilyName; | ||
use alloc::borrow::ToOwned; | ||
use alloc::string::ToString; | ||
use core::str::FromStr; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
impl<'identity> Serialize for PackageFamilyName { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&self.to_string()) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for PackageFamilyName { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let deserialized_name = <&str>::deserialize(deserializer)?; | ||
let (identity_name, identity_id) = | ||
deserialized_name | ||
.split_once('_') | ||
.ok_or(serde::de::Error::custom( | ||
"Invalid format for PackageFamilyName", | ||
))?; | ||
let publisher_id = PublisherId::from_str(identity_id).map_err(serde::de::Error::custom)?; | ||
Ok(PackageFamilyName { | ||
identity_name: identity_name.to_owned(), | ||
publisher_id, | ||
}) | ||
} | ||
} | ||
|
||
impl Serialize for PublisherId { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&self.0) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for PublisherId { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let deserialized_id = <&str>::deserialize(deserializer)?; | ||
PublisherId::from_str(deserialized_id).map_err(serde::de::Error::custom) | ||
} | ||
} |