1
- use iroh_bytes:: HashAndFormat ;
1
+ use iroh_bytes:: { Hash , HashAndFormat } ;
2
+ use iroh_net:: ticket:: BlobTicket ;
2
3
use iroh_pkarr_naming_system:: { Record , IPNS } ;
3
- use std:: { process, str:: FromStr } ;
4
+ use std:: { fmt:: Display , process, str:: FromStr } ;
5
+
6
+ /// Various ways to specify content.
7
+ #[ derive( Debug , Clone , derive_more:: From ) ]
8
+ pub enum ContentArg {
9
+ Hash ( Hash ) ,
10
+ HashAndFormat ( HashAndFormat ) ,
11
+ Ticket ( BlobTicket ) ,
12
+ }
13
+
14
+ impl ContentArg {
15
+ /// Get the hash and format of the content.
16
+ pub fn hash_and_format ( & self ) -> HashAndFormat {
17
+ match self {
18
+ ContentArg :: Hash ( hash) => HashAndFormat :: raw ( * hash) ,
19
+ ContentArg :: HashAndFormat ( haf) => * haf,
20
+ ContentArg :: Ticket ( ticket) => HashAndFormat {
21
+ hash : ticket. hash ( ) ,
22
+ format : ticket. format ( ) ,
23
+ } ,
24
+ }
25
+ }
26
+ }
27
+
28
+ impl Display for ContentArg {
29
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
30
+ match self {
31
+ ContentArg :: Hash ( hash) => Display :: fmt ( hash, f) ,
32
+ ContentArg :: HashAndFormat ( haf) => Display :: fmt ( haf, f) ,
33
+ ContentArg :: Ticket ( ticket) => Display :: fmt ( ticket, f) ,
34
+ }
35
+ }
36
+ }
37
+
38
+ impl FromStr for ContentArg {
39
+ type Err = anyhow:: Error ;
40
+
41
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
42
+ if let Ok ( hash) = Hash :: from_str ( s) {
43
+ Ok ( hash. into ( ) )
44
+ } else if let Ok ( haf) = HashAndFormat :: from_str ( s) {
45
+ Ok ( haf. into ( ) )
46
+ } else if let Ok ( ticket) = BlobTicket :: from_str ( s) {
47
+ Ok ( ticket. into ( ) )
48
+ } else {
49
+ anyhow:: bail!( "invalid hash and format" )
50
+ }
51
+ }
52
+ }
4
53
5
54
#[ tokio:: main]
6
55
async fn main ( ) -> anyhow:: Result < ( ) > {
@@ -24,7 +73,7 @@ async fn main() -> anyhow::Result<()> {
24
73
let secret_key = iroh_net:: key:: SecretKey :: from_str ( & args[ 0 ] ) ?;
25
74
let public_key = secret_key. public ( ) ;
26
75
let zid = pkarr:: PublicKey :: try_from ( * public_key. as_bytes ( ) ) ?. to_z32 ( ) ;
27
- let content = HashAndFormat :: from_str ( & args[ 1 ] ) ?;
76
+ let content = ContentArg :: from_str ( & args[ 1 ] ) ?. hash_and_format ( ) ;
28
77
let record = Record :: Content { content } ;
29
78
let ipns = IPNS :: default ( ) ;
30
79
println ! ( "Publishing record to: {}" , public_key) ;
0 commit comments