diff options
-rw-r--r-- | users/Profpatsch/netencode/default.nix | 5 | ||||
-rw-r--r-- | users/Profpatsch/netencode/netencode.rs | 50 |
2 files changed, 28 insertions, 27 deletions
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::<ScalarAsBytes>::dec(t) { + match Record::<ScalarAsBytes>::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<u8>) -> 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<Self::A, DecodeError>; + fn dec(u: U<'a>) -> Result<Self::A, DecodeError>; } pub struct ScalarAsBytes; - impl Decoder for ScalarAsBytes { + impl<'a> Decoder<'a> for ScalarAsBytes { type A = Vec<u8>; - fn dec(t: T) -> Result<Self::A, DecodeError> { - 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<Self::A, DecodeError> { + 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<T>(pub T); - impl<Inner: Decoder> Decoder for Record<Inner> { - type A = HashMap<String, Inner::A>; - fn dec(t: T) -> Result<Self::A, DecodeError> { - match t { - T::Record(map) => + impl<'a, Inner: Decoder<'a>> Decoder<'a> for Record<Inner> { + type A = HashMap<&'a str, Inner::A>; + fn dec(u: U<'a>) -> Result<Self::A, DecodeError> { + match u { + U::Record(map) => map.into_iter() .map(|(k, v)| Inner::dec(v).map(|v2| (k, v2))) .collect::<Result<Self::A, _>>(), |