Skip to content

Commit

Permalink
Stop convertting NSError to String on file operation result.
Browse files Browse the repository at this point in the history
  • Loading branch information
nori0620 committed Jan 20, 2015
1 parent 430b994 commit 7ba0b95
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -165,7 +165,7 @@ let data = binFile.readData()!

## Error handling

`touch`/`remove`/`copyTo`/`writeTo`/`mkdir` returns `Result` Object.
`touch`/`remove`/`copyTo`/`writeTo`/`mkdir` returns `Result` as Enum.

If operation is success, `Result` has `value` property.
If operation is failure,`Result` has `error` property.
Expand All @@ -188,7 +188,7 @@ Path.documentsDir["subdir"].mkdir()
.onSuccess({ (value:Path) in
println( value )
})
.onFailure({ (error:String) in
.onFailure({ (error:NSError) in
println( error )
})
````
Expand Down
12 changes: 6 additions & 6 deletions SwiftFilePath/Path.swift
Expand Up @@ -59,35 +59,35 @@ public class Path {
return path_string
}

public func remove() -> Result<Path,String> {
public func remove() -> Result<Path,NSError> {
assert(self.exists,"To remove file, file MUST be exists")
var error: NSError?
let result = fileManager.removeItemAtPath(path_string, error:&error)
return result
? Result(success: self)
: Result(failure: "Failed to remove file.<error:\(error?.localizedDescription) path:\(path_string)>");
: Result(failure: error!);
}

public func copyTo(toPath:Path) -> Result<Path,String> {
public func copyTo(toPath:Path) -> Result<Path,NSError> {
assert(self.exists,"To copy file, file MUST be exists")
var error: NSError?
let result = fileManager.copyItemAtPath(path_string,
toPath: toPath.toString(),
error: &error)
return result
? Result(success: self)
: Result(failure: "Failed to copy file.<error:\(error?.localizedDescription) from-path:\(path_string) to-path:\(toPath)>");
: Result(failure: error!)
}

public func moveTo(toPath:Path) -> Result<Path,String> {
public func moveTo(toPath:Path) -> Result<Path,NSError> {
assert(self.exists,"To move file, file MUST be exists")
var error: NSError?
let result = fileManager.moveItemAtPath(path_string,
toPath: toPath.toString(),
error: &error)
return result
? Result(success: self)
: Result(failure: "Failed to move file.<error:\(error?.localizedDescription) from-path:\(path_string) to-path:\(toPath)>");
: Result(failure: error!)
}

private func loadAttributes() -> NSDictionary? {
Expand Down
7 changes: 2 additions & 5 deletions SwiftFilePath/PathExtensionDir.swift
Expand Up @@ -71,10 +71,7 @@ extension Path: SequenceType {
return self.content(path)
}

public func mkdir() -> Result<Path,String> {
if( self.exists ){
return Result(failure: "Already exists.<path:\(path_string)>")
}
public func mkdir() -> Result<Path,NSError> {
var error: NSError?
let result = fileManager.createDirectoryAtPath(path_string,
withIntermediateDirectories:true,
Expand All @@ -83,7 +80,7 @@ extension Path: SequenceType {
)
return result
? Result(success: self)
: Result(failure: "Failed to mkdir.< error:\(error?.localizedDescription) path:\(path_string) >");
: Result(failure: error!)

}

Expand Down
27 changes: 11 additions & 16 deletions SwiftFilePath/PathExtensionFile.swift
Expand Up @@ -13,14 +13,14 @@ extension Path {
return path_string.pathExtension
}

public func touch() -> Result<Path,String> {
public func touch() -> Result<Path,NSError> {
assert(!self.isDir,"Can NOT touch to dir")
return self.exists
? self.updateModificationDate()
: self.createEmptyFile()
}

public func updateModificationDate(date: NSDate = NSDate() ) -> Result<Path,String>{
public func updateModificationDate(date: NSDate = NSDate() ) -> Result<Path,NSError>{
var error: NSError?
let result = fileManager.setAttributes(
[NSFileModificationDate :date],
Expand All @@ -29,17 +29,11 @@ extension Path {
)
return result
? Result(success: self)
: Result(failure: "Failed to modify file.< error:\(error?.localizedDescription) path:\(path_string) >");
: Result(failure: error!)
}

private func createEmptyFile() -> Result<Path,String>{
let result = fileManager.createFileAtPath(path_string,
contents:NSData(),
attributes:nil
)
return result
? Result(success: self)
: Result(failure: "Failed to create file:\(path_string)");
private func createEmptyFile() -> Result<Path,NSError>{
return self.writeString("")
}

// MARK: - read/write String
Expand All @@ -58,7 +52,7 @@ extension Path {
return read
}

public func writeString(string:String) -> Result<Path,String> {
public func writeString(string:String) -> Result<Path,NSError> {
assert(!self.isDir,"Can NOT write data from dir")
var error: NSError?
let result = string.writeToFile(path_string,
Expand All @@ -67,7 +61,7 @@ extension Path {
error: &error)
return result
? Result(success: self)
: Result(failure: "Failed to write file.< error:\(error?.localizedDescription) path:\(path_string) >");
: Result(failure: error!)
}

// MARK: - read/write NSData
Expand All @@ -77,12 +71,13 @@ extension Path {
return NSData(contentsOfFile: path_string)
}

public func writeData(data:NSData) -> Result<Path,String> {
public func writeData(data:NSData) -> Result<Path,NSError> {
assert(!self.isDir,"Can NOT write data from dir")
let result = data.writeToFile(path_string, atomically:true)
var error: NSError?
let result = data.writeToFile(path_string, options:.DataWritingAtomic, error: &error)
return result
? Result(success: self)
: Result(failure: "Failed to write file.< path:\(path_string) >");
: Result(failure: error!)
}

}
2 changes: 2 additions & 0 deletions SwiftFilePathTests/SwiftFilePathTests.swift
Expand Up @@ -122,6 +122,7 @@ class SwiftFilePathTests: XCTestCase {
XCTAssertFalse( file.exists )
}


}

func testMkdirAndRemove(){
Expand Down Expand Up @@ -407,6 +408,7 @@ class SwiftFilePathTests: XCTestCase {
XCTAssertEqual(error,"NG!")
})


XCTAssertTrue( result.isFailure )
XCTAssertEqual( result.error!, "NG!" )
XCTAssertTrue( callOnFailure )
Expand Down

0 comments on commit 7ba0b95

Please sign in to comment.