Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to snake_case builtins #305

Merged
merged 13 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions build.roc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import cli.Env
main! : _ => Result {} _
main! = \_ ->

roc_cmd = Env.var! "ROC" |> Result.withDefault "roc"
roc_cmd = Env.var! "ROC" |> Result.with_default "roc"

debug_mode =
when Env.var! "DEBUG" is
Ok str if !(Str.isEmpty str) -> Debug
Ok str if !(Str.is_empty str) -> Debug
_ -> Release

try roc_version! roc_cmd
Expand Down Expand Up @@ -48,7 +48,7 @@ roc_version! = \roc_cmd ->

roc_cmd
|> Cmd.exec! ["version"]
|> Result.mapErr RocVersionCheckFailed
|> Result.map_err RocVersionCheckFailed

get_os_and_arch! : {} => Result OSAndArch _
get_os_and_arch! = \{} ->
Expand Down Expand Up @@ -82,7 +82,7 @@ build_stub_app_lib! = \roc_cmd, stub_lib_path ->

roc_cmd
|> Cmd.exec! ["build", "--lib", "platform/libapp.roc", "--output", stub_lib_path, "--optimize"]
|> Result.mapErr ErrBuildingAppStub
|> Result.map_err ErrBuildingAppStub

stub_file_extension : OSAndArch -> Str
stub_file_extension = \os_and_arch ->
Expand All @@ -108,13 +108,13 @@ get_rust_target_folder! = \debug_mode ->

when Env.var! "CARGO_BUILD_TARGET" is
Ok target_env_var ->
if Str.isEmpty target_env_var then
if Str.is_empty target_env_var then
Ok "target/$(debug_or_release)/"
else
Ok "target/$(target_env_var)/$(debug_or_release)/"

Err e ->
try info! "Failed to get env var CARGO_BUILD_TARGET with error $(Inspect.toStr e). Assuming default CARGO_BUILD_TARGET (native)..."
try info! "Failed to get env var CARGO_BUILD_TARGET with error $(Inspect.to_str e). Assuming default CARGO_BUILD_TARGET (native)..."

Ok "target/$(debug_or_release)/"

Expand All @@ -127,7 +127,7 @@ cargo_build_host! = \debug_mode ->

"cargo"
|> Cmd.exec! (try cargo_build_args)
|> Result.mapErr ErrBuildingHostBinaries
|> Result.map_err ErrBuildingHostBinaries

copy_host_lib! : OSAndArch, Str => Result {} _
copy_host_lib! = \os_and_arch, rust_target_folder ->
Expand All @@ -140,7 +140,7 @@ copy_host_lib! = \os_and_arch, rust_target_folder ->

"cp"
|> Cmd.exec! [host_build_path, host_dest_path]
|> Result.mapErr ErrMovingPrebuiltLegacyBinary
|> Result.map_err ErrMovingPrebuiltLegacyBinary

preprocess_host! : Str, Str, Str => Result {} _
preprocess_host! = \roc_cmd, stub_lib_path, rust_target_folder ->
Expand All @@ -151,7 +151,7 @@ preprocess_host! = \roc_cmd, stub_lib_path, rust_target_folder ->

roc_cmd
|> Cmd.exec! ["preprocess-host", surgical_build_path, "platform/main.roc", stub_lib_path]
|> Result.mapErr ErrPreprocessingSurgicalBinary
|> Result.map_err ErrPreprocessingSurgicalBinary

info! : Str => Result {} _
info! = \msg ->
Expand Down
27 changes: 0 additions & 27 deletions ci/expect_scripts/http-get-json.exp
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to roc-json in lukewilliamboswell/roc-json#42

This file was deleted.

2 changes: 1 addition & 1 deletion ci/rust_http_server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hyper::service::{make_service_fn, service_fn};
use std::convert::Infallible;

