Skip to content

Commit

Permalink
Update for Gleam v0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Aug 3, 2023
1 parent c0d64d2 commit 33a2d09
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ jobs:
with:
otp-version: "25.2"
rebar3-version: "3"
gleam-version: "0.27.0"
gleam-version: "0.30.4"
- run: gleam test
- run: gleam format --check src test
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.20.0 - 2023-08-03

- Updated for Gleam v0.30.0.

## v0.19.0 - 2023-05-17

- The `gleam/erlang/process` module gains functions `selecting_record5`
Expand Down
46 changes: 23 additions & 23 deletions src/gleam/erlang.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import gleam/list
import gleam/erlang/atom.{Atom}
import gleam/erlang/charlist.{Charlist}

external fn erl_format(String, List(a)) -> Charlist =
"io_lib" "format"
@external(erlang, "io_lib", "format")
fn erl_format(a: String, b: List(a)) -> Charlist

/// Return a string representation of any term
pub fn format(term: any) -> String {
charlist.to_string(erl_format("~p", [term]))
}

pub external fn term_to_binary(a) -> BitString =
"erlang" "term_to_binary"
@external(erlang, "erlang", "term_to_binary")
pub fn term_to_binary(a: a) -> BitString

type Safe {
Safe
}

external fn erl_binary_to_term(BitString, List(Safe)) -> Dynamic =
"erlang" "binary_to_term"
@external(erlang, "erlang", "binary_to_term")
fn erl_binary_to_term(a: BitString, b: List(Safe)) -> Dynamic

