asciidoc_parser/span/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
// Adapted from nom-span, which comes with the following license:
// Copyright 2023 Jules Guesnon
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the “Software”), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use std::ops::Deref;
/// Represents a subset of the overall UTF-8 input stream.
///
/// Annotated with 1-based line and column numbers relative to the
/// beginning of the overall input stream.
///
/// Called `Span` because its `data` member can be consumed
/// to yield another `Span` with annotations for the end of the
/// syntactic element in question.
///
/// ## How to use it?
///
/// Here is a basic example of how to create the input and how to retrieve all
/// the informations you need.
///
/// ```
/// # use asciidoc_parser::Span;
/// #
/// fn main() {
/// let span = Span::new(r#"{"hello": "world 🙌"}"#);
///
/// assert_eq!(span.line(), 1);
/// assert_eq!(span.col(), 1);
/// assert_eq!(span.byte_offset(), 0);
/// }
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Span<'src> {
data: &'src str,
line: usize,
col: usize,
offset: usize,
}
impl<'src> Span<'src> {
/// Create a new `Span` that describes an entire UTF-8 input stream.
pub fn new(data: &'src str) -> Self {
Self {
data,
line: 1,
col: 1,
offset: 0,
}
}
/// Get the current line number.
pub fn line(&self) -> usize {
self.line
}
/// Get the current column number.
pub fn col(&self) -> usize {
self.col
}
/// Get the current byte offset.
pub fn byte_offset(&self) -> usize {
self.offset
}
/// Get the current data in the span.
pub fn data(&self) -> &'src str {
self.data
}
}
impl<'src> AsRef<str> for Span<'src> {
fn as_ref(&self) -> &str {
self.data
}
}
impl<'src> Deref for Span<'src> {
type Target = &'src str;
fn deref(&self) -> &Self::Target {
&self.data
}
}
// NOTE: The `Span` API is large. Only the public interface is implemented here.
// The other modules referenced below implement additional APIs that are only
// available inside this crate only.
mod discard;
mod line;
mod matched_item;
mod primitives;
mod r#slice;
mod split;
mod take;
pub(crate) use matched_item::MatchedItem;
/// Any syntactic element can describe its location
/// within the source material using this trait.
pub trait HasSpan<'src> {
/// Return a [`Span`] describing the syntactic element's
/// location within the source string/file.
fn span(&'src self) -> &'src Span<'src>;
}