@@ -137,73 +137,69 @@ impl PyDocument {
137137 }
138138
139139 /// Apply patches to this document and return a new document.
140- /// NOTE: Similar patch-application logic exists in ops::apply_patches (returns String).
141140 fn apply_patches ( & self , patches : Vec < PyPatch > ) -> PyResult < Self > {
142- let mut current_doc = self . inner . clone ( ) ;
143- let mut batch: Vec < usize > = Vec :: new ( ) ;
144-
145- for ( idx, patch) in patches. iter ( ) . enumerate ( ) {
146- let is_complex_replace = matches ! (
147- & patch. operation. inner,
148- yamlpatch:: Op :: Replace ( v) if matches!( v, serde_yaml:: Value :: Mapping ( _) | serde_yaml:: Value :: Sequence ( _) )
149- ) ;
150-
151- if is_complex_replace {
152- // Flush any pending yamlpatch batch first
153- if !batch. is_empty ( ) {
154- let yaml_patches: Vec < yamlpatch:: Patch < ' _ > > = batch
155- . iter ( )
156- . map ( |& i| yamlpatch:: Patch {
157- route : patches[ i] . route . to_yamlpath_route ( ) ,
158- operation : patches[ i] . operation . inner . clone ( ) ,
159- } )
160- . collect ( ) ;
161- current_doc = yamlpatch:: apply_yaml_patches ( & current_doc, & yaml_patches)
162- . map_err ( |e| {
163- PyErr :: new :: < pyo3:: exceptions:: PyRuntimeError , _ > ( format ! (
164- "Patch failed: {e}"
165- ) )
166- } ) ?;
167- batch. clear ( ) ;
168- }
141+ let doc = apply_patches_impl ( & self . inner , & patches) . map_err ( |e| {
142+ PyErr :: new :: < pyo3:: exceptions:: PyRuntimeError , _ > ( format ! ( "Patch failed: {e}" ) )
143+ } ) ?;
144+ Ok ( Self { inner : doc } )
145+ }
146+ }
169147
170- // Apply the complex replace directly
171- let route = patch. route . to_yamlpath_route ( ) ;
172- let value = match & patch. operation . inner {
173- yamlpatch:: Op :: Replace ( v) => v,
174- _ => unreachable ! ( ) ,
175- } ;
176- current_doc =
177- apply_complex_replace ( & current_doc, & route, value) . map_err ( |e| {
178- PyErr :: new :: < pyo3:: exceptions:: PyRuntimeError , _ > ( format ! (
179- "Patch failed: {e}"
180- ) )
181- } ) ?;
182- } else {
183- batch. push ( idx) ;
148+ /// Shared patch-application logic used by both PyDocument::apply_patches and ops::apply_patches.
149+ pub ( crate ) fn apply_patches_impl (
150+ doc : & yamlpath:: Document ,
151+ patches : & [ PyPatch ] ,
152+ ) -> Result < yamlpath:: Document , String > {
153+ let mut current_doc = doc. clone ( ) ;
154+ let mut batch: Vec < usize > = Vec :: new ( ) ;
155+
156+ for ( idx, patch) in patches. iter ( ) . enumerate ( ) {
157+ let is_complex_replace = matches ! (
158+ & patch. operation. inner,
159+ yamlpatch:: Op :: Replace ( v) if matches!( v, serde_yaml:: Value :: Mapping ( _) | serde_yaml:: Value :: Sequence ( _) )
160+ ) ;
161+
162+ if is_complex_replace {
163+ // Flush any pending yamlpatch batch first
164+ if !batch. is_empty ( ) {
165+ let yaml_patches: Vec < yamlpatch:: Patch < ' _ > > = batch
166+ . iter ( )
167+ . map ( |& i| yamlpatch:: Patch {
168+ route : patches[ i] . route . to_yamlpath_route ( ) ,
169+ operation : patches[ i] . operation . inner . clone ( ) ,
170+ } )
171+ . collect ( ) ;
172+ current_doc = yamlpatch:: apply_yaml_patches ( & current_doc, & yaml_patches)
173+ . map_err ( |e| e. to_string ( ) ) ?;
174+ batch. clear ( ) ;
184175 }
185- }
186176
187- // Flush remaining batch
188- if !batch. is_empty ( ) {
189- let yaml_patches: Vec < yamlpatch:: Patch < ' _ > > = batch
190- . iter ( )
191- . map ( |& i| yamlpatch:: Patch {
192- route : patches[ i] . route . to_yamlpath_route ( ) ,
193- operation : patches[ i] . operation . inner . clone ( ) ,
194- } )
195- . collect ( ) ;
196- current_doc = yamlpatch:: apply_yaml_patches ( & current_doc, & yaml_patches) . map_err (
197- |e| {
198- PyErr :: new :: < pyo3:: exceptions:: PyRuntimeError , _ > ( format ! (
199- "Patch failed: {e}"
200- ) )
201- } ,
202- ) ?;
177+ // Apply the complex replace directly
178+ let route = patch. route . to_yamlpath_route ( ) ;
179+ let value = match & patch. operation . inner {
180+ yamlpatch:: Op :: Replace ( v) => v,
181+ _ => unreachable ! ( ) ,
182+ } ;
183+ current_doc = apply_complex_replace ( & current_doc, & route, value) ?;
184+ } else {
185+ batch. push ( idx) ;
203186 }
187+ }
204188
205- Ok ( Self { inner : current_doc } )
189+ // Flush remaining batch
190+ if !batch. is_empty ( ) {
191+ let yaml_patches: Vec < yamlpatch:: Patch < ' _ > > = batch
192+ . iter ( )
193+ . map ( |& i| yamlpatch:: Patch {
194+ route : patches[ i] . route . to_yamlpath_route ( ) ,
195+ operation : patches[ i] . operation . inner . clone ( ) ,
196+ } )
197+ . collect ( ) ;
198+ current_doc = yamlpatch:: apply_yaml_patches ( & current_doc, & yaml_patches)
199+ . map_err ( |e| e. to_string ( ) ) ?;
206200 }
201+
202+ Ok ( current_doc)
207203}
208204
209205fn apply_complex_replace (
0 commit comments