async fn handle_request(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
// Encode.toBytes {foo: "Hello Json!"} Json.utf8
// Encode.to_bytes {foo: "Hello Json!"} Json.utf8
lukewilliamboswell marked this conversation as resolved.
Show resolved Hide resolved
let json_bytes: Vec<u8> = vec![123, 34, 102, 111, 111, 34, 58, 34, 72, 101, 108, 108, 111, 32, 74, 115, 111, 110, 33, 34, 125];

let response = Response::builder()
Expand Down
2 changes: 1 addition & 1 deletion examples/args.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ main! = \raw_args ->
args = List.map raw_args Arg.display

# get the second argument, the first is the executable's path
when List.get args 1 |> Result.mapErr (\_ -> ZeroArgsGiven) is
when List.get args 1 |> Result.map_err (\_ -> ZeroArgsGiven) is
Err ZeroArgsGiven ->
Err (Exit 1 "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc main.roc -- input.txt`")

Expand Down
6 changes: 3 additions & 3 deletions examples/command.roc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ status_example! = \{} ->

when result is
Ok exit_code if exit_code == 0 -> Ok {}
Ok exit_code -> Stdout.line! "Child exited with non-zero code: $(Num.toStr exit_code)"
Err err -> Stdout.line! "Error executing command: $(Inspect.toStr err)"
Ok exit_code -> Stdout.line! "Child exited with non-zero code: $(Num.to_str exit_code)"
Err err -> Stdout.line! "Error executing command: $(Inspect.to_str err)"

# Run "env" with verbose option, clear all environment variables, and pass in
# only as an environment variable "FOO"
Expand All @@ -45,6 +45,6 @@ output_example! = \{} ->
|> Cmd.args ["-v"]
|> Cmd.output!

msg = Str.fromUtf8 output.stdout |> Result.withDefault "Failed to decode stdout"
msg = Str.from_utf8 output.stdout |> Result.with_default "Failed to decode stdout"

Stdout.write! msg
2 changes: 1 addition & 1 deletion examples/countdown.roc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ tick! = \n ->
try Stdout.line! "🎉 SURPRISE! Happy Birthday! 🎂"
Ok {}
else
try Stdout.line! (n |> Num.toStr |> \s -> "$(s)...")
try Stdout.line! (n |> Num.to_str |> \s -> "$(s)...")
_ = Stdin.line! {}
tick! (n - 1)
2 changes: 1 addition & 1 deletion examples/dir.roc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ main! = \_args ->
|> try

# Check the contents of the directory
expect (Set.fromList paths_as_str) == (Set.fromList ["dirExampleA/b", "dirExampleA/child"])
expect (Set.from_list paths_as_str) == (Set.from_list ["dirExampleA/b", "dirExampleA/child"])

# Try to create a directory without a parent (should fail, ignore error)
when Dir.create! "dirExampleD/child" is
Expand Down
14 changes: 7 additions & 7 deletions examples/echo.roc
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ tick! = \{} ->
Ok {}

Err (StdinErr err) ->
try Stdout.line! (echo "Unable to read input $(Inspect.toStr err)")
try Stdout.line! (echo "Unable to read input $(Inspect.to_str err)")
Ok {}

echo : Str -> Str
echo = \shout ->
silence = \length -> List.repeat ' ' length

shout
|> Str.toUtf8
|> List.mapWithIndex \_, i ->
length = (List.len (Str.toUtf8 shout) - i)
phrase = (List.splitAt (Str.toUtf8 shout) length).before
|> Str.to_utf8
|> List.map_with_index \_, i ->
length = (List.len (Str.to_utf8 shout) - i)
phrase = (List.split_at (Str.to_utf8 shout) length).before

List.concat (silence (if i == 0 then 2 * length else length)) phrase
|> List.join
|> Str.fromUtf8
|> Result.withDefault ""
|> Str.from_utf8
|> Result.with_default ""

expect
message = "hello!"
Expand Down
4 changes: 2 additions & 2 deletions examples/env-var.roc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ main! = \_args ->

# Env.decode! does not return the same type everywhere.
# The type is determined based on type inference.
# Here `Str.joinWith` forces the type that Env.decode! returns to be `List Str`
# Here `Str.join_with` forces the type that Env.decode! returns to be `List Str`
joined_letters =
Env.decode! "LETTERS"
|> Result.map \letters -> Str.joinWith letters " "
|> Result.map \letters -> Str.join_with letters " "
|> try

Stdout.line! "Your favorite letters are: $(joined_letters)"
2 changes: 1 addition & 1 deletion examples/file-mixed.roc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ task! = \{} ->

dir_entries = try Dir.list! cwd_str

dir_entries_tr = Str.joinWith (List.map dir_entries Path.display) "\n "
dir_entries_tr = Str.join_with (List.map dir_entries Path.display) "\n "

try Stdout.line! "Directory contents:\n $(dir_entries_tr)\n"

Expand Down
6 changes: 3 additions & 3 deletions examples/file-read-buffered.roc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ main! = \_args ->

read_summary = try process_line! reader { lines_read: 0, bytes_read: 0 }

Stdout.line! "Done reading file: $(Inspect.toStr read_summary)"
Stdout.line! "Done reading file: $(Inspect.to_str read_summary)"

ReadSummary : {
lines_read : U64,
Expand All @@ -40,8 +40,8 @@ process_line! = \reader, { lines_read, bytes_read } ->
Ok bytes ->
process_line! reader {
lines_read: lines_read + 1,
bytes_read: bytes_read + (List.len bytes |> Num.intCast),
bytes_read: bytes_read + (List.len bytes |> Num.int_cast),
}

Err err ->
Err (ErrorReadingLine (Inspect.toStr err))
Err (ErrorReadingLine (Inspect.to_str err))
4 changes: 2 additions & 2 deletions examples/file-read.roc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ main! = \_args ->
run! = \{} ->
file_name = "LICENSE"
contents = try File.read_utf8! file_name
lines = Str.splitOn contents "\n"
lines = Str.split_on contents "\n"

Stdout.line! (Str.concat "First line of $(file_name): " (List.first lines |> Result.withDefault "err"))
Stdout.line! (Str.concat "First line of $(file_name): " (List.first lines |> Result.with_default "err"))
19 changes: 0 additions & 19 deletions examples/http-get-json.roc

This file was deleted.

2 changes: 1 addition & 1 deletion examples/http-get.roc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ main! = \_args ->
timeout_ms: TimeoutMilliseconds 5000,
}

body = (Str.fromUtf8 response.body)?
body = (Str.from_utf8 response.body)?

Stdout.line! "Response body:\n\t$(body)."
2 changes: 1 addition & 1 deletion examples/path.roc
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ main! = \_args ->
c = try Path.is_sym_link! path
d = try Path.type! path

Stdout.line! "isFile: $(Inspect.toStr a) isDir: $(Inspect.toStr b) isSymLink: $(Inspect.toStr c) type: $(Inspect.toStr d)"
Stdout.line! "isFile: $(Inspect.to_str a) isDir: $(Inspect.to_str b) isSymLink: $(Inspect.to_str c) type: $(Inspect.to_str d)"
2 changes: 1 addition & 1 deletion examples/piping.roc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import pf.Stdin
# Try piping in some text like this: `echo -e "test\n123" | roc piping.roc`
main! = \_args ->
lines = count! 0
Stdout.line! "I read $(Num.toStr lines) lines from stdin."
Stdout.line! "I read $(Num.to_str lines) lines from stdin."

count! = \n ->
when Stdin.line! {} is
Expand Down
2 changes: 1 addition & 1 deletion examples/record-builder.roc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ main! = \_args ->

Stdout.line! "Apples: $(apples)\nOranges: $(oranges)"

join_strs = \fruits -> Str.joinWith fruits ", "
join_strs = \fruits -> Str.join_with fruits ", "

## This doesn't actually perform any effects, but we can imagine that it does
## for the sake of this example, maybe it fetches data from a server or reads a file.
Expand Down
6 changes: 3 additions & 3 deletions examples/sqlite.roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ main! = \_args ->
todo = try query_todos_by_status! db_path "todo"

try Stdout.line! "Todo Tasks:"
try List.forEachTry! todo \{ id, task } ->
try List.for_each_try! todo \{ id, task } ->
Stdout.line! "\tid: $(id), task: $(task)"

completed = try query_todos_by_status! db_path "completed"

try Stdout.line! "\nCompleted Tasks:"
try List.forEachTry! completed \{ id, task } ->
try List.for_each_try! completed \{ id, task } ->
Stdout.line! "\tid: $(id), task: $(task)"

Ok {}
Expand All @@ -29,7 +29,7 @@ query_todos_by_status! = \db_path, status ->
query: "SELECT id, task FROM todos WHERE status = :status;",
bindings: [{ name: ":status", value: String status }],
rows: { Sqlite.decode_record <-
id: Sqlite.i64 "id" |> Sqlite.map_value Num.toStr,
id: Sqlite.i64 "id" |> Sqlite.map_value Num.to_str,
task: Sqlite.str "task",
},
}
4 changes: 2 additions & 2 deletions examples/stdin.roc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ main! = \_args ->

