11use std:: string:: String ;
22
33use pyo3:: prelude:: PyModule ;
4- use pyo3:: { pyfunction, pymodule, wrap_pyfunction, Bound , PyResult } ;
4+ use pyo3:: { pyclass , pyfunction, pymethods , pymodule, wrap_pyfunction, Bound , PyResult } ;
55use taplo:: formatter:: { format_syntax, Options } ;
66use taplo:: parser:: parse;
77
@@ -16,46 +16,69 @@ mod project;
1616mod global;
1717mod helpers;
1818
19- /// Format toml file
20- #[ pyfunction]
21- #[ must_use]
22- pub fn format_toml (
23- content : & str ,
19+ #[ pyclass( frozen, get_all) ]
20+ pub struct Settings {
2421 column_width : usize ,
2522 indent : usize ,
2623 keep_full_version : bool ,
2724 max_supported_python : ( u8 , u8 ) ,
2825 min_supported_python : ( u8 , u8 ) ,
29- ) -> String {
26+ }
27+
28+ #[ pymethods]
29+ impl Settings {
30+ #[ new]
31+ #[ pyo3( signature = ( * , column_width, indent, keep_full_version, max_supported_python, min_supported_python ) ) ]
32+ const fn new (
33+ column_width : usize ,
34+ indent : usize ,
35+ keep_full_version : bool ,
36+ max_supported_python : ( u8 , u8 ) ,
37+ min_supported_python : ( u8 , u8 ) ,
38+ ) -> Self {
39+ Self {
40+ column_width,
41+ indent,
42+ keep_full_version,
43+ max_supported_python,
44+ min_supported_python,
45+ }
46+ }
47+ }
48+
49+ /// Format toml file
50+ #[ must_use]
51+ #[ pyfunction]
52+ pub fn format_toml ( content : & str , opt : & Settings ) -> String {
3053 let root_ast = parse ( content) . into_syntax ( ) . clone_for_update ( ) ;
3154 let mut tables = Tables :: from_ast ( & root_ast) ;
3255
33- fix_build ( & mut tables, keep_full_version) ;
56+ fix_build ( & mut tables, opt . keep_full_version ) ;
3457 fix_project_table (
3558 & mut tables,
36- keep_full_version,
37- max_supported_python,
38- min_supported_python,
59+ opt . keep_full_version ,
60+ opt . max_supported_python ,
61+ opt . min_supported_python ,
3962 ) ;
4063 reorder_tables ( & root_ast, & mut tables) ;
4164
4265 let options = Options {
43- align_entries : false , // do not align by =
44- align_comments : true , // align inline comments
45- align_single_comments : true , // align comments after entries
46- array_trailing_comma : true , // ensure arrays finish with trailing comma
47- array_auto_expand : true , // arrays go to multi line for easier diffs
48- array_auto_collapse : false , // do not collapse for easier diffs
49- compact_arrays : false , // do not compact for easier diffs
50- compact_inline_tables : false , // do not compact for easier diffs
51- compact_entries : false , // do not compact for easier diffs
52- column_width, // always expand arrays per https://github.com/tamasfe/taplo/issues/390
66+ align_entries : false , // do not align by =
67+ align_comments : true , // align inline comments
68+ align_single_comments : true , // align comments after entries
69+ array_trailing_comma : true , // ensure arrays finish with trailing comma
70+ array_auto_expand : true , // arrays go to multi line for easier diffs
71+ array_auto_collapse : false , // do not collapse for easier diffs
72+ compact_arrays : false , // do not compact for easier diffs
73+ compact_inline_tables : false , // do not compact for easier diffs
74+ compact_entries : false , // do not compact for easier diffs
75+ column_width : opt . column_width , // always expand arrays per https://github.com/tamasfe/taplo/issues/390
5376 indent_tables : false ,
5477 indent_entries : false ,
5578 inline_table_expand : true ,
5679 trailing_newline : true ,
5780 allowed_blank_lines : 1 , // one blank line to separate
58- indent_string : " " . repeat ( indent) ,
81+ indent_string : " " . repeat ( opt . indent ) ,
5982 reorder_keys : false , // respect custom order
6083 reorder_arrays : false , // for natural sorting we need to this ourselves
6184 crlf : false ,
@@ -71,6 +94,7 @@ pub fn format_toml(
7194#[ cfg( not( tarpaulin_include) ) ]
7295pub fn _lib ( m : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
7396 m. add_function ( wrap_pyfunction ! ( format_toml, m) ?) ?;
97+ m. add_class :: < Settings > ( ) ?;
7498 Ok ( ( ) )
7599}
76100
@@ -79,7 +103,7 @@ mod tests {
79103 use indoc:: indoc;
80104 use rstest:: rstest;
81105
82- use crate :: format_toml;
106+ use crate :: { format_toml, Settings } ;
83107
84108 #[ rstest]
85109 #[ case:: simple(
@@ -160,7 +184,16 @@ mod tests {
160184 #[ case] keep_full_version : bool ,
161185 #[ case] max_supported_python : ( u8 , u8 ) ,
162186 ) {
163- let got = format_toml ( start, 1 , indent, keep_full_version, max_supported_python, ( 3 , 8 ) ) ;
187+ let got = format_toml (
188+ start,
189+ & Settings {
190+ column_width : 1 ,
191+ indent,
192+ keep_full_version,
193+ max_supported_python,
194+ min_supported_python : ( 3 , 8 ) ,
195+ } ,
196+ ) ;
164197 assert_eq ! ( got, expected) ;
165198 }
166199}
0 commit comments