@@ -2,6 +2,9 @@ import Files
22import ShellOut
33import struct Foundation. Data
44
5+ /// Script type
6+ ///
7+ /// By using method chain, you can express a workflow of your script in Swift.
58public struct Script < T> {
69 var input : Result < T , Error >
710
@@ -17,16 +20,14 @@ public struct Script<T> {
1720 self . init ( input: . failure( failure) )
1821 }
1922
23+ /// This function collects inputs from stdin and returns as `String`.
24+ /// - Returns: ``Script`` object containing `String` value or failure
2025 public func stdin( ) -> Script < String > {
2126 guard let array: [ String ] = readLine ( ) ? . split ( separator: " " ) . map ( { s in String ( s) } ) else { return . init( success: " " ) }
2227 return . init( success: array. joined ( separator: " " ) )
2328 }
2429
25- // public func stdin() -> Script<[String]> {
26- // guard let array: [String] = readLine()?.split(separator: " ").map({ s in String(s) }) else { return .init(success: []) }
27- // return .init(success: array)
28- // }
29-
30+ /// This function accepts inputs and outputs it to stdout.
3031 public func stdout( ) {
3132 switch input {
3233 case . success( let input) :
@@ -36,6 +37,9 @@ public struct Script<T> {
3637 }
3738 }
3839
40+ /// This function executes externtal command.
41+ /// - Parameter command: `Array` of `String` to execute command
42+ /// - Returns: ``Script`` object containing `String` value or failure
3943 public func exec( _ command: [ String ] ) -> Script < String > {
4044 do {
4145 return . init( success: try shellOut ( to: command) )
@@ -44,6 +48,9 @@ public struct Script<T> {
4448 }
4549 }
4650
51+ /// This function executes externtal command.
52+ /// - Parameter command: `String` to execute command
53+ /// - Returns: ``Script`` object containing `String` value or failure
4754 public func exec( _ command: String ) -> Script < String > {
4855 do {
4956 return . init( success: try shellOut ( to: command) )
@@ -52,6 +59,9 @@ public struct Script<T> {
5259 }
5360 }
5461
62+ /// This function pass `self` to next function in the method chain if a file exists.
63+ /// - Parameter filename: `String` to represent name of a file
64+ /// - Returns: ``Script`` object passed from previous function or failure
5565 public func ifExists( _ filename: String ) -> Script < T > {
5666 do {
5767 _ = try File ( path: filename)
@@ -61,6 +71,9 @@ public struct Script<T> {
6171 }
6272 }
6373
74+ /// This function lets user modify the contained value in the method chain.
75+ /// - Parameter transform: A closure to modify the contained value
76+ /// - Returns: ``Script`` object with modified value or failure
6477 public func map< N> ( _ transform: ( T ) -> N ) -> Script < N > {
6578 switch input {
6679 case . success( let input) :
@@ -70,6 +83,8 @@ public struct Script<T> {
7083 }
7184 }
7285
86+ /// This function returns the contained value, ending the method chain.
87+ /// - Returns: The contained value or exit with failure
7388 public func raw( ) -> T {
7489 switch input {
7590 case . success( let input) :
@@ -79,6 +94,8 @@ public struct Script<T> {
7994 }
8095 }
8196
97+ /// This function returns the contained value or error as `String`.
98+ /// - Returns: `String` representaion of the contained value or error
8299 public func asString( ) -> String {
83100 switch input {
84101 case . success( let input) :
0 commit comments