chiark / gitweb /
support: Move packetframe et al.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 14 May 2022 18:37:12 +0000 (19:37 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 14 May 2022 18:55:38 +0000 (19:55 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
17 files changed:
Cargo.lock
Cargo.toml
daemon/cmdlistener.rs
src/bundles.rs
src/crates.rs
src/imports.rs
src/lib.rs
src/mgmtchannel.rs
src/utils.rs
support/Cargo.toml
support/crates.rs
support/imports.rs
support/lib.rs
support/packetframe.rs [moved from src/packetframe.rs with 93% similarity]
support/progress.rs [moved from src/progress.rs with 89% similarity]
support/support.rs
support/termprogress.rs [moved from src/termprogress.rs with 100% similarity]

index 939464ba37bfa0902390d89405d5613d4ecb9da2..dd68c43ddf2aff70ec5766e42810c980edd1d7a0 100644 (file)
@@ -2678,13 +2678,10 @@ dependencies = [
  "base64 0.13.0",
  "boolinator",
  "bundle-sources",
- "byteorder",
  "cast_trait_object",
- "console",
  "const-default",
  "crossbeam-utils",
  "delegate",
- "derive-into-owned",
  "digest 0.10.3",
  "downcast-rs",
  "educe",
@@ -2706,7 +2703,6 @@ dependencies = [
  "libc",
  "mio 0.8.2",
  "num",
- "num-derive",
  "num-traits",
  "once_cell",
  "openssh-keys",
@@ -2717,8 +2713,6 @@ dependencies = [
  "pwd",
  "rand 0.8.5",
  "regex",
- "rmp",
- "rmp-serde",
  "serde",
  "serde_with",
  "sha2 0.10.2",
@@ -2733,7 +2727,6 @@ dependencies = [
  "typetag",
  "uds",
  "unicase",
- "unicode-width",
  "url",
  "usvg",
  "vecdeque-stableix",
@@ -2831,15 +2824,24 @@ name = "otter-support"
 version = "1.0.0"
 dependencies = [
  "anyhow",
+ "byteorder",
  "chrono",
  "chrono-tz",
+ "console",
+ "derive-into-owned",
  "fehler",
  "log",
  "nix 0.23.1",
+ "num-derive",
+ "num-traits",
  "otter-base",
  "parking_lot",
+ "rmp",
+ "rmp-serde",
  "serde",
  "serde_with",
+ "strum",
+ "unicode-width",
 ]
 
 [[package]]
index 3e66be3d01072738cd02af80117698d8b847ebae..de459ba56b0194d3ed6df1e980cc73a009a57b5f 100644 (file)
@@ -35,12 +35,9 @@ otter-support.version="=1.0.0"
 backtrace="0.3"
 base64="0.13"
 boolinator="2"
-byteorder="1.3"
 cast_trait_object="0.1"
-console="0.15"
 crossbeam-utils="0.8"
 delegate="0.6"
-derive-into-owned="0.2"
 digest="0.10"
 downcast-rs="1"
 educe="0.4"
@@ -56,8 +53,6 @@ lazy-regex="2"
 lazy_static="1"
 libc="0.2"
 num="0.4"
-num-derive="0.3"
-num-traits="0.2"
 once_cell="1"
 openssh-keys="0.5"
 ordered-float="2"
@@ -66,8 +61,6 @@ percent-encoding="2"
 pwd="1"
 rand="0.8"
 regex="1"
-rmp="0.8"
-rmp-serde="1"
 structopt="0.3"
 sha2="0.10"
 subtle="2.4"
@@ -77,7 +70,6 @@ toml="0.5"
 typetag="0.1.6"
 uds="0.2"
 unicase="2"
-unicode-width="0.1"
 url="2"
 vecdeque-stableix="1"
 xmlparser = "0.13"
@@ -92,14 +84,15 @@ flexi_logger = { version="0.22" , features=["specfile"    ] }
 image = { version = "0.24", default-features=false, features=["jpeg","png"] }
 index_vec    = { version="0.1.1", features=["serde"       ] }
 mio          = { version="0.8",   features=["os-ext", "os-poll" ] }
-strum        = { version="0.24" , features=["derive"      ] }
 
 slotmap = { package="slotmap-fork-otter", version="1", git="https://github.com/ijackson/slotmap", branch="slotmap-fork-otter", features=["serde"] }
 
 # Repeated in other Cargo.toml's because importing does not work properly
 fehler="1"
-serde        = { version="1"    , features=["derive", "rc"] }
+num-traits="0.2"
 serde_with="1"
 thiserror="1"
+serde        = { version="1"    , features=["derive", "rc"] }
+strum        = { version="0.24" , features=["derive"      ] }
 
 #fin.
index ba97590696a089b8ab039fa539110155928540e3..00b335218c993db09c864bcdf784aa484a5be224 100644 (file)
@@ -5,6 +5,7 @@
 // management API implementation
 
 use otter::crates::*;
+use otter_support::imports::*;
 
 use super::*;
 use otter::commands::*;
index 1872264d9adfcc293fd7f7cb7f57654d9654a2dc..ca6afe66c6ef234788d8705fd02a0c177bfc9e1d 100644 (file)
@@ -1160,7 +1160,10 @@ impl Uploading {
   {
     let mut for_progress_box: Box<dyn progress::Originator> =
       if progress_mode >= PUM::Simplex {
-        Box::new(progress::ResponseOriginator::new(progress_stream))
+        Box::new(progress::ResponseOriginator::new(
+          progress_stream,
+          |pi: ProgressInfo<'_>| MgmtResponse::Progress(pi.into_owned()),
+        ))
       } else {
         Box::new(())
       };
index 03ef76d100da115ef1042c19179e94d948fb3581..4f30987daff29e266493c771d08792e5266ed52a 100644 (file)
@@ -28,12 +28,10 @@ pub use once_cell;
 pub use ordered_float;
 pub use pwd;
 pub use regex;
-pub use rmp_serde;
 pub use sha2;
 pub use slotmap;
 pub use tera;
 pub use toml;
 pub use uds;
-pub use unicode_width;
 pub use vecdeque_stableix;
 pub use zip as zipfile;
index 69475c3784df32399624cc6021e7a3e07bf3231a..53b659c47d425331058c2f898186f0c15d68afa9 100644 (file)
@@ -11,10 +11,7 @@ use otter_base::crates::*;
 use otter_base::imports::*;
 
 pub use std::any::Any;
-pub use std::borrow::Cow;
-pub use std::cmp::{self, max, min, Ordering};
 pub use std::convert::{Infallible, TryFrom, TryInto};
-pub use std::env;
 pub use std::error::Error;
 pub use std::ffi::OsStr;
 pub use std::fmt::Formatter;
@@ -37,11 +34,9 @@ pub use std::time::{self, Duration, Instant};
 
 pub use async_condvar_fair::{Condvar, BatonExt as _};
 pub use boolinator::Boolinator as _;
-pub use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
 pub use cast_trait_object::{dyn_upcast, DynCastExt};
 pub use const_default::ConstDefault;
 pub use delegate::delegate;
-pub use derive_into_owned::IntoOwned;
 pub use digest::Digest;
 pub use downcast_rs::{impl_downcast, Downcast};
 pub use educe::Educe;
@@ -55,8 +50,6 @@ pub use index_vec::{define_index_type, index_vec, IndexSlice, IndexVec};
 pub use lazy_regex::regex;
 pub use lazy_static::lazy_static;
 pub use log::{log, log_enabled};
-pub use num_derive::{ToPrimitive, FromPrimitive};
-pub use num_traits::{Bounded, FromPrimitive, ToPrimitive};
 pub use ordered_float::OrderedFloat;
 pub use paste::paste;
 pub use percent_encoding::percent_decode_str;
@@ -69,9 +62,6 @@ pub use rand::prelude::SliceRandom;
 pub use regex::Regex;
 pub use sha2::{Sha512, Sha512_256};
 pub use slotmap::{dense::DenseSlotMap, SparseSecondaryMap, Key as _};
-pub use strum::{EnumCount, EnumDiscriminants};
-pub use strum::{EnumString, EnumIter, EnumMessage, EnumProperty};
-pub use strum::{AsRefStr, IntoEnumIterator, IntoStaticStr};
 pub use subtle::ConstantTimeEq;
 pub use tempfile::{self, NamedTempFile};
 pub use tera::Tera;
@@ -98,7 +88,6 @@ pub use crate::{deref_to_field, deref_to_field_mut};
 pub use crate::ensure_eq;
 pub use crate::format_by_fmt_hex;
 pub use crate::impl_via_ambassador;
-pub use crate::matches_doesnot;
 pub use crate::trace_dbg;
 pub use crate::{want, wantok, wants, want_let, want_failed_internal};
 pub use crate::serde_with_compat;
@@ -130,12 +119,9 @@ pub use crate::materials_format;
 pub use crate::mgmtchannel::*;
 pub use crate::occultilks::*;
 pub use crate::organise;
-pub use crate::packetframe::{FrameReader, FrameWriter, ReadFrame, WriteFrame};
-pub use crate::packetframe::{ReadExt, ResponseWriter};
 pub use crate::pcaliases::*;
 pub use crate::pcrender::*;
 pub use crate::pieces::*;
-pub use crate::progress::{self, ProgressInfo, OriginatorExt as _};
 pub use crate::shapelib;
 pub use crate::shapelib::{CircleShape, RectShape};
 pub use crate::shapelib::{ItemEnquiryData, LibraryEnquiryData};
@@ -145,7 +131,6 @@ pub use crate::spec::*;
 pub use crate::spec::piece_specs::{FaceColourSpecs, SimpleCommon};
 pub use crate::toml_de;
 pub use crate::timedfd::*;
-pub use crate::termprogress;
 pub use crate::updates::*;
 pub use crate::utils::*;
 pub use crate::ui::*;
index a7236beaae3a74df5a027601d682f735ded93ddd..2a3762bc1a01da1e2eb653b9250faf37803eef2a 100644 (file)
@@ -45,14 +45,11 @@ pub mod mgmtchannel;
 pub mod nwtemplates;
 pub mod occultilks;
 pub mod organise;
-pub mod packetframe;
 pub mod pcaliases;
 pub mod pcrender;
 pub mod pieces;
-pub mod progress;
 pub mod shapelib;
 pub mod spec;
-pub mod termprogress;
 pub mod timedfd;
 pub mod updates;
 pub mod ui;
index 880a0dceb08b8645928e8ba3fc5c48a4ed78db4f..a765318267ed75ddce1a5d79f36341df751e6bd0 100644 (file)
@@ -5,34 +5,6 @@
 use crate::prelude::*;
 use crate::commands::*;
 
-#[derive(Debug,Error)]
-pub enum MgmtChannelReadError {
-  #[error("unexpected EOF")]         EOF,
-  #[error("parse MessagePack: {0}")] Parse(String),
-  #[error("{0}")]                    IO(#[from] io::Error),
-}
-
-#[derive(Debug,Error)]
-pub enum MgmtChannelWriteError {
-  Serialize(rmp_serde::encode::Error), // but not ValueWriteError so no from
-  IO(#[from] io::Error),
-}
-display_as_debug!{MgmtChannelWriteError}
-
-impl From<rmp_serde::encode::Error> for MgmtChannelWriteError {
-  fn from(re: rmp_serde::encode::Error) -> MgmtChannelWriteError {
-    use rmp_serde::encode::Error::*;
-    use MgmtChannelWriteError as MCWE;
-    use rmp::encode::ValueWriteError as RVWE;
-    match re {
-      InvalidValueWrite(RVWE::InvalidMarkerWrite(ioe)) => MCWE::IO(ioe),
-      InvalidValueWrite(RVWE::InvalidDataWrite  (ioe)) => MCWE::IO(ioe),
-      ser@ (UnknownLength | InvalidDataModel(_) |
-            DepthLimitExceeded | Syntax(_)) => MCWE::Serialize(ser),
-    }
-  }
-}
-
 pub struct MgmtChannel<R:Read, W:Write> {
   pub read:  FrameReader<R>,
   pub write: FrameWriter<W>,
index 439892b174bfa70522b025690b6d2f3f87fd5070..6aa057444f2fef5a9c7d8f410e68e9e0903c8c07 100644 (file)
@@ -670,62 +670,6 @@ fn test_parse_hex(){
   assert_eq!( parse_fixed_hex("xy"  ), None::<[_;1]>      );
 }
 
-//========== matches_doesnot ==========
-
-#[macro_export] // <- otherwise bogus warning `unused_macros`
-macro_rules! matches_doesnot_yn2bool {
-  (=) => (true);
-  (!) => (false);
-}
-
-#[macro_export]
-macro_rules! matches_doesnot {
-  ($v:expr,
-   $(
-     $yn:tt $p:pat
-   ),* $(,)?
-  ) => {
-    match $v {
-      $(
-        $p => $crate::matches_doesnot_yn2bool!($yn),
-      )*
-    }
-  }
-}
-
-#[test]
-fn matches_doesnot_test() {
-  assert!(
-    matches_doesnot!(
-      Some(42),
-      = Some(_),
-      ! None
-    )
-  );
-  assert!(
-    matches_doesnot!(
-      Some(42),
-      ! None,
-      ! Some(3),
-      = Some(_),
-    )
-  );
-  assert!(
-    matches_doesnot!(
-      Some(1),
-      = Some(1) | Some(2),
-      ! Some(_) | None
-    )
-  );
-  assert!(
-    ! matches_doesnot!(
-      Some(1),
-      ! Some(1) | Some(2),
-      = Some(_) | None
-    )
-  );
-}
-
 //========== want* macros ==========
 
 #[macro_export]
index 4faca4c3d0262792fb289b40b879afb55d08e8cf..16b036ceb0e3a54d94269ca40f15b41a58a99a90 100644 (file)
@@ -27,15 +27,24 @@ otter-base.path="../base"
 otter-base.version="=1.0.0"
        
 anyhow="1"
+byteorder="1.3"
 chrono="0.4"
 chrono-tz="0.6"
+console="0.15"
+derive-into-owned="0.2"
 fehler="1"
 log="0.4"
 nix="0.23"
+num-derive="0.3"
 parking_lot="0.12"
+rmp="0.8"
+rmp-serde="1"
+unicode-width="0.1"
 
+# Repeated in other Cargo.toml's because importing does not work properly
+num-traits="0.2"
 serde_with="1"
-
 serde        = { version="1"    , features=["derive", "rc"] }
+strum        = { version="0.24" , features=["derive"      ] }
 
 #fin.
index f2a74b42a618b5867df6e0228601131e5a2ebbc8..46f0e18ce2a0c90ba5cf9734e145417d4a63c83a 100644 (file)
@@ -12,3 +12,5 @@ pub use chrono_tz;
 pub use log;
 pub use nix;
 pub use parking_lot;
+pub use rmp_serde;
+pub use unicode_width;
index 7da9160515be3f28a82bcc3c5c61ec2ab04498e6..c58026ed2c39255d0a3e990b4167bcfa4c4b8c00 100644 (file)
@@ -4,10 +4,13 @@
 
 // See Import Structure Doctrine in src/prelude.rs
 
+pub use std::borrow::Cow;
+pub use std::cmp::{self, max, min, Ordering};
 pub use std::collections::{hash_map, HashMap, HashSet};
 pub use std::collections::VecDeque;
 pub use std::collections::{btree_map, BTreeMap};
 pub use std::collections::{btree_set, BTreeSet};
+pub use std::env;
 pub use std::fs;
 pub use std::fs::File;
 pub use std::io;
@@ -24,16 +27,23 @@ pub use std::process::{exit, Child, Command, Stdio};
 pub use std::sync::Arc;
 
 pub use anyhow::{anyhow, ensure, Context};
+pub use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
+pub use derive_into_owned::IntoOwned;
 pub use log::{debug, error, info, trace, warn};
 pub use nix::unistd::{self, Uid};
 pub use nix::sys::time::TimeSpec;
 pub use nix::time::clock_gettime;
+pub use num_derive::{ToPrimitive, FromPrimitive};
+pub use num_traits::{Bounded, FromPrimitive, ToPrimitive};
 pub use serde::ser::SerializeTuple;
 pub use serde::{de::DeserializeOwned, Deserialize, Serialize};
 pub use serde::de::Error as _;
 pub use serde::{Deserializer, Serializer};
 pub use serde_with::DeserializeFromStr;
 pub use serde_with::SerializeDisplay;
+pub use strum::{EnumCount, EnumDiscriminants};
+pub use strum::{EnumString, EnumIter, EnumMessage, EnumProperty};
+pub use strum::{AsRefStr, IntoEnumIterator, IntoStaticStr};
 
 // No debug version of this
 pub use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
@@ -42,9 +52,16 @@ pub use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
 pub use parking_lot::{Mutex, MutexGuard};
 //pub use crate::debugmutex::{Mutex, MutexGuard};
 
+pub use crate::matches_doesnot;
+
 pub use crate::childio;
 pub use crate::debugmutex::DebugIdentify;
+pub use crate::packetframe::{FrameReader, FrameWriter, ReadFrame, WriteFrame};
+pub use crate::packetframe::{ReadExt, ResponseWriter};
+pub use crate::packetframe::{MgmtChannelReadError, MgmtChannelWriteError};
+pub use crate::progress::{self, ProgressInfo, OriginatorExt as _};
 pub use crate::support::*;
+pub use crate::termprogress;
 pub use crate::tz::*;
 
 // ---------- type abbreviations ----------
index 6b467cd943c3f89666a0d59b78a93099a85a8069..0307bc497cedbe8c96182d664f0e5afccf9cb965 100644 (file)
@@ -8,5 +8,8 @@ pub mod prelude;
 
 pub mod childio;
 pub mod debugmutex;
+pub mod packetframe;
+pub mod progress;
 pub mod support;
+pub mod termprogress;
 pub mod tz;
similarity index 93%
rename from src/packetframe.rs
rename to support/packetframe.rs
index 4701a3b77281234ec0645f3f4cd385593746e9b9..ad2ffe3f2e57a37cceac99cf9a1e5096857a5d56 100644 (file)
 
 use crate::prelude::*;
 
+// ---------- errors (MgmtChannel, anomalous name) ----------
+
+#[derive(Debug,Error)]
+pub enum MgmtChannelReadError {
+  #[error("unexpected EOF")]         EOF,
+  #[error("parse MessagePack: {0}")] Parse(String),
+  #[error("{0}")]                    IO(#[from] io::Error),
+}
+
+#[derive(Debug,Error)]
+pub enum MgmtChannelWriteError {
+  Serialize(rmp_serde::encode::Error), // but not ValueWriteError so no from
+  IO(#[from] io::Error),
+}
+display_as_debug!{MgmtChannelWriteError}
+
+impl From<rmp_serde::encode::Error> for MgmtChannelWriteError {
+  fn from(re: rmp_serde::encode::Error) -> MgmtChannelWriteError {
+    use rmp_serde::encode::Error::*;
+    use MgmtChannelWriteError as MCWE;
+    use rmp::encode::ValueWriteError as RVWE;
+    match re {
+      InvalidValueWrite(RVWE::InvalidMarkerWrite(ioe)) => MCWE::IO(ioe),
+      InvalidValueWrite(RVWE::InvalidDataWrite  (ioe)) => MCWE::IO(ioe),
+      ser@ (UnknownLength | InvalidDataModel(_) |
+            DepthLimitExceeded | Syntax(_)) => MCWE::Serialize(ser),
+    }
+  }
+}
+
 // ---------- common ----------
 
 type ChunkLen = u16;
@@ -467,13 +497,18 @@ impl<'c,W:Write> ResponseWriter<'c,W> {
     self.f
   }
 
-  
+  #[throws(MgmtChannelWriteError)]
+  pub fn progress_with<RESP: Serialize>(&mut self, resp: RESP) {
+    rmp_serde::encode::write_named(&mut self.f, &resp)?;
+    self.f.flush()?;
+  }
+/*
   #[throws(MgmtChannelWriteError)]
   pub fn progress(&mut self, pi: ProgressInfo<'_>) {
     let resp = crate::commands::MgmtResponse::Progress(pi.into_owned());
     rmp_serde::encode::write_named(&mut self.f, &resp)?;
     self.f.flush()?;
-  }
+  }*/
 }
 
 // ==================== tests ====================
similarity index 89%
rename from src/progress.rs
rename to support/progress.rs
index a23dac002a80e5ed5ec445fb0b9de6d56300582d..d71c82bd0c42ce1cec229db527157008b54e39fc 100644 (file)
@@ -23,22 +23,30 @@ pub trait Originator {
   fn item_(&mut self, item: usize, desc: Cow<'_, str>);
 }
 
-pub struct ResponseOriginator<'c,'w,W> where W: Write {
+pub struct ResponseOriginator<'c,'w,W,F> where W: Write {
   chan: &'c mut ResponseWriter<'w,W>,
   phase: Count<'static>,
   len: usize,
+  formatter: F,
 }
-impl<'c,'w,W> ResponseOriginator<'c,'w,W> where W: Write {
-  pub fn new(chan: &'c mut ResponseWriter<'w,W>) -> Self { Self {
+impl<'c,'w,W,F> ResponseOriginator<'c,'w,W,F> where W: Write {
+  pub fn new(chan: &'c mut ResponseWriter<'w,W>,
+             formatter: F) -> Self { Self {
     chan,
     phase: Count { i:0, n:0, desc: Cow::Borrowed("") },
     len: 0,
+    formatter,
   } }
 }
 
-impl<W> Originator for ResponseOriginator<'_,'_,W> where W: Write {
+impl<W,F,M> Originator for ResponseOriginator<'_,'_,W,F>
+where W: Write,
+      F: Fn(ProgressInfo<'_>) -> M,
+      M: Serialize,
+{
   fn report(&mut self, pi: ProgressInfo<'_>) {
-    self.chan.progress(pi).unwrap_or(());
+    let resp = (self.formatter)(pi);
+    self.chan.progress_with(resp).unwrap_or(());
   }
   fn phase_begin_(&mut self, phase: Count<'_>, len: usize) {
     self.phase = phase.into_owned();
index 3296df0620a12c6a72814f66e8b271575006b785..c5aa50031b02d5dca6369210fbe1f61ae1bae040 100644 (file)
@@ -4,6 +4,8 @@
 
 use crate::prelude::*;
 
+//========== Timestamp ==========
+
 #[derive(Copy,Clone,Debug,Serialize,Deserialize,Eq,Ord,PartialEq,PartialOrd)]
 #[serde(transparent)]
 pub struct Timestamp(pub u64); /* time_t */
@@ -24,4 +26,59 @@ impl Timestamp {
   }
 }
 
+//========== matches_doesnot ==========
+
+#[macro_export] // <- otherwise bogus warning `unused_macros`
+macro_rules! matches_doesnot_yn2bool {
+  (=) => (true);
+  (!) => (false);
+}
+
+#[macro_export]
+macro_rules! matches_doesnot {
+  ($v:expr,
+   $(
+     $yn:tt $p:pat
+   ),* $(,)?
+  ) => {
+    match $v {
+      $(
+        $p => $crate::matches_doesnot_yn2bool!($yn),
+      )*
+    }
+  }
+}
+
+#[test]
+fn matches_doesnot_test() {
+  assert!(
+    matches_doesnot!(
+      Some(42),
+      = Some(_),
+      ! None
+    )
+  );
+  assert!(
+    matches_doesnot!(
+      Some(42),
+      ! None,
+      ! Some(3),
+      = Some(_),
+    )
+  );
+  assert!(
+    matches_doesnot!(
+      Some(1),
+      = Some(1) | Some(2),
+      ! Some(_) | None
+    )
+  );
+  assert!(
+    ! matches_doesnot!(
+      Some(1),
+      ! Some(1) | Some(2),
+      = Some(_) | None
+    )
+  );
+}
 
similarity index 100%
rename from src/termprogress.rs
rename to support/termprogress.rs