From ca52b29078519b22083f1e8edd24ee8527c10857 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Fri, 29 Jan 2021 15:49:29 +0100 Subject: refactor(users/Profpatsch/netencode): rust read from stdin helper Change-Id: I195c0212e224f676de5db37807731b814f99e818 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2452 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- users/Profpatsch/netencode/netencode.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'users/Profpatsch') 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}; -- cgit 1.4.1