Skip to content

Commit 0d4cd96

Browse files
christollidayfacebook-github-bot
authored andcommitted
Add buck2_error::Error conversion for superconsole::SpanError
Summary: Seeing a lot of errors come from SpanError -> anyhow::Error -> buck2_error tagged with ErrorTag::Tier0, and I'm trying to reduce usage of ErrorTag::Tier0. These don't need to be converted to anyhow in the superconsole crate, keeping them as SpanError let's us convert directly into buck2_error with `From` to specify a tag in one place and clean up a bunch of code. The downside is making buck2_error depend on superconsole, if needed we could introduce a superconsole_error crate later (possibly along with adding structured errors for `draw` methods). Reviewed By: JakobDegen Differential Revision: D72018086 fbshipit-source-id: 996bdb5092202b37baea332378f13f43c6e000e6
1 parent d2ef83d commit 0d4cd96

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/content.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
pub use line::Line;
1313
pub use lines::Lines;
1414
pub use span::Span;
15+
pub use span::SpanError;
1516

1617
mod line;
1718
mod lines;

src/content/line.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use unicode_segmentation::UnicodeSegmentation;
2424

2525
use crate::vec_as_fmt_write::VecAsFmtWrite;
2626
use crate::Span;
27+
use crate::SpanError;
2728

2829
/// A `Line` is an abstraction for a collection of stylized or unstylized strings.
2930
/// Since each `Span` denotes a portion of a single line, an ordered collection represents a single line of text.
@@ -36,7 +37,7 @@ pub struct Line(
3637
);
3738

3839
impl Line {
39-
pub fn unstyled(text: &str) -> anyhow::Result<Line> {
40+
pub fn unstyled(text: &str) -> Result<Line, SpanError> {
4041
Ok(Line::from_iter([Span::new_unstyled(text)?]))
4142
}
4243

@@ -240,24 +241,24 @@ impl FromIterator<Span> for Line {
240241
}
241242

242243
impl TryFrom<Vec<String>> for Line {
243-
type Error = anyhow::Error;
244+
type Error = SpanError;
244245

245246
fn try_from(other: Vec<String>) -> Result<Self, Self::Error> {
246247
other
247248
.into_iter()
248249
.map(Span::new_unstyled)
249-
.collect::<anyhow::Result<Line>>()
250+
.collect::<Result<Line, SpanError>>()
250251
}
251252
}
252253

253254
impl TryFrom<Vec<&str>> for Line {
254-
type Error = anyhow::Error;
255+
type Error = SpanError;
255256

256257
fn try_from(other: Vec<&str>) -> Result<Self, Self::Error> {
257258
other
258259
.into_iter()
259260
.map(Span::new_unstyled)
260-
.collect::<anyhow::Result<Line>>()
261+
.collect::<Result<Line, SpanError>>()
261262
}
262263
}
263264

@@ -360,7 +361,7 @@ mod tests {
360361

361362
#[test]
362363
fn test_trim_ends() -> anyhow::Result<()> {
363-
let line = |spans: &[&str]| -> anyhow::Result<Line> { spans.to_vec().try_into() };
364+
let line = |spans: &[&str]| -> Result<Line, SpanError> { spans.to_vec().try_into() };
364365
let mut test = line(&["hello", "cat", "world"])?;
365366
test.trim_ends(0, 15);
366367
assert_eq!(test, line(&["hello", "cat", "world"])?);

src/content/span.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use unicode_segmentation::Graphemes;
2727
use unicode_segmentation::UnicodeSegmentation;
2828

2929
#[derive(Debug, thiserror::Error)]
30-
enum SpanError {
30+
pub enum SpanError {
3131
#[error("Word {0} contains non-space whitespace")]
3232
InvalidWhitespace(String),
3333
}
@@ -102,7 +102,7 @@ impl Span {
102102

103103
/// Attempt to create a new, unstyled span equivalent to the underlying stringlike.
104104
/// This will fail if the input string is not [`valid`](Span::valid).
105-
pub fn new_unstyled<S: std::fmt::Display>(stringlike: S) -> anyhow::Result<Self> {
105+
pub fn new_unstyled<S: std::fmt::Display>(stringlike: S) -> Result<Self, SpanError> {
106106
let owned = stringlike.to_string();
107107
if Self::valid(&owned) {
108108
Ok(Self {
@@ -111,7 +111,7 @@ impl Span {
111111
hyperlink: None,
112112
})
113113
} else {
114-
Err(SpanError::InvalidWhitespace(owned).into())
114+
Err(SpanError::InvalidWhitespace(owned))
115115
}
116116
}
117117

@@ -126,15 +126,15 @@ impl Span {
126126

127127
/// Equivalent to [`new_unstyled`](Span::new_unstyled), except with styling.
128128
// TODO(brasselsprouts): Does this have to be a `String`? probably not.
129-
pub fn new_styled(content: StyledContent<String>) -> anyhow::Result<Self> {
129+
pub fn new_styled(content: StyledContent<String>) -> Result<Self, SpanError> {
130130
if Self::valid(content.content()) {
131131
Ok(Self {
132132
content: Cow::Owned(content.content().clone()),
133133
style: *content.style(),
134134
hyperlink: None,
135135
})
136136
} else {
137-
Err(SpanError::InvalidWhitespace(content.content().to_owned()).into())
137+
Err(SpanError::InvalidWhitespace(content.content().to_owned()))
138138
}
139139
}
140140

@@ -148,7 +148,7 @@ impl Span {
148148
}
149149
}
150150

151-
pub fn new_colored(text: &str, color: Color) -> anyhow::Result<Self> {
151+
pub fn new_colored(text: &str, color: Color) -> Result<Self, SpanError> {
152152
Self::new_styled(StyledContent::new(
153153
ContentStyle {
154154
foreground_color: Some(color),
@@ -328,23 +328,23 @@ impl Span {
328328
}
329329

330330
impl TryFrom<String> for Span {
331-
type Error = anyhow::Error;
331+
type Error = SpanError;
332332

333333
fn try_from(value: String) -> Result<Self, Self::Error> {
334334
Self::new_unstyled(value)
335335
}
336336
}
337337

338338
impl TryFrom<&str> for Span {
339-
type Error = anyhow::Error;
339+
type Error = SpanError;
340340

341341
fn try_from(value: &str) -> Result<Self, Self::Error> {
342342
Self::new_unstyled(value)
343343
}
344344
}
345345

346346
impl TryFrom<StyledContent<String>> for Span {
347-
type Error = anyhow::Error;
347+
type Error = SpanError;
348348

349349
fn try_from(value: StyledContent<String>) -> Result<Self, Self::Error> {
350350
Self::new_styled(value)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub use components::DrawMode;
2929
pub use content::Line;
3030
pub use content::Lines;
3131
pub use content::Span;
32+
pub use content::SpanError;
3233
pub use dimensions::Dimensions;
3334
pub use dimensions::Direction;
3435

0 commit comments

Comments
 (0)