libdiffsitter/
lib.rs

1//! The supporting library for `diffsitter`.
2//!
3//! This library is not particularly cohesive or well organized. It exists to support the
4//! diffsitter binary and I am open to refactoring it and organizing it better if there's demand.
5//!
6//! In the meantime, buyer beware.
7//!
8//! All of the methods used to create diffsitter are here and we have attempted to keep the library
9//! at least somewhat sane and organized for our own usage.
10
11pub mod cli;
12pub mod config;
13pub mod console_utils;
14pub mod diff;
15pub mod input_processing;
16pub mod neg_idx_vec;
17pub mod parse;
18pub mod render;
19
20use anyhow::Result;
21use input_processing::VectorData;
22use log::{debug, info};
23use parse::GrammarConfig;
24use std::{fs, path::PathBuf};
25
26/// Create an AST vector from a path
27///
28/// This returns an `AstVector` and a pinned struct with the owned data, which the `AstVector`
29/// references.
30///
31/// `data` is used as an out-parameter. We need some external struct we can reference because the
32/// return type references the data in that struct.
33///
34/// This returns an anyhow [Result], which is bad practice for a library and will need to be
35/// refactored in the future. This method was originally used in the `diffsitter` binary so we
36/// didn't feel the need to specify a specific error type.
37pub fn generate_ast_vector_data(
38    path: PathBuf,
39    file_type: Option<&str>,
40    grammar_config: &GrammarConfig,
41) -> Result<VectorData> {
42    let text = fs::read_to_string(&path)?;
43    let file_name = path.to_string_lossy();
44    debug!("Reading {} to string", file_name);
45
46    if let Some(file_type) = file_type {
47        info!(
48            "Using user-set filetype \"{}\" for {}",
49            file_type, file_name
50        );
51    } else {
52        info!("Will deduce filetype from file extension");
53    };
54    let tree = parse::parse_file(&path, file_type, grammar_config)?;
55    Ok(VectorData { text, tree, path })
56}