mirror of
https://github.com/zaphar/abortable_parser.git
synced 2025-07-22 20:39:50 -04:00
MAINT: Rustdoc for the Error and Offsetable types.
This commit is contained in:
parent
170e74209d
commit
bb3502d6fd
11
src/lib.rs
11
src/lib.rs
@ -2,6 +2,7 @@
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
|
|
||||||
|
/// A trait for types that can have an offset as a count of processed items.
|
||||||
pub trait Offsetable {
|
pub trait Offsetable {
|
||||||
fn get_offset(&self) -> usize;
|
fn get_offset(&self) -> usize;
|
||||||
}
|
}
|
||||||
@ -15,6 +16,9 @@ impl Offsetable for usize {
|
|||||||
/// A Cloneable Iterator that can report an offset as a count of processed Items.
|
/// A Cloneable Iterator that can report an offset as a count of processed Items.
|
||||||
pub trait InputIter: Iterator + Clone + Offsetable {}
|
pub trait InputIter: Iterator + Clone + Offsetable {}
|
||||||
|
|
||||||
|
/// The custom error type for use in `Result::{Fail, Abort}`.
|
||||||
|
/// Stores a wrapped err that must implement Display as well as an offset and
|
||||||
|
/// an optional cause.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error<E: Display> {
|
pub struct Error<E: Display> {
|
||||||
err: E,
|
err: E,
|
||||||
@ -23,7 +27,7 @@ pub struct Error<E: Display> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Display> Error<E> {
|
impl<E: Display> Error<E> {
|
||||||
// Constructs a new Error with an offset and no cause.
|
/// Constructs a new Error with an offset and no cause.
|
||||||
pub fn new<S: Offsetable>(err: E, offset: &S) -> Self {
|
pub fn new<S: Offsetable>(err: E, offset: &S) -> Self {
|
||||||
Error {
|
Error {
|
||||||
err: err,
|
err: err,
|
||||||
@ -32,7 +36,7 @@ impl<E: Display> Error<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Constructs a new Error with an offset and a cause.
|
/// Constructs a new Error with an offset and a cause.
|
||||||
pub fn caused_by<S: Offsetable>(err: E, offset: &S, cause: Self) -> Self {
|
pub fn caused_by<S: Offsetable>(err: E, offset: &S, cause: Self) -> Self {
|
||||||
Error {
|
Error {
|
||||||
err: err,
|
err: err,
|
||||||
@ -41,10 +45,12 @@ impl<E: Display> Error<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the contained err.
|
||||||
pub fn get_err<'a>(&'a self) -> &'a E {
|
pub fn get_err<'a>(&'a self) -> &'a E {
|
||||||
&self.err
|
&self.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `Some(cause)` if there is one, None otherwise.
|
||||||
pub fn get_cause<'a>(&'a self) -> Option<&'a Error<E>> {
|
pub fn get_cause<'a>(&'a self) -> Option<&'a Error<E>> {
|
||||||
match self.cause {
|
match self.cause {
|
||||||
Some(ref cause) => Some(cause),
|
Some(ref cause) => Some(cause),
|
||||||
@ -52,6 +58,7 @@ impl<E: Display> Error<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the offset at which this Error happened.
|
||||||
pub fn get_offset(&self) -> usize {
|
pub fn get_offset(&self) -> usize {
|
||||||
self.offset
|
self.offset
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user