1use std::io;
2
3use io::BufWriter;
4use io::Write;
5
6use io::BufRead;
7
8pub fn str2f(s: &str) -> Result<f32, io::Error> {
9 str::parse(s).map_err(io::Error::other)
10}
11
12pub fn str2d(s: &str) -> Result<f64, io::Error> {
13 str::parse(s).map_err(io::Error::other)
14}
15
16pub fn f2binl(f: f32) -> [u8; 4] {
17 f.to_le_bytes()
18}
19pub fn f2binb(f: f32) -> [u8; 4] {
20 f.to_be_bytes()
21}
22
23pub fn d2binl(d: f64) -> [u8; 8] {
24 d.to_le_bytes()
25}
26pub fn d2binb(d: f64) -> [u8; 8] {
27 d.to_be_bytes()
28}
29
30pub fn str2f2bin_new<C>(conv: C) -> impl Fn(&str) -> Result<[u8; 4], io::Error>
31where
32 C: Fn(f32) -> [u8; 4],
33{
34 move |s: &str| str2f(s).map(&conv)
35}
36
37pub fn str2d2bin_new<C>(conv: C) -> impl Fn(&str) -> Result<[u8; 8], io::Error>
38where
39 C: Fn(f64) -> [u8; 8],
40{
41 move |s: &str| str2d(s).map(&conv)
42}
43
44pub struct Writer<W> {
45 pub wtr: W,
46}
47
48impl<W> Writer<W>
49where
50 W: Write,
51{
52 pub fn into_line_consumer4<C>(mut self, conv: C) -> impl FnMut(&str) -> Result<(), io::Error>
53 where
54 C: Fn(&str) -> Result<[u8; 4], io::Error>,
55 {
56 move |line: &str| {
57 let b: [u8; 4] = conv(line)?;
58 self.wtr.write_all(&b)
59 }
60 }
61
62 pub fn into_line_consumer8<C>(mut self, conv: C) -> impl FnMut(&str) -> Result<(), io::Error>
63 where
64 C: Fn(&str) -> Result<[u8; 8], io::Error>,
65 {
66 move |line: &str| {
67 let b: [u8; 8] = conv(line)?;
68 self.wtr.write_all(&b)
69 }
70 }
71}
72
73pub struct Consumer<C> {
74 pub cns: C,
75}
76
77impl<C> Consumer<C>
78where
79 C: FnMut(&str) -> Result<(), io::Error>,
80{
81 pub fn consume_all<I>(mut self, lines: I) -> Result<(), io::Error>
82 where
83 I: Iterator<Item = Result<String, io::Error>>,
84 {
85 for rline in lines {
86 let line: String = rline?;
87 (self.cns)(&line)?;
88 }
89 Ok(())
90 }
91}
92
93#[derive(Clone, Copy)]
94pub enum Endian {
95 Lit,
96 Big,
97}
98
99impl Endian {
100 pub fn to_f2bin(self) -> impl Fn(f32) -> [u8; 4] {
101 match self {
102 Self::Lit => f2binl,
103 Self::Big => f2binb,
104 }
105 }
106
107 pub fn to_d2bin(self) -> impl Fn(f64) -> [u8; 8] {
108 match self {
109 Self::Lit => d2binl,
110 Self::Big => d2binb,
111 }
112 }
113}
114
115#[derive(Clone, Copy)]
116pub enum Size {
117 Single,
118 Double,
119}
120
121#[derive(Clone, Copy)]
122pub struct Config {
123 pub end: Endian,
124 pub siz: Size,
125}
126
127impl Config {
128 pub fn to_lines_consumer32<W, I>(&self, wtr: W) -> impl FnOnce(I) -> Result<(), io::Error>
129 where
130 W: Write,
131 I: Iterator<Item = Result<String, io::Error>>,
132 {
133 move |lines: I| {
134 let end: Endian = self.end;
135 let f2bin = end.to_f2bin();
136 let str2f2bin = str2f2bin_new(f2bin);
137 let w = Writer { wtr };
138 let cns = w.into_line_consumer4(str2f2bin);
139 Consumer { cns }.consume_all(lines)
140 }
141 }
142
143 pub fn to_lines_consumer64<W, I>(&self, wtr: W) -> impl FnOnce(I) -> Result<(), io::Error>
144 where
145 W: Write,
146 I: Iterator<Item = Result<String, io::Error>>,
147 {
148 move |lines: I| {
149 let end: Endian = self.end;
150 let d2bin = end.to_d2bin();
151 let str2d2bin = str2d2bin_new(d2bin);
152 let w = Writer { wtr };
153 let cns = w.into_line_consumer8(str2d2bin);
154 Consumer { cns }.consume_all(lines)
155 }
156 }
157}
158
159pub struct Reader<R> {
160 pub rdr: R,
161}
162
163impl<R> Reader<R>
164where
165 R: BufRead,
166{
167 pub fn lines2wtr<W>(self, mut wtr: W) -> impl FnOnce(Config) -> Result<(), io::Error>
168 where
169 W: Write,
170 {
171 move |cfg: Config| {
172 let lines = self.rdr.lines();
173 match cfg.siz {
174 Size::Single => cfg.to_lines_consumer32(&mut wtr)(lines),
175 Size::Double => cfg.to_lines_consumer64(&mut wtr)(lines),
176 }?;
177 wtr.flush()
178 }
179 }
180}
181
182pub fn stdin2lines2stdout(cfg: Config) -> Result<(), io::Error> {
183 let o = io::stdout();
184 let mut ol = o.lock();
185 let rdr = Reader {
186 rdr: io::stdin().lock(),
187 };
188 {
189 let l2w = rdr.lines2wtr(BufWriter::new(&mut ol));
190 l2w(cfg)?;
191 }
192 ol.flush()
193}