plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


plum-core/src/formatter.rs
// plum-core/src/formatter.rs
use std::io::Cursor;

use topiary_core::{formatter, Language, Operation, TopiaryQuery};

const FORMAT_QUERY: &str =
    include_str!("../../tooling/tree-sitter-plum/queries/plum/format.scm");

#[derive(Debug)]
pub enum FormatterError {
    TopiaryCoreError(topiary_core::FormatterError),
}

impl std::fmt::Display for FormatterError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FormatterError::TopiaryCoreError(e) => write!(f, "Topiary error: {e}"),
        }
    }
}

impl std::error::Error for FormatterError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            FormatterError::TopiaryCoreError(e) => Some(e),
        }
    }
}

impl From<topiary_core::FormatterError> for FormatterError {
    fn from(e: topiary_core::FormatterError) -> Self {
        FormatterError::TopiaryCoreError(e)
    }
}

pub fn formatSource(source: &str) -> Result<String, FormatterError> {
    formatSourceWithOpts(source, false)
}

pub fn formatSourceWithOpts(source: &str, skip_idempotence: bool) -> Result<String, FormatterError> {
    let ts_language: tree_sitter::Language = tree_sitter_plum::LANGUAGE.into();
    let facade_grammar: topiary_tree_sitter_facade::Language = ts_language.into();
    let query = TopiaryQuery::new(&facade_grammar, FORMAT_QUERY)?;
    let language = Language {
        name: "plum".to_owned(),
        query,
        grammar: facade_grammar,
        indent: None,
    };
    let mut input = Cursor::new(source.as_bytes());
    let mut output = Vec::new();
    formatter(
        &mut input,
        &mut output,
        &language,
        Operation::Format {
            skip_idempotence,
            tolerate_parsing_errors: false,
        },
    )?;
    Ok(String::from_utf8(output).expect("Topiary output is valid UTF-8"))
}