use std::num::NonZeroUsize;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BodyStart {
offset: usize,
line: NonZeroUsize,
}
impl BodyStart {
pub(crate) const fn new(offset: usize, line: NonZeroUsize) -> Self {
Self { offset, line }
}
#[must_use]
pub const fn line(&self) -> NonZeroUsize {
self.line
}
#[must_use]
pub fn body<'a>(&self, text: &'a str) -> &'a str {
text.get(self.offset..).unwrap_or("")
}
}
#[cfg(test)]
mod tests {
use super::*;
type TestResult = Result<(), Box<dyn std::error::Error>>;
#[test]
fn body_start_slices_body_and_reports_line() -> TestResult {
let text = "---\nname: x\n---\n# Title\n";
let start = BodyStart::new(16, NonZeroUsize::new(4).ok_or("line is non-zero")?);
assert_eq!(start.body(text), "# Title\n");
assert_eq!(start.line().get(), 4);
Ok(())
}
}