diff --git a/README.md b/README.md index bcf6adb..b539cbb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/file_streams/file_stream.gleam b/src/file_streams/file_stream.gleam index db3c5d4..a84cb43 100644 --- a/src/file_streams/file_stream.gleam +++ b/src/file_streams/file_stream.gleam @@ -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) @@ -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] } @@ -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 @@ -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 @@ -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 @@ -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 @@ -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. diff --git a/src/file_streams/text_encoding.gleam b/src/file_streams/text_encoding.gleam index 8b31d86..ba2ec25 100644 --- a/src/file_streams/text_encoding.gleam +++ b/src/file_streams/text_encoding.gleam @@ -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 }