@@ -15,38 +15,53 @@ use std::collections::HashSet;
1515/// from this one that the file does not already contain.
1616pub const MARKER : & str = "__toml_fmt_disabled__" ;
1717
18- /// Whether the entry is one this pass turned back on, which is what a comment carrying [`MARKER`]
19- /// says.
18+ thread_local ! {
19+ /// The marker the pass running on this thread chose. A guard outside a pass has none, so
20+ /// nothing the file wrote is read as one.
21+ static IN_USE : std:: cell:: RefCell <Option <String >> = const { std:: cell:: RefCell :: new( None ) } ;
22+ }
23+
24+ /// Holds the marker for as long as the pass runs, and takes it back however the pass ends.
25+ struct InUse ;
26+
27+ impl InUse {
28+ fn hold ( marker : & str ) -> Self {
29+ IN_USE . with_borrow_mut ( |held| * held = Some ( marker. to_owned ( ) ) ) ;
30+ Self
31+ }
32+ }
33+
34+ impl Drop for InUse {
35+ fn drop ( & mut self ) {
36+ IN_USE . with_borrow_mut ( |held| * held = None ) ;
37+ }
38+ }
39+
40+ /// Whether the entry is one this pass turned back on, which is what the marker beside it says.
2041///
2142/// A pass that would split, drop or merge such an entry has to leave it alone: what says the entry
2243/// is disabled is the comment beside it, and none of those rewrites can say it of the entries they
2344/// leave behind.
2445#[ must_use]
2546pub fn is_enabled_here ( entry : & toml_doc:: Entry < ' _ > ) -> bool {
26- entry
27- . trail
28- . comment
29- . as_ref ( )
30- . is_some_and ( |comment| marks_a_disabled_key ( comment) )
47+ let Some ( comment) = entry. trail . comment . as_ref ( ) else {
48+ return false ;
49+ } ;
50+ IN_USE . with_borrow ( |held| {
51+ held. as_deref ( )
52+ . is_some_and ( |marker| marks_a_disabled_key ( comment, marker) )
53+ } )
3154}
3255
33- /// Whether the comment is one [`enabled_form`] wrote: a comment holding nothing but the marker, or
34- /// the marker written at the end of a comment the file already had. Prose that merely names the
35- /// marker is a comment like any other.
36- fn marks_a_disabled_key ( comment : & str ) -> bool {
56+ /// Whether the comment is the one [`enabled_form`] wrote for this pass: a comment holding nothing
57+ /// but the marker, or the marker written at the end of a comment the file already had.
58+ fn marks_a_disabled_key ( comment : & str , marker : & str ) -> bool {
3759 let body = comment. trim_start_matches ( '#' ) . trim ( ) ;
38- is_a_marker ( body)
60+ body == marker
3961 || body
4062 . rsplit ( char:: is_whitespace)
4163 . next ( )
42- . and_then ( |last| last. strip_suffix ( "-kept" ) )
43- . is_some_and ( is_a_marker)
44- }
45-
46- /// Whether the text is [`MARKER`] or one of the longer ones [`fresh_marker`] builds from it.
47- fn is_a_marker ( text : & str ) -> bool {
48- text. strip_prefix ( MARKER )
49- . is_some_and ( |rest| rest. chars ( ) . all ( |held| held == 'x' ) )
64+ . is_some_and ( |last| last == kept_marker ( marker) )
5065}
5166
5267/// A marker the source does not already hold, so nothing the file says can be read as one.
@@ -66,7 +81,12 @@ fn fresh_marker(source: &str) -> String {
6681/// wrote a comment on a line of its own, so turning one back on leaves a document that still reads.
6782pub fn with_disabled_keys ( content : & str , format : impl FnOnce ( & str ) -> String ) -> String {
6883 let marker = fresh_marker ( content) ;
69- restore_disabled_keys ( & format ( & enable_disabled_keys ( content, & marker) ) , & marker)
84+ let enabled = enable_disabled_keys ( content, & marker) ;
85+ let formatted = {
86+ let _in_use = InUse :: hold ( & marker) ;
87+ format ( & enabled)
88+ } ;
89+ restore_disabled_keys ( & formatted, & marker)
7090}
7191
7292/// [`with_disabled_keys`] for a formatter that may reject its input; a rejected pass restores nothing.
@@ -77,7 +97,11 @@ pub fn with_disabled_keys(content: &str, format: impl FnOnce(&str) -> String) ->
7797pub fn try_with_disabled_keys < E > ( content : & str , format : impl FnOnce ( & str ) -> Result < String , E > ) -> Result < String , E > {
7898 let marker = fresh_marker ( content) ;
7999 let enabled = enable_disabled_keys ( content, & marker) ;
80- Ok ( restore_disabled_keys ( & format ( & enabled) ?, & marker) )
100+ let formatted = {
101+ let _in_use = InUse :: hold ( & marker) ;
102+ format ( & enabled)
103+ } ;
104+ Ok ( restore_disabled_keys ( & formatted?, & marker) )
81105}
82106
83107/// The lines the file wrote a comment on where a key could have stood: what leads an entry or a
0 commit comments