From 1e5baa0dea9375ffc9b446b369e552876f6a15fd Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 9 Feb 2021 20:56:56 +0100 Subject: feat(users/Profpatsch/netencode): add record-get Projecting into one record field of netencode given on stdin. Change-Id: I975bd5558a06988aa159156ca73a449710db983f Reviewed-on: https://cl.tvl.fyi/c/depot/+/2502 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/netencode/default.nix | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'users') diff --git a/users/Profpatsch/netencode/default.nix b/users/Profpatsch/netencode/default.nix index 3b62c8aaac59..92672470e902 100644 --- a/users/Profpatsch/netencode/default.nix +++ b/users/Profpatsch/netencode/default.nix @@ -93,6 +93,35 @@ let } (builtins.readFile ./netencode-mustache.rs); + record-get = imports.writers.rustSimple { + name = "record-get"; + dependencies = [ + netencode-rs + depot.users.Profpatsch.execline.exec-helpers + depot.users.Profpatsch.arglib.netencode.rust + ]; + } '' + extern crate netencode; + extern crate arglib_netencode; + extern crate exec_helpers; + use netencode::{encode, dec}; + use netencode::dec::{Decoder, DecodeError}; + + fn main() { + let mut buf = vec![]; + let (args, prog) = exec_helpers::args_for_exec("record-get", 1); + let field = match std::str::from_utf8(&args[0]) { + Ok(f) => f, + Err(_e) => exec_helpers::die_user_error("record-get", format!("The field name needs to be valid unicode")) + }; + let u = netencode::u_from_stdin_or_die_user_error("record-get", &mut buf); + match (dec::RecordDot {field, inner: dec::AnyU }).dec(u) { + Ok(u) => encode(&mut std::io::stdout(), &u).expect("encoding to stdout failed"), + Err(DecodeError(err)) => exec_helpers::die_user_error("record-get", err) + } + } + ''; + record-splice-env = imports.writers.rustSimple { name = "record-splice-env"; dependencies = [ @@ -122,6 +151,7 @@ in { netencode-rs netencode-rs-tests netencode-mustache + record-get record-splice-env gen ; -- cgit 1.4.1