Skip to content

Commit

Permalink
Merge pull request #24 from rameel/formatting
Browse files Browse the repository at this point in the history
Formatting and clean up
  • Loading branch information
rameel authored Sep 12, 2024
2 parents 17426b9 + c4bfed0 commit f73d07d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 87 deletions.
24 changes: 12 additions & 12 deletions src/Ramstack.FileSystem.Abstractions/VirtualFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public static async ValueTask<StreamReader> OpenTextAsync(this VirtualFile file,
return new StreamReader(stream, encoding);
}

/// <summary>
/// Asynchronously writes the specified content to a file. If the file exists, an exception will be thrown.
/// </summary>
/// <param name="file">The file to write to.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAsync(this VirtualFile file, Stream stream, CancellationToken cancellationToken = default) =>
file.WriteAsync(stream, overwrite: false, cancellationToken);

/// <summary>
/// Asynchronously reads all the text in the current file.
/// </summary>
Expand Down Expand Up @@ -239,18 +251,6 @@ static void Error() =>
throw new EndOfStreamException();
}

/// <summary>
/// Asynchronously writes the specified content to a file. If the file exists, an exception will be thrown.
/// </summary>
/// <param name="file">The file to write to.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAsync(this VirtualFile file, Stream stream, CancellationToken cancellationToken = default) =>
file.WriteAsync(stream, overwrite: false, cancellationToken);

/// <summary>
/// Asynchronously writes the specified string to the current file. If the file already exists, it is truncated and overwritten.
/// </summary>
Expand Down
150 changes: 75 additions & 75 deletions src/Ramstack.FileSystem.Abstractions/VirtualFileSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,44 @@ namespace Ramstack.FileSystem;
public static partial class VirtualFileSystemExtensions
{
/// <summary>
/// Asynchronously determines whether the file exists.
/// Asynchronously opens the file at the specified path for reading.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation.
/// The task result is <see langword="true"/> if the file exists; otherwise, <see langword="false"/>.
/// A <see cref="ValueTask"/> representing the asynchronous operation, containing a stream for reading the file content.
/// </returns>
public static ValueTask<bool> FileExistsAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ExistsAsync(cancellationToken);
public static ValueTask<Stream> OpenReadAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).OpenReadAsync(cancellationToken);

/// <summary>
/// Asynchronously opens the file at the specified path for reading.
/// Asynchronously returns a <see cref="StreamReader"/> with <see cref="Encoding.UTF8"/> encoding for a file at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <param name="cancellationToken">The optional cancellation token used for canceling the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation, containing a stream for reading the file content.
/// A task representing the asynchronous operation and returns a <see cref="StreamReader"/> that reads from the text file.
/// </returns>
public static ValueTask<Stream> OpenReadAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).OpenReadAsync(cancellationToken);
public static Task<StreamReader> OpenTextAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.OpenTextAsync(path, Encoding.UTF8, cancellationToken);

/// <summary>
/// Asynchronously returns a <see cref="StreamReader"/> with the specified character encoding for a file at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="encoding">The character encoding to use.</param>
/// <param name="cancellationToken">The optional cancellation token used for canceling the operation.</param>
/// <returns>
/// A task representing the asynchronous operation and returns a <see cref="StreamReader"/> that reads from the text file.
/// </returns>
public static async Task<StreamReader> OpenTextAsync(this IVirtualFileSystem fs, string path, Encoding encoding, CancellationToken cancellationToken = default)
{
var stream = await fs.OpenReadAsync(path, cancellationToken).ConfigureAwait(false);
return new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: -1, leaveOpen: false);
}

/// <summary>
/// Asynchronously opens the file at the specified path for writing.
Expand All @@ -44,6 +59,41 @@ public static ValueTask<Stream> OpenReadAsync(this IVirtualFileSystem fs, string
public static ValueTask<Stream> OpenWriteAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).OpenWriteAsync(cancellationToken);

/// <summary>
/// Asynchronously writes the specified content to a file at the specified path. If the file exists, an exception will be thrown.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAsync(this IVirtualFileSystem fs, string path, Stream stream, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAsync(stream, overwrite: false, cancellationToken);

/// <summary>
/// Asynchronously writes the specified content to a file at the specified path, creating a new file or overwriting an existing one.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file;
/// <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
public static ValueTask WriteAsync(this IVirtualFileSystem fs, string path, Stream stream, bool overwrite, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAsync(stream, overwrite, cancellationToken);

/// <summary>
/// Asynchronously reads all the text in the file with the specified encoding.
/// </summary>
Expand Down Expand Up @@ -111,41 +161,6 @@ public static ValueTask<string[]> ReadAllLinesAsync(this IVirtualFileSystem fs,
public static ValueTask<byte[]> ReadAllBytesAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ReadAllBytesAsync(cancellationToken);

/// <summary>
/// Asynchronously writes the specified content to a file at the specified path. If the file exists, an exception will be thrown.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAsync(this IVirtualFileSystem fs, string path, Stream stream, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAsync(stream, overwrite: false, cancellationToken);

/// <summary>
/// Asynchronously writes the specified content to a file at the specified path, creating a new file or overwriting an existing one.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file;
/// <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
public static ValueTask WriteAsync(this IVirtualFileSystem fs, string path, Stream stream, bool overwrite, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAsync(stream, overwrite, cancellationToken);

/// <summary>
/// Asynchronously writes the specified string to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
Expand Down Expand Up @@ -253,6 +268,19 @@ public static ValueTask WriteAllBytesAsync(this IVirtualFileSystem fs, string pa
public static ValueTask WriteAllBytesAsync(this IVirtualFileSystem fs, string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllBytesAsync(bytes, cancellationToken);

/// <summary>
/// Asynchronously determines whether the file exists.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="cancellationToken">A cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation.
/// The task result is <see langword="true"/> if the file exists; otherwise, <see langword="false"/>.
/// </returns>
public static ValueTask<bool> FileExistsAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ExistsAsync(cancellationToken);

/// <summary>
/// Asynchronously deletes the file at the specified path. No exception is thrown if the file does not exist.
/// </summary>
Expand Down Expand Up @@ -372,32 +400,4 @@ public static IAsyncEnumerable<VirtualFile> GetFilesAsync(this IVirtualFileSyste
/// </returns>
public static IAsyncEnumerable<VirtualDirectory> GetDirectoriesAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetDirectory(path).GetDirectoriesAsync(cancellationToken);

/// <summary>
/// Asynchronously returns a <see cref="StreamReader"/> with <see cref="Encoding.UTF8"/> encoding for a file at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="cancellationToken">The optional cancellation token used for canceling the operation.</param>
/// <returns>
/// A task representing the asynchronous operation and returns a <see cref="StreamReader"/> that reads from the text file.
/// </returns>
public static Task<StreamReader> OpenTextAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.OpenTextAsync(path, Encoding.UTF8, cancellationToken);

/// <summary>
/// Asynchronously returns a <see cref="StreamReader"/> with the specified character encoding for a file at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="encoding">The character encoding to use.</param>
/// <param name="cancellationToken">The optional cancellation token used for canceling the operation.</param>
/// <returns>
/// A task representing the asynchronous operation and returns a <see cref="StreamReader"/> that reads from the text file.
/// </returns>
public static async Task<StreamReader> OpenTextAsync(this IVirtualFileSystem fs, string path, Encoding encoding, CancellationToken cancellationToken = default)
{
var stream = await fs.OpenReadAsync(path, cancellationToken).ConfigureAwait(false);
return new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: -1, leaveOpen: false);
}
}

0 comments on commit f73d07d

Please sign in to comment.