Skip to content

Commit bc65878

Browse files
committed
feat: panic when incorrectly using list builder
1 parent 9eb80bc commit bc65878

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# markdown-builder
1+
# Markdown Builder
22

3-
> Opinionated Rust crate for programmatically building markdown documents.
4-
5-
This project is based of the [markdown-composer](https://github.com/mainrs/markdown-composer-rs) crate by [mainrs](https://github.com/mainrs).
3+
**markdown-builder** is an opinionated Rust crate for programmatically building
4+
markdown documents. This project is based of the [markdown-composer] crate by
5+
[mainrs].
66

77
```shell
88
cargo add markdown-builder
@@ -63,3 +63,6 @@ This project was forked out of markdown-composer due to a multitude of reasons:
6363
- Markdown-composer looked unmaintained
6464
- Not wanting the additional settings to make it work for everyone
6565
- Word wrapping
66+
67+
[markdown-composer]: https://github.com/mainrs/markdown-composer-rs
68+
[mainrs]: https://github.com/mainrs

src/builders/list.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::types::{
66
#[derive(Clone, Debug, Default)]
77
pub struct ListBuilder {
88
items: Vec<ListItem>,
9+
has_checkmarks: bool,
910
}
1011

1112
impl ListBuilder {
@@ -18,17 +19,30 @@ impl ListBuilder {
1819
self
1920
}
2021

21-
/// Adds a checkmark using checkmark::CheckmarkItem.
22+
/// Adds a checkmark using [checkmark::CheckmarkItem].
2223
pub fn checkmark(mut self, item: impl Into<String>, checked: bool) -> Self {
2324
self.items.push(CheckmarkItem::from(item, checked).into());
25+
self.has_checkmarks = true;
2426
self
2527
}
2628

2729
pub fn ordered(self) -> List {
30+
if self.items.is_empty() {
31+
panic!("Attempt to bulid list without contents");
32+
}
33+
34+
if self.has_checkmarks {
35+
panic!("Attempt to build ordered list with checkboxes")
36+
}
37+
2838
List::ordered_with(self.items)
2939
}
3040

3141
pub fn unordered(self) -> List {
42+
if self.items.is_empty() {
43+
panic!("Attempt to bulid list without contents");
44+
}
45+
3246
List::unordered_with(self.items)
3347
}
3448
}
@@ -77,4 +91,25 @@ mod tests {
7791
"- [x] Eat spaghetti\n- [ ] Eat pizza\n- [x] Eat kebab\n"
7892
);
7993
}
94+
95+
#[test]
96+
#[should_panic]
97+
fn test_list_builder_unordered_no_elements_panic() {
98+
List::builder().unordered();
99+
}
100+
101+
#[test]
102+
#[should_panic]
103+
fn test_list_builder_ordered_no_elements_panic() {
104+
List::builder().ordered();
105+
}
106+
107+
#[test]
108+
#[should_panic]
109+
fn test_list_builder_ordered_checkmark_panic() {
110+
List::builder()
111+
.checkmark("Hello world", false)
112+
.checkmark("Checked", true)
113+
.ordered();
114+
}
80115
}

0 commit comments

Comments
 (0)