1+ pub use crate :: PackageId ;
12use crate :: ProcessIdParseError ;
23use serde:: { Deserialize , Serialize } ;
3- use std:: hash:: Hash ;
4-
5- /// PackageId is like a ProcessId, but for a package. Only contains the name
6- /// of the package and the name of the publisher.
7- #[ derive( Hash , Eq , PartialEq , Debug , Clone , Serialize , Deserialize ) ]
8- pub struct PackageId {
9- package_name : String ,
10- publisher_node : String ,
11- }
124
5+ /// `PackageId` is defined in the wit bindings, but constructors and methods
6+ /// are defined here. A `PackageId` contains a package name and a publisher node ID.
137impl PackageId {
148 /// Create a new `PackageId`.
159 pub fn new ( package_name : & str , publisher_node : & str ) -> Self {
@@ -31,6 +25,32 @@ impl PackageId {
3125 }
3226}
3327
28+ impl Serialize for PackageId {
29+ fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
30+ where
31+ S : serde:: ser:: Serializer ,
32+ {
33+ format ! ( "{}" , self ) . serialize ( serializer)
34+ }
35+ }
36+
37+ impl < ' a > Deserialize < ' a > for PackageId {
38+ fn deserialize < D > ( deserializer : D ) -> Result < PackageId , D :: Error >
39+ where
40+ D : serde:: de:: Deserializer < ' a > ,
41+ {
42+ let s = String :: deserialize ( deserializer) ?;
43+ s. parse ( ) . map_err ( serde:: de:: Error :: custom)
44+ }
45+ }
46+
47+ impl std:: hash:: Hash for PackageId {
48+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
49+ self . package_name . hash ( state) ;
50+ self . publisher_node . hash ( state) ;
51+ }
52+ }
53+
3454impl std:: str:: FromStr for PackageId {
3555 type Err = ProcessIdParseError ;
3656 /// Attempt to parse a `PackageId` from a string. The string must
@@ -69,3 +89,9 @@ impl std::fmt::Display for PackageId {
6989 write ! ( f, "{}:{}" , self . package_name, self . publisher_node)
7090 }
7191}
92+
93+ impl PartialEq for PackageId {
94+ fn eq ( & self , other : & Self ) -> bool {
95+ self . package_name == other. package_name && self . publisher_node == other. publisher_node
96+ }
97+ }
0 commit comments