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,25 @@ 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+
3447impl std:: str:: FromStr for PackageId {
3548 type Err = ProcessIdParseError ;
3649 /// Attempt to parse a `PackageId` from a string. The string must
@@ -64,8 +77,29 @@ impl std::str::FromStr for PackageId {
6477 }
6578}
6679
80+ impl std:: hash:: Hash for PackageId {
81+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
82+ self . package_name . hash ( state) ;
83+ self . publisher_node . hash ( state) ;
84+ }
85+ }
86+
6787impl std:: fmt:: Display for PackageId {
6888 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
6989 write ! ( f, "{}:{}" , self . package_name, self . publisher_node)
7090 }
7191}
92+
93+ impl From < ( & str , & str ) > for PackageId {
94+ fn from ( input : ( & str , & str ) ) -> Self {
95+ PackageId :: new ( input. 0 , input. 1 )
96+ }
97+ }
98+
99+ impl std:: cmp:: Eq for PackageId { }
100+
101+ impl PartialEq for PackageId {
102+ fn eq ( & self , other : & Self ) -> bool {
103+ self . package_name == other. package_name && self . publisher_node == other. publisher_node
104+ }
105+ }
0 commit comments