-
-
Notifications
You must be signed in to change notification settings - Fork 3
Convenience Utilities
Cesil provides the CesilUtils
class which has a number of methods for quickly reading and writing data to and from streams or files. These methods are not the most performant or most configurable ways to use Cesil, but they are very quick to use.
CesilUtils
exposes the following methods for quick reading:
-
IEnumerable<TRow> Enumerate<TRow>(TextReader, Options?, object?)
- Enumerate static rows with an optional Options and context object from a TextReader
-
IEnumerable<TRow> EnumerateFromFile<TRow>(string, Options?, object?)
- Enumerate static rows with an optional Options and context object from a file
-
IEnumerable<TRow> EnumerateFromString<TRow>(string, Options?, object?)
- Enumerate static rows with an optional Options and context object from a string
-
IEnumerable<dynamic> EnumerateDynamic(TextReader, Options?, object?)
- Enumerate dynamic rows with an optional Options and context object from a TextReader
-
IEnumerable<dynamic> EnumerateDynamicFromFile(string, Options?, object?)
- Enumerate dynamic rows with an optional Options and context object from a file
-
IEnumerable<dynamic> EnumerateDynamicFromString(string, Options?, object?)
- Enumerate dynamic rows with an optional Options and context object from a string
-
IAsyncEnumerable<TRow> EnumerateAsync<TRow>(TextReader, Options?, object?, CancellationToken)
- Enumerate static rows asynchronously with an optional Options, context object, and
CancellationToken
from a TextReader
- Enumerate static rows asynchronously with an optional Options, context object, and
-
IAsyncEnumerable<TRow> EnumerateFromFileAsync<TRow>(string, Options?, object?, CancellationToken)
- Enumerate static rows asynchronously with an optional Options, context object, and
CancellationToken
from a file
- Enumerate static rows asynchronously with an optional Options, context object, and
-
IAsyncEnumerable<TRow> EnumerateFromStringAsync<TRow>(string, Options?, object?, CancellationToken)
- Enumerate static rows asynchronously with an optional Options, context object, and
CancellationToken
from a string
- Enumerate static rows asynchronously with an optional Options, context object, and
-
IAsyncEnumerable<dynamic> EnumerateDynamicAsync(TextReader, Options?, object?, CancellationToken)
- Enumerate dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
from a TextReader
- Enumerate dynamic rows asynchronously with an optional Options, context object, and
-
IAsyncEnumerable<dynamic> EnumerateDynamicFromFileAsync(TextReader, Options?, object?, CancellationToken)
- Enumerate dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
from a file
- Enumerate dynamic rows asynchronously with an optional Options, context object, and
-
IAsyncEnumerable<dynamic> EnumerateDynamicFromStringAsync(TextReader, Options?, object?, CancellationToken)
- Enumerate dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
from a string
- Enumerate dynamic rows asynchronously with an optional Options, context object, and
All returned enumerations are lazy, that is work will happen as you enumerate them.
When reading from a file, Cesil will attempt to detect encodings automatically using StreamReader's
byte-order mark (BOM) inspection logic.
CesilUtils
exposes the following methods for quick reading:
-
void Write<TRow>(IEnumerable<TRow>, TextWriter, Options?, object?)
- Write static rows with an optional Options and context object to a TextWriter
-
void WriteToFile<TRow>(IEnumerable<TRow>, string, Options?, object?)
- Write static rows with an optional Options and context object to a file
-
void WriteToString<TRow>(IEnumerable<TRow>, Options?, object?)
- Write static rows with an optional Options and context object to a string
-
void WriteDynamic(IEnumerable<dynamic>, TextWriter, Options?, object?)
- Write dynamic rows with an optional Options and context object to a TextWriter
-
void WriteDynamicToFile(IEnumerable<dynamic>, string, Options?, object?)
- Write dynamic rows with an optional Options and context object to a file
-
void WriteDynamicToString(IEnumerable<dynamic>, Options?, object?)
- Write dynamic rows with an optional Options and context object to a string
-
ValueTask WriteAsync<TRow>(IEnumerable<TRow>, TextWriter, Options?, object?, CancellationToken)
- Write static rows asynchronously with an optional Options, context object, and
CancellationToken
to a TextWriter
- Write static rows asynchronously with an optional Options, context object, and
-
ValueTask WriteAsync<TRow>(IAsyncEnumerable<TRow>, TextWriter, Options?, object?, CancellationToken)
- Write static rows asynchronously with an optional Options, context object, and
CancellationToken
to a TextWriter
- Write static rows asynchronously with an optional Options, context object, and
-
ValueTask WriteDynamicAsync(IEnumerable<dynamic>, TextWriter, Options?, object?, CancellationToken)
- Write dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
to a TextWriter
- Write dynamic rows asynchronously with an optional Options, context object, and
-
ValueTask WriteDynamicAsync(IAsyncEnumerable<dynamic>, TextWriter, Options?, object?, CancellationToken)
- Write dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
to a TextWriter
- Write dynamic rows asynchronously with an optional Options, context object, and
-
ValueTask WriteToFileAsync<TRow>(IEnumerable<TRow>, string, Options?, object?, CancellationToken)
- Write static rows asynchronously with an optional Options, context object, and
CancellationToken
to a file
- Write static rows asynchronously with an optional Options, context object, and
-
ValueTask WriteToFileAsync<TRow>(IAsyncEnumerable<TRow>, string, Options?, object?, CancellationToken)
- Write static rows asynchronously with an optional Options, context object, and
CancellationToken
to a file
- Write static rows asynchronously with an optional Options, context object, and
-
ValueTask WriteToStringAsync<TRow>(IEnumerable<TRow>, Options?, object?, CancellationToken)
- Write static rows asynchronously with an optional Options, context object, and
CancellationToken
to a string
- Write static rows asynchronously with an optional Options, context object, and
-
ValueTask WriteToStringAsync<TRow>(IAsyncEnumerable<TRow>, Options?, object?, CancellationToken)
- Write static rows asynchronously with an optional Options, context object, and
CancellationToken
to a string
- Write static rows asynchronously with an optional Options, context object, and
-
ValueTask WriteDynamicToFileAsync(IEnumerable<dynamic>, string, Options?, object?, CancellationToken)
- Write dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
to a file
- Write dynamic rows asynchronously with an optional Options, context object, and
-
ValueTask WriteDynamicToFileAsync(IAsyncEnumerable<dynamic>, string, Options?, object?, CancellationToken)
- Write dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
to a file
- Write dynamic rows asynchronously with an optional Options, context object, and
-
ValueTask WriteDynamicToStringAsync(IEnumerable<dynamic>, Options?, object?, CancellationToken)
- Write dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
to a string
- Write dynamic rows asynchronously with an optional Options, context object, and
-
ValueTask WriteDynamicToStringAsync(IAsyncEnumerable<dynamic>, Options?, object?, CancellationToken)
- Write dynamic rows asynchronously with an optional Options, context object, and
CancellationToken
to a string
- Write dynamic rows asynchronously with an optional Options, context object, and
When writing to a file, Cesil will create the file (overwriting it if it already exists) and use the UTF-8 encoding when writing data.
Using CesilUtils
introduces the following inefficiencies:
CesilUtils
makes a new configuration object for each invocation. This involves extensively consulting the ITypeDescriber
of the given (or default) Options, and generating some code for reading or writing members of various types. Explicitly creating and managing your IBoundConfiguration<TRow>
will let you re-use this work.
This inefficiency is only present if your code repeatedly reads or writes the same types with the same Options, giving you an opportunity to benefit from re-using the work.
The async
code behind the interfaces goes through substantial effort to avoid using any async
machinery if the code can complete synchronously without blocking. By contrast, the code behind CesilUtils
is relatively simple and naive - only leveraging ValueTask
to avoid unnecessary allocations.
This inefficiency is only likely to matter in very hot code paths where completing synchronously is commonplace.
The various reading, writing, and configuration interfaces provide many methods that allow to control exactly what Cesil does in a fine grained manner. By contrast, CesilUtils
only supports lazily reading and writing batches of rows to streams, files, or strings.
An incomplete list of things you can do with Cesil, but not directly with CesilUtils
is:
- read/write single rows
- read directly into collections other than
IEnumerable<T>
andIAsyncEnumerable<T>
- read/write comments
- precisely control when things are disposed
- read from a
PipeReader
or aReadOnlySequence<char|byte>
- write to a
PipeWriter
or anIBufferWriter<char|byte>
- reuse or pre-allocate rows to be populated when reading