chiark / gitweb /
progress: Reowrk, introducing ResponseReporter
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 May 2021 01:48:49 +0000 (02:48 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 12 May 2021 01:48:49 +0000 (02:48 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bundles.rs
src/prelude.rs
src/progress.rs

index 82f75c5262044a8dba053fe74695c3317c97846d..8b94910fb7e701314200ea8e2781fa4b308d73bd 100644 (file)
@@ -600,10 +600,12 @@ impl InstanceBundles {
 
 impl Uploading {
   #[throws(MgmtError)]
-  pub fn bulk<R>(self, data: &mut R, expected: &Hash,
-                 for_progress: &mut dyn progress::Reporter) -> Uploaded
-  where R: Read
+  pub fn bulk<R,PW>(self, data: &mut R, expected: &Hash,
+                    for_progress: &mut ResponseWriter<PW>) -> Uploaded
+  where R: Read, PW: Write
   {
+    let mut for_progress = progress::ResponseReporter::new(for_progress);
+
     let Uploading { id, mut file, instance } = self;
     let tmp = id.path_tmp(&instance);
 
@@ -621,9 +623,9 @@ impl Uploading {
     let mut file = BufReader::new(file);
 
     let parsed = parse_bundle(id, &mut file, BundleParseUpload,
-                              for_progress)?;
+                              &mut for_progress)?;
 
-    process_bundle(id, &*instance, for_progress)?;
+    process_bundle(id, &*instance, &mut for_progress)?;
 
     Uploaded { id, parsed }
   }
index e9c3462248660dd45e0e44d975a3f1df9b01b7f8..c77472807d27fa215d88df9788284f5ba88f05a0 100644 (file)
@@ -147,7 +147,7 @@ pub use crate::nwtemplates;
 pub use crate::occultilks::*;
 pub use crate::organise;
 pub use crate::packetframe::{FrameReader, FrameWriter, ReadFrame, WriteFrame};
-pub use crate::packetframe::{ReadExt};
+pub use crate::packetframe::{ReadExt, ResponseWriter};
 pub use crate::pcaliases::*;
 pub use crate::pcrender::*;
 pub use crate::pieces::*;
index a9692a51da695705bccb5528400e0a098df346ad..5f590da4ae534c57f3a82a3c98fc16d82371831c 100644 (file)
@@ -3,7 +3,6 @@
 // There is NO WARRANTY.
 
 use crate::prelude::*;
-use crate::packetframe::ResponseWriter;
 
 #[derive(Debug,Clone,Serialize,Deserialize,IntoOwned)]
 pub struct ProgressInfo<'pi> {
@@ -20,16 +19,44 @@ pub struct Count<'pi> {
 
 pub trait Reporter {
   fn report(&mut self, info: ProgressInfo<'_>);
+  fn phase_begin_(&mut self, phase: Count<'_>, len: usize);
+  fn item_(&mut self, entry: usize, desc: Cow<'_, str>);
 }
 
-impl<W> Reporter for ResponseWriter<'_, W> where W: Write {
+pub struct ResponseReporter<'c,'w,W> where W: Write {
+  chan: &'c mut ResponseWriter<'w,W>,
+  phase: Count<'static>,
+  len: usize,
+}
+impl<'c,'w,W> ResponseReporter<'c,'w,W> where W: Write {
+  pub fn new(chan: &'c mut ResponseWriter<'w,W>) -> Self { Self {
+    chan,
+    phase: Count { i:0, n:0, desc: Cow::Borrowed("") },
+    len: 0,
+  } }
+}
+
+impl<W> Reporter for ResponseReporter<'_,'_,W> where W: Write {
   fn report(&mut self, pi: ProgressInfo<'_>) {
-    self.progress(pi).unwrap_or(());
+    self.chan.progress(pi).unwrap_or(());
+  }
+  fn phase_begin_(&mut self, phase: Count<'_>, len: usize) {
+    self.phase = phase.into_owned();
+    self.len = len;
+  }
+  fn item_(&mut self, entry: usize, desc: Cow<'_, str>) {
+    self.report(ProgressInfo {
+      phase: self.phase.clone(),
+      entry: Count { i: entry, n: self.len, desc }
+    })
   }
 }
 
+#[allow(unused_variables)]
 impl Reporter for () {
-  fn report(&mut self, _pi: ProgressInfo<'_>) { }
+  fn report(&mut self, pi: ProgressInfo<'_>) { }
+  fn phase_begin_(&mut self, phase: Count<'_>, len: usize) { }
+  fn item_(&mut self, entry: usize, desc: Cow<'_, str>) { }
 }
 
 impl<'t,T> From<&'t T> for Count<'t>
@@ -54,4 +81,16 @@ impl &mut dyn Reporter {
     let entry = &entry; let entry = entry.into();
     self.report(ProgressInfo { phase, entry });
   }
+
+  fn phase_begin<P>(&mut self, phase: P, len: usize)
+  where for <'p> &'p P: Into<Count<'p>>,
+  {
+    self.phase_begin_((&phase).into(), len)
+  }
+
+  fn item<'s,S>(&mut self, entry: usize, desc: S)
+  where S: Into<Cow<'s, str>>,
+  {
+    self.item_(entry, desc.into())
+  }
 }