Convert all functions to using the wrapped input type.
This commit is contained in:
@@ -8,6 +8,9 @@ use nom::InputTakeAtPosition;
|
||||
use nom::Offset;
|
||||
use nom::Slice;
|
||||
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct OrgSource<'s> {
|
||||
full_source: &'s str,
|
||||
@@ -34,6 +37,10 @@ impl<'s> OrgSource<'s> {
|
||||
let start = self.preceding_line_break.unwrap_or(0);
|
||||
&self.full_source[start..self.start]
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.end - self.start
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> InputTake for OrgSource<'s> {
|
||||
@@ -68,6 +75,12 @@ impl<'s> From<&OrgSource<'s>> for &'s str {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> From<OrgSource<'s>> for &'s str {
|
||||
fn from(value: OrgSource<'s>) -> Self {
|
||||
&value.full_source[value.start..value.end]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s, R> Slice<R> for OrgSource<'s>
|
||||
where
|
||||
R: RangeBounds<usize>,
|
||||
@@ -217,6 +230,29 @@ impl<'s> InputTakeAtPosition for OrgSource<'s> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn convert_error(err: nom::Err<CustomError<OrgSource<'_>>>) -> nom::Err<CustomError<&str>> {
|
||||
match err {
|
||||
nom::Err::Incomplete(needed) => nom::Err::Incomplete(needed),
|
||||
nom::Err::Error(err) => nom::Err::Error(err.into()),
|
||||
nom::Err::Failure(err) => nom::Err::Failure(err.into()),
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> From<CustomError<OrgSource<'s>>> for CustomError<&'s str> {
|
||||
fn from(value: CustomError<OrgSource<'s>>) -> Self {
|
||||
match value {
|
||||
CustomError::MyError(err) => CustomError::MyError(err.into()),
|
||||
CustomError::Nom(input, error_kind) => CustomError::Nom(input.into(), error_kind),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> From<MyError<OrgSource<'s>>> for MyError<&'s str> {
|
||||
fn from(value: MyError<OrgSource<'s>>) -> Self {
|
||||
MyError(value.0.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user