orion_lib 7.4.0

A library for parsing and executing the Orion Programming Language
Documentation
use std::fmt::{Debug, Display, Formatter};

pub(crate) fn location<S: AsRef<str>>(location: usize, contents: S) -> Position {
    let mut line = 1;
    let mut loc = 1;

    for (count, tok) in contents.as_ref().chars().enumerate() {
        if count == location {
            break;
        }

        if tok == '\n' {
            line += 1;
            loc = 1;
            continue;
        }

        loc += 1;
    }

    Position {
        line,
        index: loc,
    }
}

pub(crate) struct Position {
    line: usize,
    index: usize,
}

impl Debug for Position {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "line: {}, index: {}", self.line, self.index
        )
    }
}

impl Display for Position {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "line: {}, index: {}", self.line, self.index
        )
    }
}