Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Apr 2, 2024
1 parent f53c4ff commit 6c004ba
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ license = "MIT"
quoth-macros = { path = "quoth-macros", version = "0.1.5" }
regex = "1.10"
rust_decimal = "1"
safe-string = "0.1.1"
safe-string = "0.1.5"
24 changes: 14 additions & 10 deletions src/source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! home of [`Source`] and related types.

#[cfg(doc)]
use super::*;

use std::{
Expand All @@ -11,36 +10,41 @@ use std::{
/// Represents source text that can be indexed into to define individual [`Span`]s.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Source {
text: String,
text: IndexedString,
path: Option<PathBuf>,
}

impl Source {
/// Returns the underlying text of this [`Source`], with original formatting.
pub fn source_text(&self) -> &str {
&self.text
pub fn source_text(&self) -> IndexedSlice {
self.text.as_slice()
}

/// Returns the path of the file that this [`Source`] was read from, if it was read from a file.
pub fn source_path(&self) -> Option<&Path> {
self.path.as_ref().map(|path| path.as_path())
}

/// Returns the length of the underlying text of this [`Source`].
pub fn from_str(string: impl Into<String>) -> Self {
/// Creates a new [`Source`] from a string.
pub fn from_str(string: impl AsRef<str>) -> Self {
Source {
text: string.into(),
text: IndexedString::from_str(string.as_ref()),
path: None,
}
}

/// Creates a new [`Source`] from an [`IndexedString`].
pub fn from_indexed_string(text: IndexedString) -> Self {
Source { text, path: None }
}

/// Reads the contents of a file and returns a [`Source`] with the file's text.
///
/// Since no parsing is done at this stage, only IO or encoding errors will be returned,
/// regardless of the validity of the syntax in the file.
pub fn from_file(path: impl AsRef<Path>) -> core::result::Result<Self, std::io::Error> {
std::fs::read_to_string(path.as_ref()).map(|text| Source {
text,
text: IndexedString::from(&text),
path: Some(path.as_ref().to_path_buf()),
})
}
Expand All @@ -52,7 +56,7 @@ impl Source {
}

impl Deref for Source {
type Target = String;
type Target = IndexedString;

fn deref(&self) -> &Self::Target {
&self.text
Expand All @@ -62,7 +66,7 @@ impl Deref for Source {
impl<S: ToString> From<S> for Source {
fn from(value: S) -> Self {
Source {
text: value.to_string(),
text: IndexedString::from(value.to_string()),
path: None,
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl Span {
}

/// Returns the text of the [`Source`] that this [`Span`] is associated with.
pub fn source_text(&self) -> &str {
&self.source[self.byte_range.clone()]
pub fn source_text(&self) -> IndexedSlice {
self.source.slice(self.byte_range.clone())
}

/// Returns the path of the [`Source`] that this [`Span`] is associated with, if it has one.
Expand All @@ -99,8 +99,8 @@ impl Span {
pub fn start(&self) -> LineCol {
let mut line = 0;
let mut col = 0;
for c in self.source[0..self.byte_range.start].chars() {
if c == '\n' {
for c in self.source.slice(0..self.byte_range.start).chars() {
if *c == '\n' {
col = 0;
line += 1;
} else {
Expand All @@ -113,8 +113,12 @@ impl Span {
/// Returns the line and column of the end of this [`Span`] within the [`Source`].
pub fn end(&self) -> LineCol {
let LineCol { mut line, mut col } = self.start();
for c in self.source[self.byte_range.start..self.byte_range.end].chars() {
if c == '\n' {
for c in self
.source
.slice(self.byte_range.start..self.byte_range.end)
.chars()
{
if *c == '\n' {
col = 0;
line += 1;
} else {
Expand All @@ -125,17 +129,19 @@ impl Span {
}

/// Returns an iterator over the lines of the [`Source`] that this [`Span`] is associated with,
pub fn source_lines(&self) -> impl Iterator<Item = (&str, Range<usize>)> {
pub fn source_lines(&self) -> impl Iterator<Item = (IndexedString, Range<usize>)> {
let start_line_col = self.start();
let end_line_col = self.end();
let start_col = start_line_col.col;
let start_line = start_line_col.line;
let end_line = end_line_col.line;
let end_col = end_line_col.col;
self.source
.as_str()
.lines()
.enumerate()
.filter_map(move |(i, line)| {
let line = IndexedString::from(line);
if start_line == end_line && end_line == i {
Some((line, start_col..end_col))
} else if i == start_line {
Expand Down

0 comments on commit 6c004ba

Please sign in to comment.