pub fn binary_to_term(binary: BitString) -> Result(Dynamic, Nil) {
case rescue(fn() { erl_binary_to_term(binary, [Safe]) }) {
Expand Down Expand Up @@ -50,8 +50,8 @@ pub type GetLineError {
/// // -> Language: <- gleam
/// Ok("gleam\n")
///
pub external fn get_line(prompt: String) -> Result(String, GetLineError) =
"gleam_erlang_ffi" "get_line"
@external(erlang, "gleam_erlang_ffi", "get_line")
pub fn get_line(prompt prompt: String) -> Result(String, GetLineError)

pub type TimeUnit {
Second
Expand All @@ -63,14 +63,14 @@ pub type TimeUnit {
/// Returns the current OS system time.
///
/// <https://erlang.org/doc/apps/erts/time_correction.html#OS_System_Time>
pub external fn system_time(TimeUnit) -> Int =
"os" "system_time"
@external(erlang, "os", "system_time")
pub fn system_time(a: TimeUnit) -> Int

/// Returns the current OS system time as a tuple of Ints
///
/// http://erlang.org/doc/man/os.html#timestamp-0
pub external fn erlang_timestamp() -> #(Int, Int, Int) =
"os" "timestamp"
@external(erlang, "os", "timestamp")
pub fn erlang_timestamp() -> #(Int, Int, Int)

/// Gleam doesn't offer any way to raise exceptions, but they may still occur
/// due to bugs when working with unsafe code, such as when calling Erlang
Expand All @@ -79,17 +79,17 @@ pub external fn erlang_timestamp() -> #(Int, Int, Int) =
/// This function will catch any error thrown and convert it into a result
/// rather than crashing the process.
///
pub external fn rescue(fn() -> a) -> Result(a, Crash) =
"gleam_erlang_ffi" "rescue"
@external(erlang, "gleam_erlang_ffi", "rescue")
pub fn rescue(a: fn() -> a) -> Result(a, Crash)

pub type Crash {
Exited(Dynamic)
Thrown(Dynamic)
Errored(Dynamic)
}

external fn get_start_arguments() -> List(Charlist) =
"init" "get_plain_arguments"
@external(erlang, "init", "get_plain_arguments")
fn get_start_arguments() -> List(Charlist)

/// Get the arguments given to the program when it was started.
///
Expand All @@ -116,10 +116,10 @@ pub fn start_arguments() -> List(String) {
/// - <https://www.erlang.org/doc/man/application.html#ensure_all_started-1>
/// - <https://www.erlang.org/doc/man/application.html#start-1>
///
pub external fn ensure_all_started(
application: Atom,
) -> Result(List(Atom), EnsureAllStartedError) =
"gleam_erlang_ffi" "ensure_all_started"
@external(erlang, "gleam_erlang_ffi", "ensure_all_started")
pub fn ensure_all_started(
application application: Atom,
) -> Result(List(Atom), EnsureAllStartedError)

pub type EnsureAllStartedError {
UnknownApplication(name: Atom)
Expand All @@ -135,9 +135,9 @@ pub type EnsureAllStartedError {
///
/// [1]: https://www.erlang.org/doc/efficiency_guide/advanced.html#unique_references
///
pub external type Reference
pub type Reference

/// Create a new unique reference.
///
pub external fn make_reference() -> Reference =
"erlang" "make_ref"
@external(erlang, "erlang", "make_ref")
pub fn make_reference() -> Reference
18 changes: 9 additions & 9 deletions src/gleam/erlang/atom.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import gleam/dynamic.{DecodeErrors, Dynamic}
/// user input into atoms) we may hit the max limit of atoms and cause the
/// virtual machine to crash.
///
pub external type Atom
pub type Atom

/// An error returned when no atom is found in the virtual machine's atom table
/// for a given string when calling the [`from_string`](#from_string) function.
Expand All @@ -36,8 +36,8 @@ pub type FromStringError {
/// > from_string("some_new_atom")
/// Error(AtomNotLoaded)
///
pub external fn from_string(String) -> Result(Atom, FromStringError) =
"gleam_erlang_ffi" "atom_from_string"
@external(erlang, "gleam_erlang_ffi", "atom_from_string")
pub fn from_string(a: String) -> Result(Atom, FromStringError)

/// Creates an atom from a string, inserting a new value into the virtual
/// machine's atom table if an atom does not already exist for the given
Expand All @@ -48,8 +48,8 @@ pub external fn from_string(String) -> Result(Atom, FromStringError) =
/// convert user input into atoms as filling the atom table will cause the
/// virtual machine to crash!
///
pub external fn create_from_string(String) -> Atom =
"erlang" "binary_to_atom"
@external(erlang, "erlang", "binary_to_atom")
pub fn create_from_string(a: String) -> Atom

/// Retuns a `String` corresponding to the text representation of the given
/// `Atom`.
Expand All @@ -60,8 +60,8 @@ pub external fn create_from_string(String) -> Atom =
/// > to_string(ok_atom)
/// "ok"
///
pub external fn to_string(Atom) -> String =
"erlang" "atom_to_binary"
@external(erlang, "erlang", "atom_to_binary")
pub fn to_string(a: Atom) -> String

/// Checks to see whether a `Dynamic` value is an atom, and return the atom if
/// it is.
Expand All @@ -75,5 +75,5 @@ pub external fn to_string(Atom) -> String =
/// > from_dynamic(dynamic.from(123))
/// Error([DecodeError(expected: "Atom", found: "Int", path: [])])
///
pub external fn from_dynamic(from: Dynamic) -> Result(Atom, DecodeErrors) =
"gleam_erlang_ffi" "atom_from_dynamic"
@external(erlang, "gleam_erlang_ffi", "atom_from_dynamic")
pub fn from_dynamic(from from: Dynamic) -> Result(Atom, DecodeErrors)
10 changes: 5 additions & 5 deletions src/gleam/erlang/charlist.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

/// A list of characters represented as ints. Commonly used by older Erlang
/// modules.
pub external type Charlist
pub type Charlist

/// Transform a charlist to a string
pub external fn to_string(Charlist) -> String =
"unicode" "characters_to_binary"
@external(erlang, "unicode", "characters_to_binary")
pub fn to_string(a: Charlist) -> String

// Calls `unicode:characters_to_binary(Data, unicode, unicode)`
// Note: `unicode is an alias for utf8`
// See <https://www.erlang.org/doc/man/unicode.html#characters_to_binary-1>

/// Transform a string to a charlist
pub external fn from_string(String) -> Charlist =
"unicode" "characters_to_list"
@external(erlang, "unicode", "characters_to_list")
pub fn from_string(a: String) -> Charlist
// Calls `unicode:characters_to_list(Data, unicode)`
// Note: `unicode is an alias for utf8`
// See <https://www.erlang.org/doc/man/unicode.html#characters_to_list-1>
46 changes: 23 additions & 23 deletions src/gleam/erlang/file.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ pub type FileInfo {
/// Error(Eacces)
/// ```
///
pub external fn file_info(String) -> Result(FileInfo, Reason) =
"gleam_erlang_ffi" "file_info"
@external(erlang, "gleam_erlang_ffi", "file_info")
pub fn file_info(a: String) -> Result(FileInfo, Reason)

/// Results in `FileInfo` about the given `path` on success, otherwise a
/// `Reason` for failure.
Expand Down Expand Up @@ -335,8 +335,8 @@ pub external fn file_info(String) -> Result(FileInfo, Reason) =
/// Error(Eacces)
/// ```
///
pub external fn link_info(String) -> Result(FileInfo, Reason) =
"gleam_erlang_ffi" "link_info"
@external(erlang, "gleam_erlang_ffi", "link_info")
pub fn link_info(a: String) -> Result(FileInfo, Reason)

/// Results in a `Bool` on success that indicates whether the given `path` has
/// a `Directory` `FileType`, otherwise a `Reason` for failure.
Expand Down Expand Up @@ -468,8 +468,8 @@ pub fn link_exists(path: String) -> Result(Bool, Reason) {
/// Error(Enoent)
/// ```
///
pub external fn make_directory(String) -> Result(Nil, Reason) =
"gleam_erlang_ffi" "make_directory"
@external(erlang, "gleam_erlang_ffi", "make_directory")
pub fn make_directory(a: String) -> Result(Nil, Reason)

/// Lists all files in a directory, except files with
/// [raw filenames](https://www.erlang.org/doc/apps/stdlib/unicode_usage.html#notes-about-raw-filenames).
Expand All @@ -487,8 +487,8 @@ pub external fn make_directory(String) -> Result(Nil, Reason) =
/// Error(Enotdir)
/// ```
///
pub external fn list_directory(String) -> Result(List(String), Reason) =
"gleam_erlang_ffi" "list_directory"
@external(erlang, "gleam_erlang_ffi", "list_directory")
pub fn list_directory(a: String) -> Result(List(String), Reason)

/// Deletes a directory.
///
Expand All @@ -505,8 +505,8 @@ pub external fn list_directory(String) -> Result(List(String), Reason) =
/// Error(Enoent)
/// ```
///
pub external fn delete_directory(String) -> Result(Nil, Reason) =
"gleam_erlang_ffi" "delete_directory"
@external(erlang, "gleam_erlang_ffi", "delete_directory")
pub fn delete_directory(a: String) -> Result(Nil, Reason)

/// Deletes a file or directory recursively.
///
Expand All @@ -525,8 +525,8 @@ pub external fn delete_directory(String) -> Result(Nil, Reason) =
/// Error(Enoent)
/// ```
///
pub external fn recursive_delete(String) -> Result(Nil, Reason) =
"gleam_erlang_ffi" "recursive_delete"
@external(erlang, "gleam_erlang_ffi", "recursive_delete")
pub fn recursive_delete(a: String) -> Result(Nil, Reason)

/// Read the contents of the given file as a String
///
Expand Down Expand Up @@ -584,8 +584,8 @@ pub fn read_bits(from path: String) -> Result(BitString, Reason) {
do_read_bits(path)
}

external fn do_read_bits(path) -> Result(BitString, Reason) =
"gleam_erlang_ffi" "read_file"
@external(erlang, "gleam_erlang_ffi", "read_file")
fn do_read_bits(a: path) -> Result(BitString, Reason)

/// Write the given String contents to a file of the given name.
///
Expand Down Expand Up @@ -636,8 +636,8 @@ pub fn write_bits(
do_write_bits(contents, path)
}

external fn do_write_bits(BitString, String) -> Result(Nil, Reason) =
"gleam_erlang_ffi" "write_file"
@external(erlang, "gleam_erlang_ffi", "write_file")
fn do_write_bits(a: BitString, b: String) -> Result(Nil, Reason)

/// Append the given String contents to a file of the given name.
///
Expand Down Expand Up @@ -688,11 +688,11 @@ pub fn append_bits(
do_append_bits(contents, path)
}

external fn do_append_bits(
contents: BitString,
path: String,
) -> Result(Nil, Reason) =
"gleam_erlang_ffi" "append_file"
@external(erlang, "gleam_erlang_ffi", "append_file")
fn do_append_bits(
contents contents: BitString,
path path: String,
) -> Result(Nil, Reason)

/// Delete the given file.
///
Expand All @@ -709,5 +709,5 @@ external fn do_append_bits(
/// Error(Enoent)
/// ```
///
pub external fn delete(String) -> Result(Nil, Reason) =
"gleam_erlang_ffi" "delete_file"
@external(erlang, "gleam_erlang_ffi", "delete_file")
pub fn delete(a: String) -> Result(Nil, Reason)
20 changes: 10 additions & 10 deletions src/gleam/erlang/os.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import gleam/map.{Map}
/// ...
/// ])
///
pub external fn get_all_env() -> Map(String, String) =
"gleam_erlang_ffi" "get_all_env"
@external(erlang, "gleam_erlang_ffi", "get_all_env")
pub fn get_all_env() -> Map(String, String)

/// Returns the value associated with the given environment variable name.
///
Expand All @@ -27,8 +27,8 @@ pub external fn get_all_env() -> Map(String, String) =
/// > get_env(name: "PWD")
/// "/home/j3rn"
///
pub external fn get_env(name: String) -> Result(String, Nil) =
"gleam_erlang_ffi" "get_env"
@external(erlang, "gleam_erlang_ffi", "get_env")
pub fn get_env(name name: String) -> Result(String, Nil)

/// Associates the given value with the given environment variable name.
///
Expand All @@ -42,8 +42,8 @@ pub external fn get_env(name: String) -> Result(String, Nil) =
/// > set_env(value: "MYVALUE", name: "MYVAR")
/// Nil
///
pub external fn set_env(name: String, value: String) -> Nil =
"gleam_erlang_ffi" "set_env"
@external(erlang, "gleam_erlang_ffi", "set_env")
pub fn set_env(name name: String, value value: String) -> Nil

/// Removes the environment variable with the given name.
///
Expand All @@ -61,8 +61,8 @@ pub external fn set_env(name: String, value: String) -> Nil =
/// > unset_env(name: "MYVAR")
/// Nil
///
pub external fn unset_env(name: String) -> Nil =
"gleam_erlang_ffi" "unset_env"
@external(erlang, "gleam_erlang_ffi", "unset_env")
pub fn unset_env(name name: String) -> Nil

/// Represents operating system kernels
pub type OsFamily {
Expand Down Expand Up @@ -91,5 +91,5 @@ pub type OsFamily {
/// > family()
/// Other("sunos")
///
pub external fn family() -> OsFamily =
"gleam_erlang_ffi" "os_family"
@external(erlang, "gleam_erlang_ffi", "os_family")
pub fn family() -> OsFamily
Loading

0 comments on commit 33a2d09

Please sign in to comment.