use crate::formatter::{format, FormatConfig, IndentStyle};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
#[pyfunction]
#[pyo3(signature = (source, indent_width=4, use_tabs=false, preserve_comments=true, sort_imports=true))]
pub fn format_source(
source: &str,
indent_width: usize,
use_tabs: bool,
preserve_comments: bool,
sort_imports: bool,
) -> PyResult<String> {
let config = FormatConfig {
indent_width,
indent_style: if use_tabs {
IndentStyle::Tabs
} else {
IndentStyle::Spaces
},
preserve_comments,
sort_imports,
..Default::default()
};
format(source, config).map_err(|e| PyValueError::new_err(e.to_string()))
}
#[pyfunction]
#[pyo3(signature = (source, indent_width=4, use_tabs=false))]
pub fn check_format(source: &str, indent_width: usize, use_tabs: bool) -> PyResult<bool> {
let config = FormatConfig {
indent_width,
indent_style: if use_tabs {
IndentStyle::Tabs
} else {
IndentStyle::Spaces
},
..Default::default()
};
let formatted = format(source, config).map_err(|e| PyValueError::new_err(e.to_string()))?;
Ok(source == formatted)
}