From: Ian Jackson Date: Sat, 14 May 2022 18:37:12 +0000 (+0100) Subject: support: Move packetframe et al. X-Git-Tag: otter-1.1.0~170 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=329f579a74fe43020618240a6bc56296a456f117;p=otter.git support: Move packetframe et al. Signed-off-by: Ian Jackson --- diff --git a/Cargo.lock b/Cargo.lock index 939464ba..dd68c43d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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]] diff --git a/Cargo.toml b/Cargo.toml index 3e66be3d..de459ba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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. diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index ba975906..00b33521 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -5,6 +5,7 @@ // management API implementation use otter::crates::*; +use otter_support::imports::*; use super::*; use otter::commands::*; diff --git a/src/bundles.rs b/src/bundles.rs index 1872264d..ca6afe66 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -1160,7 +1160,10 @@ impl Uploading { { let mut for_progress_box: Box = 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(()) }; diff --git a/src/crates.rs b/src/crates.rs index 03ef76d1..4f30987d 100644 --- a/src/crates.rs +++ b/src/crates.rs @@ -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; diff --git a/src/imports.rs b/src/imports.rs index 69475c37..53b659c4 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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::*; diff --git a/src/lib.rs b/src/lib.rs index a7236bea..2a3762bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/mgmtchannel.rs b/src/mgmtchannel.rs index 880a0dce..a7653182 100644 --- a/src/mgmtchannel.rs +++ b/src/mgmtchannel.rs @@ -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 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 { pub read: FrameReader, pub write: FrameWriter, diff --git a/src/utils.rs b/src/utils.rs index 439892b1..6aa05744 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -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] diff --git a/support/Cargo.toml b/support/Cargo.toml index 4faca4c3..16b036ce 100644 --- a/support/Cargo.toml +++ b/support/Cargo.toml @@ -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. diff --git a/support/crates.rs b/support/crates.rs index f2a74b42..46f0e18c 100644 --- a/support/crates.rs +++ b/support/crates.rs @@ -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; diff --git a/support/imports.rs b/support/imports.rs index 7da91605..c58026ed 100644 --- a/support/imports.rs +++ b/support/imports.rs @@ -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 ---------- diff --git a/support/lib.rs b/support/lib.rs index 6b467cd9..0307bc49 100644 --- a/support/lib.rs +++ b/support/lib.rs @@ -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; diff --git a/src/packetframe.rs b/support/packetframe.rs similarity index 93% rename from src/packetframe.rs rename to support/packetframe.rs index 4701a3b7..ad2ffe3f 100644 --- a/src/packetframe.rs +++ b/support/packetframe.rs @@ -15,6 +15,36 @@ 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 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(&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 ==================== diff --git a/src/progress.rs b/support/progress.rs similarity index 89% rename from src/progress.rs rename to support/progress.rs index a23dac00..d71c82bd 100644 --- a/src/progress.rs +++ b/support/progress.rs @@ -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 Originator for ResponseOriginator<'_,'_,W> where W: Write { +impl 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(); diff --git a/support/support.rs b/support/support.rs index 3296df06..c5aa5003 100644 --- a/support/support.rs +++ b/support/support.rs @@ -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 + ) + ); +} diff --git a/src/termprogress.rs b/support/termprogress.rs similarity index 100% rename from src/termprogress.rs rename to support/termprogress.rs