From 91f927976f2c2aafd52153e3e99106573466c27f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 26 Nov 2020 21:32:13 +0000 Subject: [PATCH] wip timestamp_abbreviate Signed-off-by: Ian Jackson --- Cargo.lock.example | 2 ++ zcoord/Cargo.toml | 2 ++ zcoord/misc.rs | 24 ++++++++++++++++++++++++ zcoord/zcoord.rs | 2 ++ 4 files changed, 30 insertions(+) create mode 100644 zcoord/misc.rs diff --git a/Cargo.lock.example b/Cargo.lock.example index 6b1e531d..89c6c15c 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -1186,8 +1186,10 @@ dependencies = [ name = "otter-zcoord" version = "0.0.1" dependencies = [ + "arrayvec", "derive_more", "fehler", + "if_chain", "serde", "serde_with", "thiserror", diff --git a/zcoord/Cargo.toml b/zcoord/Cargo.toml index 7a29c1f0..8fa43fa5 100644 --- a/zcoord/Cargo.toml +++ b/zcoord/Cargo.toml @@ -13,8 +13,10 @@ path = "zcoord.rs" [dependencies] +arrayvec = "0" derive_more = "0.99" fehler = "1" +if_chain = "1" thiserror = "1" serde = { version = "1", features = ["derive","rc"] } serde_with = "1" diff --git a/zcoord/misc.rs b/zcoord/misc.rs new file mode 100644 index 00000000..43531cc9 --- /dev/null +++ b/zcoord/misc.rs @@ -0,0 +1,24 @@ +// Copyright 2020 Ian Jackson +// SPDX-License-Identifier: AGPL-3.0-or-later +// There is NO WARRANTY. + +// This is in this crate for convenience, not because it's to do with +// Z coordinates. + +use if_chain::if_chain; +use arrayvec::ArrayVec; + +pub fn timesting_abbreviate<'x>(base: &str, this: &'x str) -> (&'x str, bool) { + fn split(s: &str) -> ArrayVec<[&str; 3]> { + s.splitn(3, ' ').collect() + } + let base3 = split(base); + let this3 = split(this); + let matches = |i| base3.get(i) == this3.get(i); + if_chain! { + if matches(0) && matches(2); + if let Some(abbrev) = this3.get(1); + then { (abbrev, true) } + else { (this, false) } + } +} diff --git a/zcoord/zcoord.rs b/zcoord/zcoord.rs index 745e4ec9..bab9cfd0 100644 --- a/zcoord/zcoord.rs +++ b/zcoord/zcoord.rs @@ -79,6 +79,8 @@ use thiserror::Error; use serde_with::DeserializeFromStr; use serde_with::SerializeDisplay; +mod misc; + //---------- core definitions ---------- pub type RangeCount = u32; -- 2.30.2