From 3faf5b6f0927714d3bf1f3eaacd62189be7b6db2 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 6 Feb 2021 22:51:29 +0100 Subject: feat(users/Profpatsch/netencode): decode from U MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we don’t necessarily need to decode deeply, we can make the decoders take a `U` instead of a `T`. Change-Id: I9704a21edb3922d58411e6807d027d684b18d390 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2492 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/netencode/default.nix | 5 ++-- users/Profpatsch/netencode/netencode.rs | 50 ++++++++++++++++----------------- 2 files changed, 28 insertions(+), 27 deletions(-) (limited to 'users/Profpatsch') diff --git a/users/Profpatsch/netencode/default.nix b/users/Profpatsch/netencode/default.nix index 74dff2bc3a06..31cd822061b6 100644 --- a/users/Profpatsch/netencode/default.nix +++ b/users/Profpatsch/netencode/default.nix @@ -105,9 +105,10 @@ let use netencode::dec::{Record, ScalarAsBytes, Decoder, DecodeError}; fn main() { - let t = netencode::t_from_stdin_or_die_user_error("record-splice-env"); + let mut buf = vec![]; + let u = netencode::u_from_stdin_or_die_user_error("record-splice-env", &mut buf); let (_, prog) = exec_helpers::args_for_exec("record-splice-env", 0); - match Record::::dec(t) { + match Record::::dec(u) { Ok(map) => { exec_helpers::exec_into_args("record-splice-env", prog, map); }, diff --git a/users/Profpatsch/netencode/netencode.rs b/users/Profpatsch/netencode/netencode.rs index a1ee60452e59..c8c631689a02 100644 --- a/users/Profpatsch/netencode/netencode.rs +++ b/users/Profpatsch/netencode/netencode.rs @@ -162,16 +162,16 @@ pub fn text(s: String) -> T { T::Text(s) } -pub fn t_from_stdin_or_die_user_error(prog_name: &str) -> T { - let mut buf = vec![]; - std::io::stdin().lock().read_to_end(&mut buf); - match parse::t_t(&buf) { - Ok((rest, t)) => match rest { - b"" => t, +pub fn u_from_stdin_or_die_user_error<'a>(prog_name: &'_ str, stdin_buf: &'a mut Vec) -> U<'a> { + std::io::stdin().lock().read_to_end(stdin_buf); + let u = match parse::u_u(stdin_buf) { + Ok((rest, u)) => match rest { + b"" => u, _ => exec_helpers::die_user_error(prog_name, format!("stdin contained some soup after netencode value: {:?}", rest)) }, Err(err) => exec_helpers::die_user_error(prog_name, format!("unable to parse netencode from stdin: {:?}", err)) - } + }; + u } pub mod parse { @@ -599,25 +599,25 @@ pub mod dec { pub struct DecodeError(pub String); - pub trait Decoder { + pub trait Decoder<'a> { type A; - fn dec(T) -> Result; + fn dec(u: U<'a>) -> Result; } pub struct ScalarAsBytes; - impl Decoder for ScalarAsBytes { + impl<'a> Decoder<'a> for ScalarAsBytes { type A = Vec; - fn dec(t: T) -> Result { - match t { - T::N3(u) => Ok(format!("{}", u).into_bytes()), - T::N6(u) => Ok(format!("{}", u).into_bytes()), - T::N7(u) => Ok(format!("{}", u).into_bytes()), - T::I3(i) => Ok(format!("{}", i).into_bytes()), - T::I6(i) => Ok(format!("{}", i).into_bytes()), - T::I7(i) => Ok(format!("{}", i).into_bytes()), - T::Text(t) => Ok(t.into_bytes()), - T::Binary(b) => Ok(b), + fn dec(u: U<'a>) -> Result { + match u { + U::N3(u) => Ok(format!("{}", u).into_bytes()), + U::N6(u) => Ok(format!("{}", u).into_bytes()), + U::N7(u) => Ok(format!("{}", u).into_bytes()), + U::I3(i) => Ok(format!("{}", i).into_bytes()), + U::I6(i) => Ok(format!("{}", i).into_bytes()), + U::I7(i) => Ok(format!("{}", i).into_bytes()), + U::Text(t) => Ok(t.as_bytes().to_owned()), + U::Binary(b) => Ok(b.to_owned()), o => Err(DecodeError(format!("Cannot decode {:?} into scalar", o))), } } @@ -625,11 +625,11 @@ pub mod dec { pub struct Record(pub T); - impl Decoder for Record { - type A = HashMap; - fn dec(t: T) -> Result { - match t { - T::Record(map) => + impl<'a, Inner: Decoder<'a>> Decoder<'a> for Record { + type A = HashMap<&'a str, Inner::A>; + fn dec(u: U<'a>) -> Result { + match u { + U::Record(map) => map.into_iter() .map(|(k, v)| Inner::dec(v).map(|v2| (k, v2))) .collect::>(), -- cgit 1.4.1