Skip to content

Commit c1a3852

Browse files
committed
Remove UserBoundsList::new
1 parent d43fddb commit c1a3852

File tree

3 files changed

+41
-56
lines changed

3 files changed

+41
-56
lines changed

src/bounds.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,20 @@ impl From<Vec<BoundOrFiller>> for UserBoundsList {
122122
let mut rightmost_bound: Option<Side> = None;
123123
let mut last_bound: Option<&mut UserBounds> = None;
124124

125-
// This is risky, we could end up using last_interesting_field
126-
// internally. Didn't spend much time to figure out how to use
127-
// is_sortable without major refactoring.
128-
if ubl.is_sortable() {
129-
ubl.list.iter_mut().for_each(|bof| {
130-
if let BoundOrFiller::Bound(b) = bof {
131-
if rightmost_bound.is_none() || b.r > rightmost_bound.unwrap() {
132-
rightmost_bound = Some(b.r);
133-
}
134-
135-
last_bound = Some(b);
125+
let is_sortable = ubl.is_sortable();
126+
127+
ubl.list.iter_mut().for_each(|bof| {
128+
if let BoundOrFiller::Bound(b) = bof {
129+
if rightmost_bound.is_none() || b.r > rightmost_bound.unwrap() {
130+
rightmost_bound = Some(b.r);
136131
}
137-
});
132+
133+
last_bound = Some(b);
134+
}
135+
});
136+
137+
if !is_sortable {
138+
rightmost_bound = None;
138139
}
139140

140141
last_bound
@@ -149,15 +150,14 @@ impl From<Vec<BoundOrFiller>> for UserBoundsList {
149150
impl FromStr for UserBoundsList {
150151
type Err = anyhow::Error;
151152
fn from_str(s: &str) -> Result<Self, Self::Err> {
153+
if s.trim().is_empty() {
154+
bail!("UserBoundsList must contain at least one UserBounds");
155+
}
152156
Ok(parse_bounds_list(s)?.into())
153157
}
154158
}
155159

156160
impl UserBoundsList {
157-
pub fn new(list: Vec<BoundOrFiller>) -> Self {
158-
list.into()
159-
}
160-
161161
/// Detect whether the list can be sorted.
162162
/// It can be sorted only if every bound
163163
/// has the same sign (all positive or all negative).
@@ -423,14 +423,15 @@ impl UserBoundsTrait<i32> for UserBounds {
423423
/// # use tuc::bounds::{UserBounds, UserBoundsTrait};
424424
/// # use std::ops::Range;
425425
/// # use tuc::bounds::Side;
426+
/// # use std::str::FromStr;
426427
///
427428
/// assert_eq!(
428-
/// (UserBounds { l: Side::Some(1), r: Side::Some(2) }).try_into_range(5).unwrap(),
429+
/// UserBounds::from_str("1:2").unwrap().try_into_range(5).unwrap(),
429430
/// Range { start: 0, end: 2} // 2, not 1, because it's exclusive
430431
/// );
431432
///
432433
/// assert_eq!(
433-
/// (UserBounds { l: Side::Some(1), r: Side::Continue }).try_into_range(5).unwrap(),
434+
/// UserBounds::from_str("1:").unwrap().try_into_range(5).unwrap(),
434435
/// Range { start: 0, end: 5}
435436
/// );
436437
/// ```
@@ -769,9 +770,12 @@ mod tests {
769770
}
770771

771772
#[test]
772-
fn test_user_bounds_is_sortable() {
773-
assert!(UserBoundsList::new(Vec::new()).is_sortable());
773+
fn test_user_bounds_cannot_be_empty() {
774+
assert!(UserBoundsList::from_str("").is_err());
775+
}
774776

777+
#[test]
778+
fn test_user_bounds_is_sortable() {
775779
assert!(UserBoundsList::from_str("1").unwrap().is_sortable());
776780

777781
assert!(UserBoundsList::from_str("1,2").unwrap().is_sortable());
@@ -787,8 +791,6 @@ mod tests {
787791

788792
#[test]
789793
fn test_vec_of_bounds_is_sorted() {
790-
assert!(UserBoundsList::from_str("").unwrap().is_sorted());
791-
792794
assert!(UserBoundsList::from_str("1").unwrap().is_sorted());
793795

794796
assert!(UserBoundsList::from_str("1,2").unwrap().is_sorted());

src/cut_lines.rs

+15-32
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ pub fn read_and_cut_lines<A: BufRead, B: Write>(
131131

132132
#[cfg(test)]
133133
mod tests {
134+
use std::str::FromStr;
135+
134136
use crate::{
135-
bounds::{BoundsType, UserBounds, UserBoundsList},
137+
bounds::{BoundsType, UserBoundsList},
136138
options::EOL,
137139
};
138140

@@ -147,29 +149,10 @@ mod tests {
147149
}
148150
}
149151

150-
fn bof_f1() -> BoundOrFiller {
151-
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Some(1)))
152-
}
153-
154-
fn bof_f2() -> BoundOrFiller {
155-
BoundOrFiller::Bound(UserBounds::new(Side::Some(2), Side::Some(2)))
156-
}
157-
fn bof_f3() -> BoundOrFiller {
158-
BoundOrFiller::Bound(UserBounds::new(Side::Some(3), Side::Some(3)))
159-
}
160-
fn bof_r2_3() -> BoundOrFiller {
161-
BoundOrFiller::Bound(UserBounds::new(Side::Some(2), Side::Some(3)))
162-
}
163-
fn bof_neg1() -> BoundOrFiller {
164-
BoundOrFiller::Bound(UserBounds::new(Side::Some(-1), Side::Some(-1)))
165-
}
166-
fn bof_f1_to_end() -> BoundOrFiller {
167-
BoundOrFiller::Bound(UserBounds::new(Side::Some(1), Side::Continue))
168-
}
169152
#[test]
170153
fn fwd_cut_one_field() {
171154
let mut opt = make_lines_opt();
172-
opt.bounds = UserBoundsList::new(vec![bof_f1()]);
155+
opt.bounds = UserBoundsList::from_str("1").unwrap();
173156

174157
let mut input = b"a\nb".as_slice();
175158
let mut output = Vec::with_capacity(100);
@@ -180,7 +163,7 @@ mod tests {
180163
#[test]
181164
fn fwd_cut_multiple_fields() {
182165
let mut opt = make_lines_opt();
183-
opt.bounds = UserBoundsList::new(vec![bof_f1(), bof_f2()]);
166+
opt.bounds = UserBoundsList::from_str("1:2").unwrap();
184167

185168
let mut input = b"a\nb".as_slice();
186169
let mut output = Vec::with_capacity(100);
@@ -191,7 +174,7 @@ mod tests {
191174
#[test]
192175
fn fwd_support_ranges() {
193176
let mut opt = make_lines_opt();
194-
opt.bounds = UserBoundsList::new(vec![bof_f1(), bof_r2_3()]);
177+
opt.bounds = UserBoundsList::from_str("1,2:3").unwrap();
195178

196179
let mut input = b"a\nb\nc".as_slice();
197180
let mut output = Vec::with_capacity(100);
@@ -202,7 +185,7 @@ mod tests {
202185
#[test]
203186
fn fwd_supports_no_join() {
204187
let mut opt = make_lines_opt();
205-
opt.bounds = UserBoundsList::new(vec![bof_f1(), bof_f3()]);
188+
opt.bounds = UserBoundsList::from_str("1,3").unwrap();
206189
opt.join = false;
207190

208191
let mut input = b"a\nb\nc".as_slice();
@@ -214,7 +197,7 @@ mod tests {
214197
#[test]
215198
fn fwd_supports_no_right_bound() {
216199
let mut opt = make_lines_opt();
217-
opt.bounds = UserBoundsList::new(vec![bof_f1_to_end()]);
200+
opt.bounds = UserBoundsList::from_str("1:").unwrap();
218201

219202
let mut input = b"a\nb".as_slice();
220203
let mut output = Vec::with_capacity(100);
@@ -225,7 +208,7 @@ mod tests {
225208
#[test]
226209
fn fwd_handle_out_of_bounds() {
227210
let mut opt = make_lines_opt();
228-
opt.bounds = UserBoundsList::new(vec![bof_f3()]);
211+
opt.bounds = UserBoundsList::from_str("3").unwrap();
229212
opt.join = true;
230213

231214
let mut input = b"a\nb".as_slice();
@@ -237,7 +220,7 @@ mod tests {
237220
#[test]
238221
fn fwd_ignore_last_empty() {
239222
let mut opt = make_lines_opt();
240-
opt.bounds = UserBoundsList::new(vec![bof_f3()]);
223+
opt.bounds = UserBoundsList::from_str("3").unwrap();
241224

242225
let mut input1 = b"a\nb".as_slice();
243226
let mut input2 = b"a\nb\n".as_slice();
@@ -255,7 +238,7 @@ mod tests {
255238
#[test]
256239
fn cut_lines_handle_negative_idx() {
257240
let mut opt = make_lines_opt();
258-
opt.bounds = UserBoundsList::new(vec![bof_neg1()]);
241+
opt.bounds = UserBoundsList::from_str("-1").unwrap();
259242

260243
let mut input = b"a\nb".as_slice();
261244
let mut output = Vec::with_capacity(100);
@@ -266,7 +249,7 @@ mod tests {
266249
#[test]
267250
fn cut_lines_ignore_last_empty_when_using_positive_idx() {
268251
let mut opt = make_lines_opt();
269-
opt.bounds = UserBoundsList::new(vec![bof_f3()]);
252+
opt.bounds = UserBoundsList::from_str("3").unwrap();
270253

271254
let mut input1 = b"a\nb".as_slice();
272255
let mut input2 = b"a\nb\n".as_slice();
@@ -284,7 +267,7 @@ mod tests {
284267
#[test]
285268
fn cut_lines_ignore_last_empty_when_using_negative_idx() {
286269
let mut opt = make_lines_opt();
287-
opt.bounds = UserBoundsList::new(vec![bof_neg1()]);
270+
opt.bounds = UserBoundsList::from_str("-1").unwrap();
288271

289272
let mut input1 = b"a\nb".as_slice();
290273
let mut input2 = b"a\nb\n".as_slice();
@@ -302,7 +285,7 @@ mod tests {
302285
#[test]
303286
fn fwd_cut_zero_delimited() {
304287
let mut opt = make_lines_opt();
305-
opt.bounds = UserBoundsList::new(vec![bof_f1()]);
288+
opt.bounds = UserBoundsList::from_str("1").unwrap();
306289
opt.eol = EOL::Zero;
307290
opt.delimiter = String::from("\0");
308291

@@ -315,7 +298,7 @@ mod tests {
315298
#[test]
316299
fn cut_lines_zero_delimited() {
317300
let mut opt = make_lines_opt();
318-
opt.bounds = UserBoundsList::new(vec![bof_f1()]);
301+
opt.bounds = UserBoundsList::from_str("1").unwrap();
319302
opt.eol = EOL::Zero;
320303
opt.delimiter = String::from("\0");
321304

src/options.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::bounds::{BoundOrFiller, BoundsType, UserBounds, UserBoundsList};
1+
use crate::bounds::{BoundsType, UserBoundsList};
22
use anyhow::Result;
33
use std::str::FromStr;
44

@@ -54,7 +54,7 @@ impl Default for Opt {
5454
Opt {
5555
delimiter: String::from("-"),
5656
eol: EOL::Newline,
57-
bounds: UserBoundsList::new(vec![BoundOrFiller::Bound(UserBounds::default())]),
57+
bounds: UserBoundsList::from_str("1:").unwrap(),
5858
bounds_type: BoundsType::Fields,
5959
only_delimited: false,
6060
greedy_delimiter: false,

0 commit comments

Comments
 (0)