@@ -18,17 +18,17 @@ use indicatif::{HumanBytes, HumanDuration, MultiProgress, ProgressBar, ProgressS
18
18
use iroh:: {
19
19
base:: { base32:: fmt_short, node_addr:: AddrInfoOptions } ,
20
20
blobs:: { provider:: AddProgress , util:: SetTagOption , Hash , Tag } ,
21
- client:: {
22
- blobs:: WrapOption ,
23
- docs:: { Doc , Entry , LiveEvent , Origin , ShareMode } ,
24
- Iroh ,
25
- } ,
21
+ client:: { blobs:: WrapOption , Doc , Iroh } ,
26
22
docs:: {
27
23
store:: { DownloadPolicy , FilterKind , Query , SortDirection } ,
28
24
AuthorId , DocTicket , NamespaceId ,
29
25
} ,
30
26
util:: fs:: { path_content_info, path_to_key, PathContent } ,
31
27
} ;
28
+ use iroh_docs:: {
29
+ engine:: Origin ,
30
+ rpc:: client:: docs:: { Entry , LiveEvent , ShareMode } ,
31
+ } ;
32
32
use tokio:: io:: AsyncReadExt ;
33
33
34
34
use crate :: config:: ConsoleEnv ;
@@ -414,7 +414,7 @@ impl DocCommands {
414
414
415
415
let mut stream = doc. get_many ( query) . await ?;
416
416
while let Some ( entry) = stream. try_next ( ) . await ? {
417
- println ! ( "{}" , fmt_entry( & doc , & entry, mode) . await ) ;
417
+ println ! ( "{}" , fmt_entry( & iroh . blobs ( ) , & entry, mode) . await ) ;
418
418
}
419
419
}
420
420
Self :: Keys {
@@ -440,7 +440,7 @@ impl DocCommands {
440
440
query = query. sort_by ( sort. into ( ) , direction) ;
441
441
let mut stream = doc. get_many ( query) . await ?;
442
442
while let Some ( entry) = stream. try_next ( ) . await ? {
443
- println ! ( "{}" , fmt_entry( & doc , & entry, mode) . await ) ;
443
+ println ! ( "{}" , fmt_entry( & iroh . blobs ( ) , & entry, mode) . await ) ;
444
444
}
445
445
}
446
446
Self :: Leave { doc } => {
@@ -516,7 +516,7 @@ impl DocCommands {
516
516
}
517
517
Some ( e) => e,
518
518
} ;
519
- match entry. content_reader ( & doc ) . await {
519
+ match iroh . blobs ( ) . read ( entry. content_hash ( ) ) . await {
520
520
Ok ( mut content) => {
521
521
if let Some ( dir) = path. parent ( ) {
522
522
if let Err ( err) = std:: fs:: create_dir_all ( dir) {
@@ -547,13 +547,14 @@ impl DocCommands {
547
547
Self :: Watch { doc } => {
548
548
let doc = get_doc ( iroh, env, doc) . await ?;
549
549
let mut stream = doc. subscribe ( ) . await ?;
550
+ let blobs = iroh. blobs ( ) ;
550
551
while let Some ( event) = stream. next ( ) . await {
551
552
let event = event?;
552
553
match event {
553
554
LiveEvent :: InsertLocal { entry } => {
554
555
println ! (
555
556
"local change: {}" ,
556
- fmt_entry( & doc , & entry, DisplayContentMode :: Auto ) . await
557
+ fmt_entry( & blobs , & entry, DisplayContentMode :: Auto ) . await
557
558
)
558
559
}
559
560
LiveEvent :: InsertRemote {
@@ -563,17 +564,17 @@ impl DocCommands {
563
564
} => {
564
565
let content = match content_status {
565
566
iroh:: docs:: ContentStatus :: Complete => {
566
- fmt_entry ( & doc , & entry, DisplayContentMode :: Auto ) . await
567
+ fmt_entry ( & blobs , & entry, DisplayContentMode :: Auto ) . await
567
568
}
568
569
iroh:: docs:: ContentStatus :: Incomplete => {
569
570
let ( Ok ( content) | Err ( content) ) =
570
- fmt_content ( & doc , & entry, DisplayContentMode :: ShortHash )
571
+ fmt_content ( & blobs , & entry, DisplayContentMode :: ShortHash )
571
572
. await ;
572
573
format ! ( "<incomplete: {} ({})>" , content, human_len( & entry) )
573
574
}
574
575
iroh:: docs:: ContentStatus :: Missing => {
575
576
let ( Ok ( content) | Err ( content) ) =
576
- fmt_content ( & doc , & entry, DisplayContentMode :: ShortHash )
577
+ fmt_content ( & blobs , & entry, DisplayContentMode :: ShortHash )
577
578
. await ;
578
579
format ! ( "<missing: {} ({})>" , content, human_len( & entry) )
579
580
}
@@ -679,14 +680,19 @@ impl DocCommands {
679
680
680
681
/// Gets the document given the client, the environment (and maybe the [`NamespaceID`]).
681
682
async fn get_doc ( iroh : & Iroh , env : & ConsoleEnv , id : Option < NamespaceId > ) -> anyhow:: Result < Doc > {
683
+ let doc_id = env. doc ( id) ?;
682
684
iroh. docs ( )
683
- . open ( env . doc ( id ) ? )
685
+ . open ( doc_id )
684
686
. await ?
685
687
. context ( "Document not found" )
686
688
}
687
689
688
690
/// Formats the content. If an error occurs it's returned in a formatted, friendly way.
689
- async fn fmt_content ( doc : & Doc , entry : & Entry , mode : DisplayContentMode ) -> Result < String , String > {
691
+ async fn fmt_content (
692
+ blobs : & iroh:: client:: blobs:: Client ,
693
+ entry : & Entry ,
694
+ mode : DisplayContentMode ,
695
+ ) -> Result < String , String > {
690
696
let read_failed = |err : anyhow:: Error | format ! ( "<failed to get content: {err}>" ) ;
691
697
let encode_hex = |err : std:: string:: FromUtf8Error | format ! ( "0x{}" , hex:: encode( err. as_bytes( ) ) ) ;
692
698
let as_utf8 = |buf : Vec < u8 > | String :: from_utf8 ( buf) . map ( |repr| format ! ( "\" {repr}\" " ) ) ;
@@ -695,11 +701,17 @@ async fn fmt_content(doc: &Doc, entry: &Entry, mode: DisplayContentMode) -> Resu
695
701
DisplayContentMode :: Auto => {
696
702
if entry. content_len ( ) < MAX_DISPLAY_CONTENT_LEN {
697
703
// small content: read fully as UTF-8
698
- let bytes = entry. content_bytes ( doc) . await . map_err ( read_failed) ?;
704
+ let bytes = blobs
705
+ . read_to_bytes ( entry. content_hash ( ) )
706
+ . await
707
+ . map_err ( read_failed) ?;
699
708
Ok ( as_utf8 ( bytes. into ( ) ) . unwrap_or_else ( encode_hex) )
700
709
} else {
701
710
// large content: read just the first part as UTF-8
702
- let mut blob_reader = entry. content_reader ( doc) . await . map_err ( read_failed) ?;
711
+ let mut blob_reader = blobs
712
+ . read ( entry. content_hash ( ) )
713
+ . await
714
+ . map_err ( read_failed) ?;
703
715
let mut buf = Vec :: with_capacity ( MAX_DISPLAY_CONTENT_LEN as usize + 5 ) ;
704
716
705
717
blob_reader
@@ -714,7 +726,10 @@ async fn fmt_content(doc: &Doc, entry: &Entry, mode: DisplayContentMode) -> Resu
714
726
}
715
727
DisplayContentMode :: Content => {
716
728
// read fully as UTF-8
717
- let bytes = entry. content_bytes ( doc) . await . map_err ( read_failed) ?;
729
+ let bytes = blobs
730
+ . read_to_bytes ( entry. content_hash ( ) )
731
+ . await
732
+ . map_err ( read_failed) ?;
718
733
Ok ( as_utf8 ( bytes. into ( ) ) . unwrap_or_else ( encode_hex) )
719
734
}
720
735
DisplayContentMode :: ShortHash => {
@@ -735,12 +750,16 @@ fn human_len(entry: &Entry) -> HumanBytes {
735
750
736
751
/// Formats an entry for display as a `String`.
737
752
#[ must_use = "this won't be printed, you need to print it yourself" ]
738
- async fn fmt_entry ( doc : & Doc , entry : & Entry , mode : DisplayContentMode ) -> String {
753
+ async fn fmt_entry (
754
+ blobs : & iroh:: client:: blobs:: Client ,
755
+ entry : & Entry ,
756
+ mode : DisplayContentMode ,
757
+ ) -> String {
739
758
let key = std:: str:: from_utf8 ( entry. key ( ) )
740
759
. unwrap_or ( "<bad key>" )
741
760
. bold ( ) ;
742
761
let author = fmt_short ( entry. author ( ) ) ;
743
- let ( Ok ( content) | Err ( content) ) = fmt_content ( doc , entry, mode) . await ;
762
+ let ( Ok ( content) | Err ( content) ) = fmt_content ( blobs , entry, mode) . await ;
744
763
let len = human_len ( entry) ;
745
764
format ! ( "@{author}: {key} = {content} ({len})" )
746
765
}
0 commit comments