init: initial project setup
This commit is contained in:
commit
5d479bcaf1
12
.envrc
Normal file
12
.envrc
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
if has lorri; then
|
||||||
|
echo Using lorri
|
||||||
|
eval "$(lorri direnv)"
|
||||||
|
elif has nix; then
|
||||||
|
echo Using flake fallback
|
||||||
|
use flake
|
||||||
|
else
|
||||||
|
# noop
|
||||||
|
echo unsupported direnv configuration
|
||||||
|
fi
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "slice-cursor"
|
||||||
|
version = "0.1.0"
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "slice-cursor"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
44
flake.nix
Normal file
44
flake.nix
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
{
|
||||||
|
inputs = {
|
||||||
|
rust-overlay = {
|
||||||
|
url = "github:oxalica/rust-overlay";
|
||||||
|
inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
};
|
||||||
|
naersk.url = "github:nix-community/naersk";
|
||||||
|
flake-utils = {url = "github:numtide/flake-utils"; };
|
||||||
|
flake-compat = { url = "github:edolstra/flake-compat"; flake = false; };
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = {self, nixpkgs, flake-utils, rust-overlay, naersk, flake-compat}:
|
||||||
|
flake-utils.lib.eachDefaultSystem (system: let
|
||||||
|
overlays = [
|
||||||
|
rust-overlay.overlays.default
|
||||||
|
];
|
||||||
|
pkgs = import nixpkgs { inherit system overlays; };
|
||||||
|
rust-bin = pkgs.rust-bin.stable.latest.default;
|
||||||
|
naersk-lib = pkgs.callPackage naersk {
|
||||||
|
rustc = rust-bin;
|
||||||
|
cargo = rust-bin;
|
||||||
|
};
|
||||||
|
project = naersk-lib.buildPackage {
|
||||||
|
src = ./.;
|
||||||
|
root = ./.;
|
||||||
|
nativeBuildInputs = [pkgs.pkg-config];
|
||||||
|
buildInputs =
|
||||||
|
(
|
||||||
|
if pkgs.stdenv.isDarwin
|
||||||
|
then with pkgs.darwin.apple_sdk.frameworks; [Security SystemConfiguration]
|
||||||
|
else [pkgs.openssl]
|
||||||
|
)
|
||||||
|
++ [rust-bin];
|
||||||
|
};
|
||||||
|
in
|
||||||
|
{
|
||||||
|
packages.default = project;
|
||||||
|
devShells.default = pkgs.mkShell {
|
||||||
|
buildInputs = [ rust-bin pkgs.rust-analyzer ];
|
||||||
|
packages = with pkgs; [ gnumake ];
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
295
src/lib.rs
Normal file
295
src/lib.rs
Normal file
@ -0,0 +1,295 @@
|
|||||||
|
// Copyright 2024 Jeremy Wall <jeremy@marzhillstudios.com>
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
//! A set of zero copy utility classes for managing cursor like access through slices.
|
||||||
|
use std::fmt::Debug;
|
||||||
|
use std::iter::Iterator;
|
||||||
|
use std::ops::Index;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test;
|
||||||
|
|
||||||
|
/// A trait for types that can have an offset as a count of processed items.
|
||||||
|
pub trait Offsetable {
|
||||||
|
fn get_offset(&self) -> usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Offsetable for usize {
|
||||||
|
fn get_offset(&self) -> usize {
|
||||||
|
return *self;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A trait for types that can seek to an index.
|
||||||
|
pub trait Seekable {
|
||||||
|
fn seek(&mut self, u: usize) -> usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait for Inputs that can report current lines and columns in a text input.
|
||||||
|
pub trait Positioned: Offsetable {
|
||||||
|
fn line(&self) -> usize;
|
||||||
|
fn column(&self) -> usize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// SpanRange encompasses the valid Ops::Range types for use with the Span trait.
|
||||||
|
pub enum SpanRange {
|
||||||
|
Range(std::ops::Range<usize>),
|
||||||
|
RangeTo(std::ops::RangeTo<usize>),
|
||||||
|
RangeFrom(std::ops::RangeFrom<usize>),
|
||||||
|
RangeFull(std::ops::RangeFull),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An input that can provide a span of a range of the input.
|
||||||
|
pub trait Span<O> {
|
||||||
|
fn span(&self, idx: SpanRange) -> O;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The interface for types that can peek ahead to the next item.
|
||||||
|
pub trait Peekable<O> {
|
||||||
|
fn peek_next(&self) -> Option<O>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A Cloneable Iterator that can report an offset as a count of processed Items.
|
||||||
|
pub trait Cursor: Iterator + Clone + Offsetable {
|
||||||
|
fn curr(&self) -> Self::Item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements `InputIter` for any slice of T.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SliceCursor<'a, T: Debug + 'a> {
|
||||||
|
source: &'a [T],
|
||||||
|
offset: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug + 'a> SliceCursor<'a, T> {
|
||||||
|
/// new constructs a SliceIter from a Slice of T.
|
||||||
|
pub fn new(source: &'a [T]) -> Self {
|
||||||
|
SliceCursor {
|
||||||
|
source,
|
||||||
|
offset: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug + 'a> Iterator for SliceCursor<'a, T> {
|
||||||
|
type Item = &'a T;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
match self.source.get(self.offset) {
|
||||||
|
Some(item) => {
|
||||||
|
self.offset += 1;
|
||||||
|
Some(item)
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug + 'a> Offsetable for SliceCursor<'a, T> {
|
||||||
|
fn get_offset(&self) -> usize {
|
||||||
|
self.offset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, It> Positioned for SliceCursor<'a, It>
|
||||||
|
where
|
||||||
|
It: Positioned + Debug,
|
||||||
|
{
|
||||||
|
fn line(&self) -> usize {
|
||||||
|
match self.peek_next() {
|
||||||
|
Some(i) => i.line(),
|
||||||
|
None => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn column(&self) -> usize {
|
||||||
|
match self.peek_next() {
|
||||||
|
Some(i) => i.column(),
|
||||||
|
None => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug + 'a> Clone for SliceCursor<'a, T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
SliceCursor {
|
||||||
|
source: self.source,
|
||||||
|
offset: self.offset,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug + 'a> Cursor for SliceCursor<'a, T> {
|
||||||
|
fn curr(&self) -> Self::Item {
|
||||||
|
if self.offset >= self.source.len() {
|
||||||
|
self.source.get(self.source.len() - 1).unwrap()
|
||||||
|
} else {
|
||||||
|
if self.offset == 0 {
|
||||||
|
self.source.get(self.offset).unwrap()
|
||||||
|
} else {
|
||||||
|
self.source.get(self.offset - 1).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug + 'a> Span<&'a [T]> for SliceCursor<'a, T> {
|
||||||
|
fn span(&self, idx: SpanRange) -> &'a [T] {
|
||||||
|
match idx {
|
||||||
|
SpanRange::Range(r) => self.source.index(r),
|
||||||
|
SpanRange::RangeTo(r) => self.source.index(r),
|
||||||
|
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||||
|
SpanRange::RangeFull(r) => self.source.index(r),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for SliceCursor<'a, u8> {
|
||||||
|
fn from(source: &'a str) -> Self {
|
||||||
|
SliceCursor::new(source.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug> From<&'a [T]> for SliceCursor<'a, T> {
|
||||||
|
fn from(source: &'a [T]) -> Self {
|
||||||
|
SliceCursor::new(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: Debug> From<&'a Vec<T>> for SliceCursor<'a, T> {
|
||||||
|
fn from(source: &'a Vec<T>) -> Self {
|
||||||
|
SliceCursor::new(source.as_slice())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, O: Debug> Peekable<&'a O> for SliceCursor<'a, O> {
|
||||||
|
fn peek_next(&self) -> Option<&'a O> {
|
||||||
|
self.source.get(self.offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Implements `InputIter` for any slice of T.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct StrCursor<'a> {
|
||||||
|
source: &'a str,
|
||||||
|
offset: usize,
|
||||||
|
line: usize,
|
||||||
|
column: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> StrCursor<'a> {
|
||||||
|
/// new constructs a StrIter from a Slice of T.
|
||||||
|
pub fn new(source: &'a str) -> Self {
|
||||||
|
StrCursor {
|
||||||
|
source,
|
||||||
|
offset: 0,
|
||||||
|
line: 1,
|
||||||
|
column: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for StrCursor<'a> {
|
||||||
|
type Item = &'a u8;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
match self.source.as_bytes().get(self.offset) {
|
||||||
|
// TODO count lines and columns.
|
||||||
|
Some(item) => {
|
||||||
|
self.offset += 1;
|
||||||
|
if *item == b'\n' {
|
||||||
|
self.line += 1;
|
||||||
|
self.column = 1;
|
||||||
|
} else {
|
||||||
|
self.column += 1;
|
||||||
|
}
|
||||||
|
Some(item)
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Offsetable for StrCursor<'a> {
|
||||||
|
fn get_offset(&self) -> usize {
|
||||||
|
self.offset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Positioned for StrCursor<'a> {
|
||||||
|
fn line(&self) -> usize {
|
||||||
|
self.line
|
||||||
|
}
|
||||||
|
|
||||||
|
fn column(&self) -> usize {
|
||||||
|
self.column
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Clone for StrCursor<'a> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
StrCursor {
|
||||||
|
source: self.source,
|
||||||
|
offset: self.offset,
|
||||||
|
line: self.line,
|
||||||
|
column: self.column,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Cursor for StrCursor<'a> {
|
||||||
|
fn curr(&self) -> Self::Item {
|
||||||
|
if self.offset >= self.source.len() {
|
||||||
|
self.source.as_bytes().get(self.source.len() - 1).unwrap()
|
||||||
|
} else {
|
||||||
|
if self.offset == 0 {
|
||||||
|
self.source.as_bytes().get(self.offset).unwrap()
|
||||||
|
} else {
|
||||||
|
self.source.as_bytes().get(self.offset - 1).unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for StrCursor<'a> {
|
||||||
|
fn from(source: &'a str) -> Self {
|
||||||
|
Self::new(source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Span<&'a str> for StrCursor<'a> {
|
||||||
|
fn span(&self, idx: SpanRange) -> &'a str {
|
||||||
|
match idx {
|
||||||
|
SpanRange::Range(r) => self.source.index(r),
|
||||||
|
SpanRange::RangeTo(r) => self.source.index(r),
|
||||||
|
SpanRange::RangeFrom(r) => self.source.index(r),
|
||||||
|
SpanRange::RangeFull(r) => self.source.index(r),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Seekable for StrCursor<'a> {
|
||||||
|
fn seek(&mut self, to: usize) -> usize {
|
||||||
|
let self_len = self.source.len();
|
||||||
|
let offset = if self_len > to { to } else { self_len };
|
||||||
|
self.offset = offset;
|
||||||
|
self.offset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Peekable<&'a u8> for StrCursor<'a> {
|
||||||
|
fn peek_next(&self) -> Option<&'a u8> {
|
||||||
|
self.source.as_bytes().get(self.offset)
|
||||||
|
}
|
||||||
|
}
|
82
src/test.rs
Normal file
82
src/test.rs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
SliceCursor, StrCursor,
|
||||||
|
Cursor, Offsetable,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_slice_iter() {
|
||||||
|
let input_str = "foo";
|
||||||
|
let mut iter = SliceCursor::new(input_str.as_bytes());
|
||||||
|
let cloned = iter.clone();
|
||||||
|
assert_eq!(0, iter.get_offset());
|
||||||
|
let mut out = Vec::new();
|
||||||
|
loop {
|
||||||
|
let b = match iter.next() {
|
||||||
|
None => break,
|
||||||
|
Some(b) => b,
|
||||||
|
};
|
||||||
|
assert_eq!(*b as char, *iter.curr() as char);
|
||||||
|
out.push(b.clone());
|
||||||
|
}
|
||||||
|
assert_eq!(*iter.curr(), 'o' as u8);
|
||||||
|
assert_eq!(3, out.len());
|
||||||
|
assert_eq!('f' as u8, out[0]);
|
||||||
|
assert_eq!('o' as u8, out[1]);
|
||||||
|
assert_eq!('o' as u8, out[2]);
|
||||||
|
assert_eq!(3, iter.get_offset());
|
||||||
|
|
||||||
|
out = Vec::new();
|
||||||
|
for b in cloned {
|
||||||
|
out.push(b.clone());
|
||||||
|
}
|
||||||
|
assert_eq!(3, out.len());
|
||||||
|
assert_eq!('f' as u8, out[0]);
|
||||||
|
assert_eq!('o' as u8, out[1]);
|
||||||
|
assert_eq!('o' as u8, out[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_str_iter() {
|
||||||
|
let input_str = "foo";
|
||||||
|
let mut iter = StrCursor::new(input_str);
|
||||||
|
let cloned = iter.clone();
|
||||||
|
assert_eq!(0, iter.get_offset());
|
||||||
|
let mut out = Vec::new();
|
||||||
|
loop {
|
||||||
|
let b = match iter.next() {
|
||||||
|
None => break,
|
||||||
|
Some(b) => b,
|
||||||
|
};
|
||||||
|
assert_eq!(*b as char, *iter.curr() as char);
|
||||||
|
out.push(b.clone());
|
||||||
|
}
|
||||||
|
assert_eq!(*iter.curr(), 'o' as u8);
|
||||||
|
assert_eq!(3, out.len());
|
||||||
|
assert_eq!('f' as u8, out[0]);
|
||||||
|
assert_eq!('o' as u8, out[1]);
|
||||||
|
assert_eq!('o' as u8, out[2]);
|
||||||
|
assert_eq!(3, iter.get_offset());
|
||||||
|
|
||||||
|
out = Vec::new();
|
||||||
|
for b in cloned {
|
||||||
|
out.push(b.clone());
|
||||||
|
}
|
||||||
|
assert_eq!(3, out.len());
|
||||||
|
assert_eq!('f' as u8, out[0]);
|
||||||
|
assert_eq!('o' as u8, out[1]);
|
||||||
|
assert_eq!('o' as u8, out[2]);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user