@@ -184,6 +184,54 @@ pub enum ParseFlakeError {
184
184
#[ error( "Unrecognized node or token encountered" ) ]
185
185
Unrecognized ,
186
186
}
187
+
188
+ fn parse_fragment ( fragment : & str ) -> Result < ( Option < String > , Option < String > ) , ParseFlakeError > {
189
+ let mut node: Option < String > = None ;
190
+ let mut profile: Option < String > = None ;
191
+
192
+ let ast = rnix:: parse ( fragment) ;
193
+
194
+ let first_child = match ast. root ( ) . node ( ) . first_child ( ) {
195
+ Some ( x) => x,
196
+ None => return Ok ( ( None , None ) )
197
+ } ;
198
+
199
+ let mut node_over = false ;
200
+
201
+ for entry in first_child. children_with_tokens ( ) {
202
+ let x: Option < String > = match ( entry. kind ( ) , node_over) {
203
+ ( TOKEN_DOT , false ) => {
204
+ node_over = true ;
205
+ None
206
+ }
207
+ ( TOKEN_DOT , true ) => {
208
+ return Err ( ParseFlakeError :: PathTooLong ) ;
209
+ }
210
+ ( NODE_IDENT , _) => Some ( entry. into_node ( ) . unwrap ( ) . text ( ) . to_string ( ) ) ,
211
+ ( TOKEN_IDENT , _) => Some ( entry. into_token ( ) . unwrap ( ) . text ( ) . to_string ( ) ) ,
212
+ ( NODE_STRING , _) => {
213
+ let c = entry
214
+ . into_node ( )
215
+ . unwrap ( )
216
+ . children_with_tokens ( )
217
+ . nth ( 1 )
218
+ . unwrap ( ) ;
219
+
220
+ Some ( c. into_token ( ) . unwrap ( ) . text ( ) . to_string ( ) )
221
+ }
222
+ _ => return Err ( ParseFlakeError :: Unrecognized ) ,
223
+ } ;
224
+
225
+ if !node_over {
226
+ node = x;
227
+ } else {
228
+ profile = x;
229
+ }
230
+ }
231
+
232
+ Ok ( ( node, profile) )
233
+ }
234
+
187
235
pub fn parse_flake ( flake : & str ) -> Result < DeployFlake , ParseFlakeError > {
188
236
let flake_fragment_start = flake. find ( '#' ) ;
189
237
let ( repo, maybe_fragment) = match flake_fragment_start {
@@ -195,51 +243,7 @@ pub fn parse_flake(flake: &str) -> Result<DeployFlake, ParseFlakeError> {
195
243
let mut profile: Option < String > = None ;
196
244
197
245
if let Some ( fragment) = maybe_fragment {
198
- let ast = rnix:: parse ( fragment) ;
199
-
200
- let first_child = match ast. root ( ) . node ( ) . first_child ( ) {
201
- Some ( x) => x,
202
- None => {
203
- return Ok ( DeployFlake {
204
- repo,
205
- node : None ,
206
- profile : None ,
207
- } )
208
- }
209
- } ;
210
-
211
- let mut node_over = false ;
212
-
213
- for entry in first_child. children_with_tokens ( ) {
214
- let x: Option < String > = match ( entry. kind ( ) , node_over) {
215
- ( TOKEN_DOT , false ) => {
216
- node_over = true ;
217
- None
218
- }
219
- ( TOKEN_DOT , true ) => {
220
- return Err ( ParseFlakeError :: PathTooLong ) ;
221
- }
222
- ( NODE_IDENT , _) => Some ( entry. into_node ( ) . unwrap ( ) . text ( ) . to_string ( ) ) ,
223
- ( TOKEN_IDENT , _) => Some ( entry. into_token ( ) . unwrap ( ) . text ( ) . to_string ( ) ) ,
224
- ( NODE_STRING , _) => {
225
- let c = entry
226
- . into_node ( )
227
- . unwrap ( )
228
- . children_with_tokens ( )
229
- . nth ( 1 )
230
- . unwrap ( ) ;
231
-
232
- Some ( c. into_token ( ) . unwrap ( ) . text ( ) . to_string ( ) )
233
- }
234
- _ => return Err ( ParseFlakeError :: Unrecognized ) ,
235
- } ;
236
-
237
- if !node_over {
238
- node = x;
239
- } else {
240
- profile = x;
241
- }
242
- }
246
+ ( node, profile) = parse_fragment ( fragment) ?;
243
247
}
244
248
245
249
Ok ( DeployFlake {
@@ -316,59 +320,10 @@ fn test_parse_flake() {
316
320
}
317
321
318
322
pub fn parse_file < ' a > ( file : & ' a str , attribute : & ' a str ) -> Result < DeployFlake < ' a > , ParseFlakeError > {
319
- let repo = file; //format!("./{file}");
320
-
321
- let mut node: Option < String > = None ;
322
- let mut profile: Option < String > = None ;
323
-
324
- let ast = rnix:: parse ( attribute) ;
325
-
326
- let first_child = match ast. root ( ) . node ( ) . first_child ( ) {
327
- Some ( x) => x,
328
- None => {
329
- return Ok ( DeployFlake {
330
- repo : & repo,
331
- node : None ,
332
- profile : None ,
333
- } )
334
- }
335
- } ;
336
-
337
- let mut node_over = false ;
338
-
339
- for entry in first_child. children_with_tokens ( ) {
340
- let x: Option < String > = match ( entry. kind ( ) , node_over) {
341
- ( TOKEN_DOT , false ) => {
342
- node_over = true ;
343
- None
344
- }
345
- ( TOKEN_DOT , true ) => {
346
- return Err ( ParseFlakeError :: PathTooLong ) ;
347
- }
348
- ( NODE_IDENT , _) => Some ( entry. into_node ( ) . unwrap ( ) . text ( ) . to_string ( ) ) ,
349
- ( TOKEN_IDENT , _) => Some ( entry. into_token ( ) . unwrap ( ) . text ( ) . to_string ( ) ) ,
350
- ( NODE_STRING , _) => {
351
- let c = entry
352
- . into_node ( )
353
- . unwrap ( )
354
- . children_with_tokens ( )
355
- . nth ( 1 )
356
- . unwrap ( ) ;
357
-
358
- Some ( c. into_token ( ) . unwrap ( ) . text ( ) . to_string ( ) )
359
- }
360
- _ => return Err ( ParseFlakeError :: Unrecognized ) ,
361
- } ;
362
-
363
- if !node_over {
364
- node = x;
365
- } else {
366
- profile = x;
367
- }
368
- }
323
+ let ( node, profile) = parse_fragment ( attribute) ?;
369
324
370
325
Ok ( DeployFlake {
371
- repo : & repo ,
326
+ repo : & file ,
372
327
node,
373
328
profile,
374
329
} )
0 commit comments