diff options
author | Profpatsch <mail@profpatsch.de> | 2021-01-29T14·49+0100 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2021-01-31T11·10+0000 |
commit | ca52b29078519b22083f1e8edd24ee8527c10857 (patch) | |
tree | db79d1b8aec72c58eb7ddb1249d8176f39f3da08 /users/Profpatsch | |
parent | f05bece2cb240090a4eb40071daa69b857c0663c (diff) |
refactor(users/Profpatsch/netencode): rust read from stdin helper r/2170
Change-Id: I195c0212e224f676de5db37807731b814f99e818 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2452 Tested-by: BuildkiteCI Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to 'users/Profpatsch')
-rw-r--r-- | users/Profpatsch/netencode/netencode.rs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/users/Profpatsch/netencode/netencode.rs b/users/Profpatsch/netencode/netencode.rs index 94327fc5e172..3c6c113df589 100644 --- a/users/Profpatsch/netencode/netencode.rs +++ b/users/Profpatsch/netencode/netencode.rs @@ -1,7 +1,7 @@ extern crate nom; use std::collections::HashMap; -use std::io::Write; +use std::io::{Write, Read}; #[derive(Debug, PartialEq, Eq, Clone)] pub enum T { @@ -116,6 +116,18 @@ pub fn text(s: String) -> T { T::Text(s) } +pub fn t_from_stdin_or_panic(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, + _ => panic!("{}: stdin contained some soup after netencode value: {:?}", prog_name, rest) + }, + Err(err) => panic!("{}: unable to parse netencode from stdin: {:?}", prog_name, err) + } +} + pub mod parse { use super::{T, Tag, U}; |