Skip to content

Commit

Permalink
Minor code and documentation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney committed Jul 26, 2024
1 parent dff9031 commit 1f63f11
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,18 @@ pub fn main() {

If a text encoding is specified when opening a file stream it allows for
reading and writing of characters and lines of text stored in that encoding.
Open a text file stream using the `file_stream.open_read_text()` and
`file_stream.open_write_text()`functions. The supported encodings are `Latin1`,
To open a text file stream use the `file_stream.open_read_text()` and
`file_stream.open_write_text()` functions. The supported encodings are `Latin1`,
`Unicode` (UTF-8), `Utf16`, and `Utf32`. The default encoding is `Latin1`.

File streams opened with a text encoding aren't compatible with the `Raw` file
open mode that is used to significantly improve IO performance on Erlang.
Specifying both `Raw` and `Encoding` when calling `file_stream.open()` returns
`Error(Enotsup)`.
open mode that significantly improves IO performance on Erlang. Specifying both
`Raw` and `Encoding` when calling `file_stream.open()` returns `Error(Enotsup)`.

Although a text encoding can't be specified with `Raw` mode,
`file_stream.read_line()` and `file_stream.write_chars()` can still be used to
work with UTF-8 data. This means that text encoded as UTF-8 can be handled with
high performance in `Raw` mode.
Although a text encoding can't be specified with `Raw` mode, the
`file_stream.read_line()` and `file_stream.write_chars()` functions can still be
used to work with UTF-8 data. This means that text encoded as UTF-8 can be
handled with high performance in `Raw` mode.

When a text encoding other than `Latin1` is specified, functions that read and
write raw bytes and other binary data aren't supported and will return
Expand Down
37 changes: 23 additions & 14 deletions src/file_streams/file_stream.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ pub fn open(
filename: String,
modes: List(FileOpenMode),
) -> Result(FileStream, FileStreamError) {
let is_raw = list.contains(modes, file_open_mode.Raw)
let is_raw = modes |> list.contains(file_open_mode.Raw)

// Find the text encoding, if one was specified
let encoding =
list.find_map(modes, fn(m) {
modes
|> list.find_map(fn(m) {
case m {
file_open_mode.Encoding(e) -> Ok(e)
_ -> Error(Nil)
Expand All @@ -55,13 +56,13 @@ pub fn open(
True, Some(_) -> Error(file_stream_error.Enotsup)

True, None -> Ok(None)
False, _ -> Ok(option.or(encoding, Some(text_encoding.Latin1)))
False, _ -> Ok(encoding |> option.or(Some(text_encoding.Latin1)))
}
use encoding <- result.try(encoding)

// Binary mode is forced on so the Erlang APIs return binaries rather than
// lists
let mode = case list.contains(modes, file_open_mode.Binary) {
let mode = case modes |> list.contains(file_open_mode.Binary) {
True -> modes
False -> [file_open_mode.Binary, ..modes]
}
Expand All @@ -87,11 +88,13 @@ fn erl_file_open(
/// - `Raw`
///
pub fn open_read(filename: String) -> Result(FileStream, FileStreamError) {
open(filename, [
let modes = [
file_open_mode.Read,
file_open_mode.ReadAhead(64 * 1024),
file_open_mode.Raw,
])
]

open(filename, modes)
}

/// Opens a new file stream for reading encoded text from a file. If only
Expand All @@ -111,11 +114,13 @@ pub fn open_read_text(
filename: String,
encoding: TextEncoding,
) -> Result(FileStream, FileStreamError) {
open(filename, [
let modes = [
file_open_mode.Read,
file_open_mode.ReadAhead(64 * 1024),
file_open_mode.Encoding(encoding),
])
]

open(filename, modes)
}

/// Opens a new file stream for writing to a file. Allows for efficient writing
Expand All @@ -128,11 +133,13 @@ pub fn open_read_text(
/// - `Raw`
///
pub fn open_write(filename: String) -> Result(FileStream, FileStreamError) {
open(filename, [
let modes = [
file_open_mode.Write,
file_open_mode.DelayedWrite(size: 64 * 1024, delay: 2000),
file_open_mode.Raw,
])
]

open(filename, modes)
}

/// Opens a new file stream for writing encoded text to a file. If only writing
Expand All @@ -141,8 +148,8 @@ pub fn open_write(filename: String) -> Result(FileStream, FileStreamError) {
///
/// The modes used are:
///
/// - `Read`
/// - `ReadAhead(size: 64 * 1024)`
/// - `Write`
/// - `DelayedWrite(size: 64 * 1024, delay: 2000)`
/// - `Encoding(encoding)`
///
/// The text encoding for a file stream can be changed with
Expand All @@ -152,11 +159,13 @@ pub fn open_write_text(
filename: String,
encoding: TextEncoding,
) -> Result(FileStream, FileStreamError) {
open(filename, [
let modes = [
file_open_mode.Write,
file_open_mode.DelayedWrite(size: 64 * 1024, delay: 2000),
file_open_mode.Encoding(encoding),
])
]

open(filename, modes)
}

/// Closes an open file stream.
Expand Down
4 changes: 2 additions & 2 deletions src/file_streams/text_encoding.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pub type TextEncoding {
/// Endianness specifier used by the `Utf16` and `Utf32` text encodings.
///
pub type Endianness {
/// Big endian. This is much less common than little endian.
/// Big endian.
Big

/// Little endian. This is much more common than big endian.
/// Little endian. The most common endianness mode, use this if uncertain.
Little
}

0 comments on commit 1f63f11

Please sign in to comment.