|
| 1 | +use std::string::FromUtf8Error; |
| 2 | + |
| 3 | +#[cfg(feature = "python")] |
| 4 | +use pyo3::{exceptions::PyException, prelude::*}; |
| 5 | + |
| 6 | +use thiserror::Error; |
| 7 | +use topiary_core::{FormatterError, TopiaryQuery}; |
| 8 | + |
| 9 | +#[derive(Error, Debug)] |
| 10 | +pub enum FormatError { |
| 11 | + #[error("parse error")] |
| 12 | + Parse, |
| 13 | + |
| 14 | + #[error("internal query error")] |
| 15 | + Query(String), |
| 16 | + |
| 17 | + #[error("idempotency violated")] |
| 18 | + Idempotency, |
| 19 | + |
| 20 | + #[error("UTF8 conversion error")] |
| 21 | + UTF8(FromUtf8Error), |
| 22 | + |
| 23 | + #[error("unknown error")] |
| 24 | + Unknown, |
| 25 | +} |
| 26 | + |
| 27 | +const QUERY: &str = include_str!("query.scm"); |
| 28 | + |
| 29 | +pub fn format( |
| 30 | + input: &str, |
| 31 | + skip_idempotence: bool, |
| 32 | + tolerate_parsing_errors: bool, |
| 33 | +) -> Result<String, FormatError> { |
| 34 | + let mut output = Vec::new(); |
| 35 | + |
| 36 | + let grammar = topiary_tree_sitter_facade::Language::from(tree_sitter_zeek::LANGUAGE); |
| 37 | + |
| 38 | + let query = TopiaryQuery::new(&grammar, QUERY).map_err(|e| match e { |
| 39 | + FormatterError::Query(m, e) => FormatError::Query(match e { |
| 40 | + None => m, |
| 41 | + Some(e) => format!("{m}: {e}"), |
| 42 | + }), |
| 43 | + _ => FormatError::Unknown, |
| 44 | + })?; |
| 45 | + |
| 46 | + let language = topiary_core::Language { |
| 47 | + name: "zeek".to_string(), |
| 48 | + indent: Some("\t".into()), |
| 49 | + grammar, |
| 50 | + query, |
| 51 | + }; |
| 52 | + |
| 53 | + if let Err(e) = topiary_core::formatter( |
| 54 | + &mut input.as_bytes(), |
| 55 | + &mut output, |
| 56 | + &language, |
| 57 | + topiary_core::Operation::Format { |
| 58 | + skip_idempotence, |
| 59 | + tolerate_parsing_errors, |
| 60 | + }, |
| 61 | + ) { |
| 62 | + Err(match e { |
| 63 | + FormatterError::Query(m, e) => FormatError::Query(match e { |
| 64 | + None => m, |
| 65 | + Some(e) => format!("{m}: {e}"), |
| 66 | + }), |
| 67 | + FormatterError::Idempotence => FormatError::Idempotency, |
| 68 | + FormatterError::Parsing { .. } => FormatError::Parse, |
| 69 | + _ => FormatError::Unknown, |
| 70 | + })?; |
| 71 | + }; |
| 72 | + |
| 73 | + let output = String::from_utf8(output).map_err(FormatError::UTF8)?; |
| 74 | + |
| 75 | + Ok(output) |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(feature = "python")] |
| 79 | +/// Formats the sum of two numbers as string. |
| 80 | +#[pyfunction(name = "format")] |
| 81 | +fn format_wrapped(input: &str) -> PyResult<String> { |
| 82 | + format(input, false, true).map_err(|e| PyException::new_err(e.to_string())) |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(feature = "python")] |
| 86 | +/// A Python module implemented in Rust. |
| 87 | +#[pymodule] |
| 88 | +fn zeekscript(m: &Bound<'_, PyModule>) -> PyResult<()> { |
| 89 | + m.add_function(wrap_pyfunction!(format_wrapped, m)?)?; |
| 90 | + Ok(()) |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod test { |
| 95 | + use insta::assert_debug_snapshot; |
| 96 | + |
| 97 | + use crate::FormatError; |
| 98 | + |
| 99 | + fn format(input: &str) -> Result<String, FormatError> { |
| 100 | + crate::format(input, false, false) |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn comments() { |
| 105 | + assert_debug_snapshot!(format("# foo\n;1;")); |
| 106 | + assert_debug_snapshot!(format("##! foo\n;1;")); |
| 107 | + assert_debug_snapshot!(format("## foo\n1;")); |
| 108 | + assert_debug_snapshot!(format("##< foo\n1;")); |
| 109 | + |
| 110 | + assert_debug_snapshot!(format("1;# foo\n;1;")); |
| 111 | + assert_debug_snapshot!(format("1;##! foo\n;1;")); |
| 112 | + assert_debug_snapshot!(format("1;## foo\n1;")); |
| 113 | + assert_debug_snapshot!(format("1;##< foo")); |
| 114 | + assert_debug_snapshot!(format("1;##< foo\n##< bar")); |
| 115 | + } |
| 116 | +} |
0 commit comments