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
use crate::error::RadError;
use regex::Regex;
use std::io::BufRead;
use lazy_static::lazy_static;
lazy_static!{
pub static ref TRIM: Regex = Regex::new(r"^[ \t\r\n]+|[ \t\r\n]+$").unwrap();
}
#[cfg(feature = "color")]
use colored::*;
pub(crate) struct Utils;
impl Utils {
pub(crate) fn local_name(level: usize, name : &str) -> String {
format!("{}.{}", level, name)
}
pub(crate) fn trim(args: &str) -> String {
let result = TRIM.replace_all(args, "");
result.to_string()
}
pub fn full_lines(mut input: impl BufRead) -> impl Iterator<Item = std::io::Result<String>> {
std::iter::from_fn(move || {
let mut vec = String::new();
match input.read_line(&mut vec) {
Ok(0) => None,
Ok(_) => Some(Ok(vec)),
Err(e) => Some(Err(e)),
}
})
}
pub(crate) fn is_blank_char(ch: char) -> bool {
ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
}
pub(crate) fn is_arg_true(arg: &str) -> Result<bool, RadError> {
let arg = Utils::trim(arg);
if let Ok(value) = arg.parse::<usize>() {
if value == 0 {
return Ok(false);
} else {
return Ok(true);
}
} else {
if arg.to_lowercase() == "true" {
return Ok(true);
} else if arg.to_lowercase() == "false" {
return Ok(false);
}
}
return Err(RadError::InvalidArgument("Neither true nor false".to_owned()));
}
pub(crate) fn utf8_substring(source: &str, min: Option<usize>, max: Option<usize>) -> String {
let mut result = String::new();
if let Some(min) = min {
if let Some(max) = max {
for (idx,ch) in source.chars().enumerate() {
if idx >= min && idx <= max {
result.push(ch);
}
}
} else {
for (idx,ch) in source.chars().enumerate() {
if idx >= min {
result.push(ch);
}
}
}
} else {
if let Some(max) = max {
for (idx,ch) in source.chars().enumerate() {
if idx <= max {
result.push(ch);
}
}
} else {
return source.to_owned();
}
}
return result;
}
pub fn green(string : &str) -> Box<dyn std::fmt::Display> {
if cfg!(feature = "color") {
#[cfg(feature = "color")]
return Box::new(string.green().to_owned());
}
Box::new(string.to_owned())
}
pub fn red(string : &str) -> Box<dyn std::fmt::Display> {
if cfg!(feature = "color") {
#[cfg(feature = "color")]
return Box::new(string.red().to_owned());
}
Box::new(string.to_owned())
}
pub fn yellow(string : &str) -> Box<dyn std::fmt::Display> {
if cfg!(feature = "color") {
#[cfg(feature = "color")]
return Box::new(string.yellow().to_owned());
}
Box::new(string.to_owned())
}
#[allow(dead_code)]
pub(crate) fn count_sentences(s: &str) -> usize {
s.as_bytes().iter().filter(|&&c| c == b'\n').count() + 1
}
#[cfg(feature = "debug")]
pub fn clear_terminal() -> Result<(), RadError> {
use crossterm::{ExecutableCommand, terminal::ClearType};
std::io::stdout()
.execute(crossterm::terminal::Clear(ClearType::All))?
.execute(crossterm::cursor::MoveTo(0,0))?;
Ok(())
}
pub fn is_real_path(path: &std::path::Path) -> Result<(), RadError> {
if !path.exists() {
return Err(RadError::InvalidFile(path.display().to_string()));
}
Ok(())
}
}