ollieatkinson / Guise

A tool to generate the Swift public interface for Frameworks and Libraries
MIT License
53 stars 5 forks source link

Sequence using Result instead of throws #14

Closed paulsamuels closed 6 years ago

paulsamuels commented 6 years ago

More an experiment than anything but if this is reasonable then it might be worth changing to Result throughout and dropping throw as it's not very composable


Why: Having do/catch blocks all over the place can clutter things up and make it harder to see the control flow of data. The Result type has functions that allow for sequencing operations with a cleaner syntax whilst still propagating errors.

This change addresses the need by: Use Result rather that do/catch for sequencing the APIGeneration

ollieatkinson commented 6 years ago

Much neater! I've rebased the change and added the following

  func postGenerationTextProcessing(options: Options) -> (_ source: String) -> Result<String, APIGeneratorError> {

    var textProcessors: [TextProcessor] = [
      RemoveSwiftOnoneSupport()
    ]

    if options.noDocumentation {
      textProcessors.append(RemoveComments())
    }

    return { source in
      Result(attempt: { try textProcessors.reduce(source, { try $1.process(input: $0) }) }).mapError({ .failedToWrite(path: options.path, error: $0) })
    }
  }
    return extractBuildArguments()
      .flatMap(generateAPI)
      .flatMap(postGenerationTextProcessing(options: options))
      .flatMap(writeToFile(options: options))