Skip to content

Commit 08f9ed4

Browse files
committed
Add conversion from Message to ParseError for less noisy invocations.
1 parent f46ca99 commit 08f9ed4

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

gix-error/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
/// A result type to hide the [Exn] error wrapper.
4242
mod exn;
4343

44+
pub use bstr;
4445
pub use exn::{ErrorExt, Exn, Frame, OptionExt, ResultExt, Something, Untyped};
4546

4647
/// An error type that wraps an inner type-erased boxed `std::error::Error` or an `Exn` frame.

gix-error/src/message.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt::{Debug, Display, Formatter};
55
#[derive(Debug)]
66
pub struct Message(
77
/// The error message.
8-
Cow<'static, str>,
8+
pub(crate) Cow<'static, str>,
99
);
1010

1111
/// Lifecycle
@@ -23,3 +23,15 @@ impl Display for Message {
2323
}
2424

2525
impl std::error::Error for Message {}
26+
27+
impl From<String> for Message {
28+
fn from(msg: String) -> Self {
29+
Message::new(msg)
30+
}
31+
}
32+
33+
impl From<&'static str> for Message {
34+
fn from(msg: &'static str) -> Self {
35+
Message::new(msg)
36+
}
37+
}

gix-error/src/parse.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::Message;
12
use bstr::BString;
23
use std::borrow::Cow;
34
use std::fmt::{Debug, Display, Formatter};
@@ -42,3 +43,21 @@ impl Display for ParseError {
4243
}
4344

4445
impl std::error::Error for ParseError {}
46+
47+
impl From<Message> for ParseError {
48+
fn from(Message(msg): Message) -> Self {
49+
ParseError::new(msg)
50+
}
51+
}
52+
53+
impl From<String> for ParseError {
54+
fn from(msg: String) -> Self {
55+
ParseError::new(msg)
56+
}
57+
}
58+
59+
impl From<&'static str> for ParseError {
60+
fn from(msg: &'static str) -> Self {
61+
ParseError::new(msg)
62+
}
63+
}

0 commit comments

Comments
 (0)