1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use core::fmt::{Display, Write, Formatter, self};
use std::io;
use std::io::BufRead;
use std::collections::VecDeque;
pub fn trim_str(target: &impl AsRef<str>, maxlen: usize) -> impl Display + '_ {
TrimStr {
target: target.as_ref(),
maxlen,
}
}
struct TrimStr<'s> {
target: &'s str,
maxlen: usize,
}
impl Display for TrimStr<'_> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
if self.target.len() > self.maxlen {
write!(fmt, "{}...", &self.target[..(self.maxlen-3)])
} else {
fmt.write_str(self.target)
}
}
}
pub fn title_case(s: &impl AsRef<str>) -> impl Display + '_ {
TitleCase { target: s.as_ref() }
}
struct TitleCase<'s> {
target: &'s str,
}
impl Display for TitleCase<'_> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
let mut prev = None;
for next in self.target.chars() {
if next.is_alphabetic() && prev.map_or(true, char::is_whitespace) {
for c in next.to_uppercase() {
fmt.write_char(c)?;
}
} else {
fmt.write_char(next)?;
}
prev.replace(next);
}
Ok(())
}
}
pub fn fmt_join<'a>(sep: impl Display + 'a, items: &'a [impl Display]) -> impl Display + 'a {
DisplayJoin { sep, items }
}
struct DisplayJoin<'a, S, D> where S: Display, D: Display {
sep: S,
items: &'a [D],
}
impl<S, D> Display for DisplayJoin<'_, S, D> where S: Display, D: Display {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
for item in self.items.iter().take(self.items.len() - 1) {
item.fmt(fmt)?;
self.sep.fmt(fmt)?;
}
if let Some(item) = self.items.last() {
item.fmt(fmt)?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct ReadChars<R> where R: BufRead {
read: R,
linebuf: String,
charbuf: VecDeque<char>,
}
impl<R> ReadChars<R> where R: BufRead {
pub fn new(read: R) -> Self {
ReadChars {
read,
linebuf: String::new(),
charbuf: VecDeque::new(),
}
}
}
impl<R> Iterator for ReadChars<R> where R: BufRead {
type Item = io::Result<char>;
fn next(&mut self) -> Option<io::Result<char>> {
let next = self.charbuf.pop_front().map(Ok);
if next.is_some() {
return next;
}
self.linebuf.clear();
let mut safety = 0;
while self.linebuf.is_empty() && safety < 0xFFFF {
match self.read.read_line(&mut self.linebuf) {
Err(error) => return Some(Err(error)),
Ok(0) => return None,
_ => { safety += 1 },
}
}
self.charbuf.extend(self.linebuf.chars());
self.charbuf.pop_front().map(Ok)
}
}
pub fn make_display<F>(fmt_func: F) -> impl fmt::Display where F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result {
FnFormatter { fmt_func }
}
struct FnFormatter<F> where F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result {
fmt_func: F,
}
impl<F> fmt::Display for FnFormatter<F> where F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.fmt_func)(fmt)
}
}
pub fn format_error(fmt: &mut fmt::Formatter<'_>, title: &str, message: Option<&str>, source: Option<&dyn std::error::Error>) -> fmt::Result {
let message =
if let Some("") = message { None }
else { message };
match (message, source) {
(None, None) => fmt.write_str(title),
(None, Some(error)) => write!(fmt, "{}: {}", title, error),
(Some(message), None) => write!(fmt, "{}: {}", title, message),
(Some(message), Some(error)) => write!(fmt, "{}: {}: {}", title, message, error),
}
}