use super::as_u32;
use super::node::{Argument, IdentOffset};
use crate::parser::stream::Input;
#[derive(Clone, Copy, Debug)]
pub struct OptNameOffset {
pub(super) start: u32,
}
#[derive(Clone, Debug, Copy)]
pub struct OptIdentOffset {
pub(super) start: u32,
}
impl OptNameOffset {
pub fn get_with_span<'i>(self, input: &Input<'i>) -> Option<(&'i [u8], (u32, u32))> {
if self.start == u32::MAX {
return None;
}
let ident = unsafe { input.starting_at(self.start).next_statement_name() };
let end = self.start + as_u32(ident.len());
Some((ident, (self.start, end)))
}
}
impl OptIdentOffset {
pub fn read_spanned<'i>(self, input: &Input<'i>) -> Option<(&'i [u8], (u32, u32))> {
if self.start == u32::MAX {
return None;
}
Some(IdentOffset { start: self.start }.read_spanned(input))
}
}
impl IdentOffset {
pub fn read_spanned<'i>(self, input: &Input<'i>) -> (&'i [u8], (u32, u32)) {
let ident = unsafe { input.starting_at(self.start).next_ident() };
(ident, (self.start, self.start + as_u32(ident.len())))
}
pub fn read<'i>(self, input: &Input<'i>) -> &'i [u8] {
self.read_spanned(input).0
}
}
impl Argument<'_> {
pub fn read<'i>(self, input: &Input<'i>) -> &'i [u8] {
let (start, end) = (self.start() as usize, self.end() as usize);
&input.input_u8()[start..end]
}
}
#[rustfmt::skip] impl From<u32> for IdentOffset { fn from(start: u32) -> Self { Self { start } } }
#[rustfmt::skip] impl From<Option<IdentOffset>> for OptIdentOffset {
fn from(value: Option<IdentOffset>) -> Self { Self { start: value.map_or(u32::MAX, |i| i.start) } }
}
#[rustfmt::skip] impl From<Option<u32>> for OptNameOffset {
fn from(value: Option<u32>) -> Self { Self { start: value.unwrap_or(u32::MAX) } }
}