Bend icon indicating copy to clipboard operation
Bend copied to clipboard

Add IO functions for moving files and directories

Open kings177 opened this issue 6 months ago • 0 comments

Depends on

  • [ ] https://github.com/HigherOrderCO/HVM/issues/421

Add file and moving capabilities to Bend by implementing a move function that interfaces with the HVM's IO system.

1. move(source, destination, overwrite)

Moves a file or directory from the source path to the destination path.

Parameters

  • source: Path of the file or directory to be moved
  • destination: Path where the file or directory should be moved to
  • overwrite: If true, overwrites the destination if it exists. Otherwise, returns an error if the destination exists. Defaults to false

Example Usage

# Moving a file
result = move("old_file.txt", "new_file.txt")
match result:
    case Ok(_):
        print("File moved successfully")
    case Err(e):
        print(f"Error moving file: {e}")
# Moving a directory
result = move("old_dir", "new_dir", overwrite=True)
match result:
    case Ok(_):
        print("Directory moved successfully")
    case Err(e):
        print(f"Error moving directory: {e}")

Implementation Details

  1. Error Handling:

    • Implement custom error types in Bend for file operations (e.g., FileNotFound, PermissionDenied, FileExists).
    • Properly map HVM errors to Bend-level errors.
  2. Type Checking:

    • Ensure proper type checking for function arguments.
    • Implement appropriate type hints.
  3. HVM Integration:

    • Use the appropriate HVM calls to perform the move operation.
    • Handle the conversion between HVM results and Bend's result type.
  4. Documentation:

    • Write clear docstrings for the move function.
    • Include usage examples in the documentation.

Integration

  • Update Bend's stdlib documentation to include the new move function.
  • If necessary, update any related functions or modules that might interact with file operations.

Considerations

  • Consider adding optional parameters for preserving metadata, following symlinks, etc.
  • Ensure that the function handles edge cases like moving a file to the same location or moving a file to a read-only destination.
  • Implement proper error handling for all possible failure scenarios.

Testing

  1. Move a file or directory to a new destination.
  2. Attempt to move a non-existent file or directory.
  3. Move a file or directory to a destination that already exists
  4. Move a file or directory to a destination with insufficient permissions.
  5. Move a file or directory to a read-only destination.

kings177 avatar Aug 20 '24 20:08 kings177