Skip to content

Commit 28fccb8

Browse files
committed
brackets: simplify required interface
The Brackets struct has a single purpose: To allow `is_balanced` to be called. It forces an object-style without any advantages over a simple function.
1 parent c2e2c2d commit 28fccb8

File tree

5 files changed

+25
-45
lines changed

5 files changed

+25
-45
lines changed

exercises/bracket-push/.meta/hints.md

Lines changed: 0 additions & 6 deletions
This file was deleted.

exercises/bracket-push/README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@ Given a string containing brackets `[]`, braces `{}`, parentheses `()`,
44
or any combination thereof, verify that any and all pairs are matched
55
and nested correctly.
66

7-
# Bracket Push in Rust
8-
9-
Reading about these Rust topics may help you implement a solution.
10-
11-
- Lifetimes and Structs: https://doc.rust-lang.org/book/second-edition/ch10-03-lifetime-syntax.html#lifetime-annotations-in-method-definitions
12-
- From trait: https://doc.rust-lang.org/std/convert/trait.From.html
13-
14-
157
## Rust Installation
168

179
Refer to the [exercism help page][help-page] for Rust installation and learning

exercises/bracket-push/example.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::collections::HashMap;
22

3-
pub struct Brackets {
3+
pub fn brackets_are_balanced(string: &str) -> bool {
4+
Brackets::from(string).are_balanced()
5+
}
6+
7+
struct Brackets {
48
raw_brackets: Vec<char>,
59
pairs: MatchingBrackets,
610
}
@@ -12,7 +16,7 @@ impl<'a> From<&'a str> for Brackets {
1216
}
1317

1418
impl Brackets {
15-
pub fn new(s: String, pairs: Option<Vec<(char, char)>>) -> Self {
19+
fn new(s: String, pairs: Option<Vec<(char, char)>>) -> Self {
1620
let p = match pairs {
1721
Some(x) => MatchingBrackets::from(x),
1822
None => MatchingBrackets::from(vec![('[', ']'), ('{', '}'), ('(', ')')]),
@@ -24,7 +28,7 @@ impl Brackets {
2428
}
2529
}
2630

27-
pub fn are_balanced(&self) -> bool {
31+
fn are_balanced(&self) -> bool {
2832
let mut unclosed: Vec<char> = Vec::new();
2933

3034
for &bracket in self.raw_brackets.iter() {
@@ -39,7 +43,7 @@ impl Brackets {
3943
}
4044
}
4145

42-
pub struct MatchingBrackets {
46+
struct MatchingBrackets {
4347
collection: HashMap<char, char>,
4448
}
4549

exercises/bracket-push/src/lib.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
pub struct Brackets;
2-
3-
impl<'a> From<&'a str> for Brackets {
4-
fn from(input: &str) -> Self {
5-
unimplemented!("From the '{}' input construct a new Brackets struct", input);
6-
}
7-
}
8-
9-
impl Brackets {
10-
pub fn are_balanced(&self) -> bool {
11-
unimplemented!("Check if your Brackets struct contains balanced brackets");
12-
}
1+
pub fn brackets_are_balanced(string: &str) -> bool {
2+
unimplemented!("Check if the string \"{}\" contains balanced brackets", string);
133
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
11
extern crate bracket_push;
22

3-
use bracket_push::*;
3+
use bracket_push::brackets_are_balanced;
44

55
#[test]
66
fn paired_square_brackets() {
7-
assert!(Brackets::from("[]").are_balanced());
7+
assert!(brackets_are_balanced("[]"));
88
}
99

1010
#[test]
1111
#[ignore]
1212
fn empty_string() {
13-
assert!(Brackets::from("").are_balanced());
13+
assert!(brackets_are_balanced(""));
1414
}
1515

1616
#[test]
1717
#[ignore]
1818
fn unpaired_brackets() {
19-
assert!(!Brackets::from("[[").are_balanced());
19+
assert!(!brackets_are_balanced("[["));
2020
}
2121

2222
#[test]
2323
#[ignore]
2424
fn wrong_ordered_brackets() {
25-
assert!(!Brackets::from("}{").are_balanced());
25+
assert!(!brackets_are_balanced("}{"));
2626
}
2727

2828
#[test]
2929
#[ignore]
3030
fn wrong_closing_bracket() {
31-
assert!(!Brackets::from("{]").are_balanced());
31+
assert!(!brackets_are_balanced("{]"));
3232
}
3333

3434
#[test]
3535
#[ignore]
3636
fn paired_with_whitespace() {
37-
assert!(Brackets::from("{ }").are_balanced());
37+
assert!(brackets_are_balanced("{ }"));
3838
}
3939

4040
#[test]
4141
#[ignore]
4242
fn simple_nested_brackets() {
43-
assert!(Brackets::from("{[]}").are_balanced());
43+
assert!(brackets_are_balanced("{[]}"));
4444
}
4545

4646
#[test]
4747
#[ignore]
4848
fn several_paired_brackets() {
49-
assert!(Brackets::from("{}[]").are_balanced());
49+
assert!(brackets_are_balanced("{}[]"));
5050
}
5151

5252
#[test]
5353
#[ignore]
5454
fn paired_and_nested_brackets() {
55-
assert!(Brackets::from("([{}({}[])])").are_balanced());
55+
assert!(brackets_are_balanced("([{}({}[])])"));
5656
}
5757

5858
#[test]
5959
#[ignore]
6060
fn unopened_closing_brackets() {
61-
assert!(!Brackets::from("{[)][]}").are_balanced());
61+
assert!(!brackets_are_balanced("{[)][]}"));
6262
}
6363

6464
#[test]
6565
#[ignore]
6666
fn unpaired_and_nested_brackets() {
67-
assert!(!Brackets::from("([{])").are_balanced());
67+
assert!(!brackets_are_balanced("([{])"));
6868
}
6969

7070
#[test]
7171
#[ignore]
7272
fn paired_and_wrong_nested_brackets() {
73-
assert!(!Brackets::from("[({]})").are_balanced());
73+
assert!(!brackets_are_balanced("[({]})"));
7474
}
7575

7676
#[test]
7777
#[ignore]
7878
fn math_expression() {
79-
assert!(Brackets::from("(((185 + 223.85) * 15) - 543)/2").are_balanced());
79+
assert!(brackets_are_balanced("(((185 + 223.85) * 15) - 543)/2"));
8080
}
8181

8282
#[test]
8383
#[ignore]
8484
fn complex_latex_expression() {
8585
let input = "\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \
8686
\\end{array}\\right)";
87-
assert!(Brackets::from(input).are_balanced());
87+
assert!(brackets_are_balanced(input));
8888
}

0 commit comments

Comments
 (0)