diff options
author | Profpatsch <mail@profpatsch.de> | 2021-02-09T19·56+0100 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2021-02-13T20·00+0000 |
commit | 1e5baa0dea9375ffc9b446b369e552876f6a15fd (patch) | |
tree | d0cd400a2c2a4dd4df103e704bf69f207d60d8a3 /users/Profpatsch/netencode/default.nix | |
parent | 7a4aca42ad9c512abd2b131ec0cae4c330deeacb (diff) |
feat(users/Profpatsch/netencode): add record-get r/2205
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 <mail@profpatsch.de>
Diffstat (limited to '')
-rw-r--r-- | users/Profpatsch/netencode/default.nix | 30 |
1 files changed, 30 insertions, 0 deletions
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 ; |