number_bytes = try take_number_bytes! {}

if List.isEmpty number_bytes then
if List.is_empty number_bytes then
Stderr.line! "Expected a series of number characters (0-9)"
else
when Str.fromUtf8 number_bytes is
when Str.from_utf8 number_bytes is
Ok n_str ->
Stdout.line! "Got number $(n_str)"

Expand Down
2 changes: 1 addition & 1 deletion examples/tcp-client.roc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ handle_err! = \error ->
err_str = Tcp.stream_err_to_str err
Stderr.line! "Error while writing: $(err_str)"

other -> Stderr.line! "Got other error: $(Inspect.toStr other)"
other -> Stderr.line! "Got other error: $(Inspect.to_str other)"

run! : {} => Result {} _
run! = \{} ->
Expand Down
2 changes: 1 addition & 1 deletion examples/temp-dir.roc
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ main! = \_args ->
temp_dir_str = Path.display (Env.temp_dir! {})

Stdout.line! "The temp dir path is $(temp_dir_str)"
|> Result.mapErr \err -> Exit 1 "Failed to print temp dir:\n\t$(Inspect.toStr err)"
|> Result.map_err \err -> Exit 1 "Failed to print temp dir:\n\t$(Inspect.to_str err)"
2 changes: 1 addition & 1 deletion examples/time.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ main! = \_args ->

finish = Utc.now! {}

duration = Num.toStr (Utc.delta_as_nanos start finish)
duration = Num.to_str (Utc.delta_as_nanos start finish)

Stdout.line! "Completed in $(duration)ns"
17 changes: 9 additions & 8 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
description = "Basic cli devShell flake";

inputs = {
roc.url = "github:roc-lang/roc";

# TODO restore when https://github.com/roc-lang/roc/pull/7463 lands in main
roc.url = "github:smores56/roc?ref=auto-snake-case";
lukewilliamboswell marked this conversation as resolved.
Show resolved Hide resolved

nixpkgs.follows = "roc/nixpkgs";

Expand Down
Loading
Loading