asciidork_ast/
source_string.rs

1use std::ops::Deref;
2
3use crate::internal::*;
4
5#[derive(PartialEq, Eq, Clone, Hash)]
6pub struct SourceString<'arena> {
7  pub src: BumpString<'arena>,
8  pub loc: SourceLocation,
9}
10
11impl<'arena> SourceString<'arena> {
12  pub const fn new(src: BumpString<'arena>, loc: SourceLocation) -> Self {
13    Self { src, loc }
14  }
15
16  pub fn split_once(self, separator: &str, bump: &'arena Bump) -> (Self, Option<Self>) {
17    match self.src.split_once(separator) {
18      Some((left, right)) => (
19        Self::new(
20          BumpString::from_str_in(left, bump),
21          SourceLocation::new(
22            self.loc.start,
23            self.loc.start + left.len() as u32,
24            self.loc.include_depth,
25          ),
26        ),
27        Some(Self::new(
28          BumpString::from_str_in(right, bump),
29          SourceLocation::new(
30            self.loc.start + left.len() as u32 + separator.len() as u32,
31            self.loc.end,
32            self.loc.include_depth,
33          ),
34        )),
35      ),
36      None => (self, None),
37    }
38  }
39
40  pub fn drop_first(&mut self) {
41    self.src.drain(..1);
42    self.loc.start += 1;
43  }
44}
45
46impl Deref for SourceString<'_> {
47  type Target = str;
48
49  fn deref(&self) -> &Self::Target {
50    &self.src
51  }
52}
53
54impl AsRef<str> for SourceString<'_> {
55  fn as_ref(&self) -> &str {
56    &self.src
57  }
58}
59
60impl std::cmp::PartialEq<str> for SourceString<'_> {
61  fn eq(&self, other: &str) -> bool {
62    self.src == other
63  }
64}
65
66impl std::fmt::Debug for SourceString<'_> {
67  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68    write!(f, "SourceString{{\"{}\",{:?}}}", self.src, self.loc)
69  }
70}