use std::path::{Path, PathBuf};
use crate::scope::VariableScope;
#[derive(Debug, Clone, PartialEq, Eq)]
struct SourceLine {
text: String,
frozen: bool,
}
impl SourceLine {
fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
frozen: false,
}
}
fn frozen(text: impl Into<String>) -> Self {
Self {
text: text.into(),
frozen: true,
}
}
}
#[derive(Debug, Clone)]
pub struct Context {
file_path: PathBuf,
lines: Vec<SourceLine>,
variables: VariableScope,
chain: Vec<PathBuf>,
}
impl Context {
pub fn new(file_path: impl Into<PathBuf>, content: &str) -> Self {
let file_path: PathBuf = file_path.into();
let variables = VariableScope::new(file_name(&file_path));
Self {
file_path,
lines: content.split('\n').map(SourceLine::new).collect(),
variables,
chain: Vec::new(),
}
}
pub fn file_path(&self) -> &Path {
&self.file_path
}
pub fn dirname(&self) -> &Path {
self.file_path.parent().unwrap_or_else(|| Path::new("."))
}
pub fn filename(&self) -> String {
self.file_path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default()
}
pub fn variables(&self) -> &VariableScope {
&self.variables
}
pub fn variables_mut(&mut self) -> &mut VariableScope {
&mut self.variables
}
pub fn len(&self) -> usize {
self.lines.len()
}
pub fn is_empty(&self) -> bool {
self.lines.is_empty()
}
pub fn line(&self, index: usize) -> &str {
&self.lines[index].text
}
pub fn position(&self, index: usize) -> crate::error::Position {
crate::error::Position::new(self.file_path.clone(), index + 1)
}
pub fn is_frozen(&self, index: usize) -> bool {
self.lines[index].frozen
}
pub fn set_line(&mut self, index: usize, text: impl Into<String>) {
self.lines[index].text = text.into();
}
pub fn remove_line(&mut self, index: usize) {
self.lines[index].text.clear();
}
pub fn splice(&mut self, index: usize, lines: impl IntoIterator<Item = String>) {
let inserted: Vec<SourceLine> = lines.into_iter().map(SourceLine::frozen).collect();
self.lines.splice(index..index + 1, inserted);
}
pub fn freeze(&mut self, index: usize) {
self.lines[index].frozen = true;
}
pub fn content(&self) -> String {
self.lines
.iter()
.map(|line| line.text.as_str())
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
.join("\n")
}
pub fn lines(&self) -> impl Iterator<Item = &str> {
self.lines.iter().map(|line| line.text.as_str())
}
pub fn chain(&self) -> &[PathBuf] {
&self.chain
}
pub fn set_chain(&mut self, chain: Vec<PathBuf>) {
self.chain = chain;
}
}
impl std::fmt::Display for Context {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.content())
}
}
fn file_name(path: &Path) -> String {
path.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|| path.to_string_lossy().into_owned())
}
#[cfg(test)]
mod tests {
use super::*;
fn ctx() -> Context {
Context::new("/tmp/example.sql", "first\nsecond\nthird")
}
#[test]
fn joins_non_empty_lines() {
assert_eq!(ctx().content(), "first\nsecond\nthird");
}
#[test]
fn drops_removed_lines() {
let mut ctx = ctx();
ctx.remove_line(1);
assert_eq!(ctx.content(), "first\nthird");
}
#[test]
fn keeps_a_trailing_empty_line_out_of_the_content() {
let ctx = Context::new("/tmp/example.sql", "first\n");
assert_eq!(ctx.len(), 2);
assert_eq!(ctx.content(), "first");
}
#[test]
fn exposes_the_file_paths() {
let ctx = ctx();
assert_eq!(ctx.file_path(), Path::new("/tmp/example.sql"));
assert_eq!(ctx.filename(), "example.sql");
assert_eq!(ctx.dirname(), Path::new("/tmp"));
}
#[test]
fn falls_back_to_the_current_directory() {
let ctx = Context::new("example.sql", "");
assert_eq!(ctx.dirname(), Path::new(""));
assert_eq!(ctx.filename(), "example.sql");
}
#[test]
fn replaces_a_line() {
let mut ctx = ctx();
ctx.set_line(1, "changed");
assert_eq!(ctx.line(1), "changed");
}
#[test]
fn splices_preprocessed_lines() {
let mut ctx = ctx();
ctx.splice(1, ["a".to_string(), "b".to_string()]);
assert_eq!(
ctx.lines().collect::<Vec<_>>(),
["first", "a", "b", "third"]
);
}
#[test]
fn spliced_lines_are_frozen() {
let mut ctx = ctx();
ctx.splice(0, ["a".to_string()]);
assert!(ctx.is_frozen(0));
assert!(!ctx.is_frozen(1));
}
#[test]
fn builds_a_one_based_position() {
assert_eq!(ctx().position(2).line, 3);
}
#[test]
fn tracks_the_chain_of_files() {
let mut ctx = ctx();
assert!(ctx.chain().is_empty());
ctx.set_chain(vec![
PathBuf::from("/tmp/a.sql"),
PathBuf::from("/tmp/main.sql"),
]);
assert_eq!(ctx.chain().len(), 2);
}
#[test]
fn displays_the_content() {
assert_eq!(ctx().to_string(), ctx().content());
}
#[test]
fn has_variables() {
let mut ctx = ctx();
ctx.variables_mut().set("key", "value");
assert_eq!(ctx.variables().get("key").as_deref(), Some("value"));
}
}