Skip to content

Commit a6c471c

Browse files
committed
- fix code duplication
1 parent 36a6aac commit a6c471c

1 file changed

Lines changed: 43 additions & 144 deletions

File tree

src/Parsley/Parser.cs

Lines changed: 43 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private string[] ReadToLines(string path)
139139
}
140140

141141
foreach (var propInfo in propInfos)
142+
{
142143
try
143144
{
144145
var attribute = (ColumnAttribute)propInfo.GetCustomAttributes(typeof(ColumnAttribute), true).First();
@@ -191,6 +192,7 @@ private string[] ReadToLines(string path)
191192
obj.SetError(string.Format(Resources.LineExceptionFormat, propInfo.Name, e.Message));
192193
}
193194
}
195+
}
194196

195197
return obj;
196198
}
@@ -215,6 +217,31 @@ private string[] GetDelimiterSeparatedValues(string line)
215217
return values;
216218
}
217219

220+
private static Result<T[]> TryOperation<T>(Func<T[]> operation) where T : IFileLine, new()
221+
{
222+
try
223+
{
224+
return Result<T[]>.Success(operation());
225+
}
226+
catch (Exception ex)
227+
{
228+
return Result<T[]>.Failure(ex.Message);
229+
}
230+
}
231+
232+
private static async Task<Result<T[]>> TryOperationAsync<T>(Func<Task<T[]>> operation) where T : IFileLine, new()
233+
{
234+
try
235+
{
236+
var result = await operation();
237+
return Result<T[]>.Success(result);
238+
}
239+
catch (Exception ex)
240+
{
241+
return Result<T[]>.Failure(ex.Message);
242+
}
243+
}
244+
218245
public T[] Parse<T>(Stream stream, Encoding encoding = null) where T : IFileLine, new()
219246
{
220247
return Parse<T>(stream, encoding, Options);
@@ -391,210 +418,82 @@ private string[] GetDelimiterSeparatedValues(string line)
391418

392419
public Result<T[]> TryParse<T>(string filepath) where T : IFileLine, new()
393420
{
394-
try
395-
{
396-
var result = Parse<T>(filepath, Options);
397-
return Result<T[]>.Success(result);
398-
}
399-
catch (Exception ex)
400-
{
401-
return Result<T[]>.Failure(ex.Message);
402-
}
421+
return TryOperation(() => Parse<T>(filepath, Options));
403422
}
404423

405424
public Result<T[]> TryParse<T>(string filepath, ParseOptions options) where T : IFileLine, new()
406425
{
407-
try
408-
{
409-
var result = Parse<T>(filepath, options);
410-
return Result<T[]>.Success(result);
411-
}
412-
catch (Exception ex)
413-
{
414-
return Result<T[]>.Failure(ex.Message);
415-
}
426+
return TryOperation(() => Parse<T>(filepath, options));
416427
}
417428

418429
public Result<T[]> TryParse<T>(string[] lines) where T : IFileLine, new()
419430
{
420-
try
421-
{
422-
var result = Parse<T>(lines, Options);
423-
return Result<T[]>.Success(result);
424-
}
425-
catch (Exception ex)
426-
{
427-
return Result<T[]>.Failure(ex.Message);
428-
}
431+
return TryOperation(() => Parse<T>(lines, Options));
429432
}
430433

431434
public Result<T[]> TryParse<T>(string[] lines, ParseOptions options) where T : IFileLine, new()
432435
{
433-
try
434-
{
435-
var result = Parse<T>(lines, options);
436-
return Result<T[]>.Success(result);
437-
}
438-
catch (Exception ex)
439-
{
440-
return Result<T[]>.Failure(ex.Message);
441-
}
436+
return TryOperation(() => Parse<T>(lines, options));
442437
}
443438

444439
public Result<T[]> TryParse<T>(byte[] bytes, Encoding encoding = null) where T : IFileLine, new()
445440
{
446-
try
447-
{
448-
var result = Parse<T>(bytes, encoding, Options);
449-
return Result<T[]>.Success(result);
450-
}
451-
catch (Exception ex)
452-
{
453-
return Result<T[]>.Failure(ex.Message);
454-
}
441+
return TryOperation(() => Parse<T>(bytes, encoding, Options));
455442
}
456443

457444
public Result<T[]> TryParse<T>(byte[] bytes, Encoding encoding, ParseOptions options) where T : IFileLine, new()
458445
{
459-
try
460-
{
461-
var result = Parse<T>(bytes, encoding, options);
462-
return Result<T[]>.Success(result);
463-
}
464-
catch (Exception ex)
465-
{
466-
return Result<T[]>.Failure(ex.Message);
467-
}
446+
return TryOperation(() => Parse<T>(bytes, encoding, options));
468447
}
469448

470449
public Result<T[]> TryParse<T>(Stream stream, Encoding encoding = null) where T : IFileLine, new()
471450
{
472-
try
473-
{
474-
var result = Parse<T>(stream, encoding, Options);
475-
return Result<T[]>.Success(result);
476-
}
477-
catch (Exception ex)
478-
{
479-
return Result<T[]>.Failure(ex.Message);
480-
}
451+
return TryOperation(() => Parse<T>(stream, encoding, Options));
481452
}
482453

483454
public Result<T[]> TryParse<T>(Stream stream, Encoding encoding, ParseOptions options) where T : IFileLine, new()
484455
{
485-
try
486-
{
487-
var result = Parse<T>(stream, encoding, options);
488-
return Result<T[]>.Success(result);
489-
}
490-
catch (Exception ex)
491-
{
492-
return Result<T[]>.Failure(ex.Message);
493-
}
456+
return TryOperation(() => Parse<T>(stream, encoding, options));
494457
}
495458

496459
public async Task<Result<T[]>> TryParseAsync<T>(string filepath) where T : IFileLine, new()
497460
{
498-
try
499-
{
500-
var result = await ParseAsync<T>(filepath, Options);
501-
return Result<T[]>.Success(result);
502-
}
503-
catch (Exception ex)
504-
{
505-
return Result<T[]>.Failure(ex.Message);
506-
}
461+
return await TryOperationAsync(() => ParseAsync<T>(filepath, Options));
507462
}
508463

509464
public async Task<Result<T[]>> TryParseAsync<T>(string filepath, ParseOptions options) where T : IFileLine, new()
510465
{
511-
try
512-
{
513-
var result = await ParseAsync<T>(filepath, options);
514-
return Result<T[]>.Success(result);
515-
}
516-
catch (Exception ex)
517-
{
518-
return Result<T[]>.Failure(ex.Message);
519-
}
466+
return await TryOperationAsync(() => ParseAsync<T>(filepath, options));
520467
}
521468

522469
public async Task<Result<T[]>> TryParseAsync<T>(string[] lines) where T : IFileLine, new()
523470
{
524-
try
525-
{
526-
var result = await ParseAsync<T>(lines, Options);
527-
return Result<T[]>.Success(result);
528-
}
529-
catch (Exception ex)
530-
{
531-
return Result<T[]>.Failure(ex.Message);
532-
}
471+
return await TryOperationAsync(() => ParseAsync<T>(lines, Options));
533472
}
534473

535474
public async Task<Result<T[]>> TryParseAsync<T>(string[] lines, ParseOptions options) where T : IFileLine, new()
536475
{
537-
try
538-
{
539-
var result = await ParseAsync<T>(lines, options);
540-
return Result<T[]>.Success(result);
541-
}
542-
catch (Exception ex)
543-
{
544-
return Result<T[]>.Failure(ex.Message);
545-
}
476+
return await TryOperationAsync(() => ParseAsync<T>(lines, options));
546477
}
547478

548479
public async Task<Result<T[]>> TryParseAsync<T>(byte[] bytes, Encoding encoding = null) where T : IFileLine, new()
549480
{
550-
try
551-
{
552-
var result = await ParseAsync<T>(bytes, encoding, Options);
553-
return Result<T[]>.Success(result);
554-
}
555-
catch (Exception ex)
556-
{
557-
return Result<T[]>.Failure(ex.Message);
558-
}
481+
return await TryOperationAsync(() => ParseAsync<T>(bytes, encoding, Options));
559482
}
560483

561484
public async Task<Result<T[]>> TryParseAsync<T>(byte[] bytes, Encoding encoding, ParseOptions options) where T : IFileLine, new()
562485
{
563-
try
564-
{
565-
var result = await ParseAsync<T>(bytes, encoding, options);
566-
return Result<T[]>.Success(result);
567-
}
568-
catch (Exception ex)
569-
{
570-
return Result<T[]>.Failure(ex.Message);
571-
}
486+
return await TryOperationAsync(() => ParseAsync<T>(bytes, encoding, options));
572487
}
573488

574489
public async Task<Result<T[]>> TryParseAsync<T>(Stream stream, Encoding encoding = null) where T : IFileLine, new()
575490
{
576-
try
577-
{
578-
var result = await ParseAsync<T>(stream, encoding, Options);
579-
return Result<T[]>.Success(result);
580-
}
581-
catch (Exception ex)
582-
{
583-
return Result<T[]>.Failure(ex.Message);
584-
}
491+
return await TryOperationAsync(() => ParseAsync<T>(stream, encoding, Options));
585492
}
586493

587494
public async Task<Result<T[]>> TryParseAsync<T>(Stream stream, Encoding encoding, ParseOptions options) where T : IFileLine, new()
588495
{
589-
try
590-
{
591-
var result = await ParseAsync<T>(stream, encoding, options);
592-
return Result<T[]>.Success(result);
593-
}
594-
catch (Exception ex)
595-
{
596-
return Result<T[]>.Failure(ex.Message);
597-
}
496+
return await TryOperationAsync(() => ParseAsync<T>(stream, encoding, options));
598497
}
599498
}
600499
}

0 commit comments

Comments
 (0)