about summary refs log tree commit diff
path: root/users/Profpatsch/netencode/netencode.rs
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-01-29T14·49+0100
committerProfpatsch <mail@profpatsch.de>2021-01-31T11·10+0000
commitca52b29078519b22083f1e8edd24ee8527c10857 (patch)
treedb79d1b8aec72c58eb7ddb1249d8176f39f3da08 /users/Profpatsch/netencode/netencode.rs
parentf05bece2cb240090a4eb40071daa69b857c0663c (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/netencode/netencode.rs')
-rw-r--r--users/Profpatsch/netencode/netencode.rs14
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};