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

Port updates from basic-webserver & basic-ssg upgrades, fix PR comments #290

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions crates/roc_host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ pub extern "C" fn rust_main(args: ReadOnlyRocList<ReadOnlyRocStr>) -> i32 {
init();

extern "C" {
#[link_name = "roc__mainForHost_1_exposed"]
#[link_name = "roc__main_for_host_1_exposed"]
pub fn roc_main_for_host_caller(not_used: i32) -> i32;

#[link_name = "roc__mainForHost_1_exposed_size"]
#[link_name = "roc__main_for_host_1_exposed_size"]
pub fn roc_main__for_host_size() -> usize;
}

Expand Down
6 changes: 3 additions & 3 deletions examples/file-mixed.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import pf.Path
import pf.Env
import pf.Dir

outTxtPath = "out.txt"
out_txt_path = "out.txt"

task! = \{} ->

Expand All @@ -23,9 +23,9 @@ task! = \{} ->

try Stdout.line! "Writing a string to out.txt"

try File.write_utf8! "a string!" outTxtPath
try File.write_utf8! "a string!" out_txt_path

contents = try File.read_utf8! outTxtPath
contents = try File.read_utf8! out_txt_path

Stdout.line! "I read the file back. Its contents: \"$(contents)\""

Expand Down
4 changes: 2 additions & 2 deletions examples/stdin.roc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ main! = \{} ->
Stderr.line! "Expected a series of number characters (0-9)"
else
when Str.fromUtf8 number_bytes is
Ok nStr ->
Stdout.line! "Got number $(nStr)"
Ok n_str ->
Stdout.line! "Got number $(n_str)"

Err _ ->
Stderr.line! "Error, bad utf8"
Expand Down
12 changes: 6 additions & 6 deletions examples/tcp-client.roc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ handle_err! : []_ => Result {} _
handle_err! = \error ->
when error is
TcpConnectErr err ->
errStr = Tcp.connect_err_to_str err
err_str = Tcp.connect_err_to_str err
Stderr.line!
"""
Failed to connect: $(errStr)
Failed to connect: $(err_str)
If you don't have anything listening on port 8085, run:
\$ nc -l 8085
Expand All @@ -30,12 +30,12 @@ handle_err! = \error ->
Stderr.line! "Received invalid UTF-8 data"

TcpReadErr err ->
errStr = Tcp.stream_err_to_str err
Stderr.line! "Error while reading: $(errStr)"
err_str = Tcp.stream_err_to_str err
Stderr.line! "Error while reading: $(err_str)"

TcpWriteErr err ->
errStr = Tcp.stream_err_to_str err
Stderr.line! "Error while writing: $(errStr)"
err_str = Tcp.stream_err_to_str err
Stderr.line! "Error while writing: $(err_str)"

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

Expand Down
9 changes: 3 additions & 6 deletions platform/Cmd.roc
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,9 @@ env = \@Cmd cmd, key, value ->
## ```
##
envs : Cmd, List (Str, Str) -> Cmd
envs = \@Cmd cmd, keyValues ->
values = keyValues |> List.joinMap \(key, value) -> [key, value]
@Cmd
{ cmd &
envs: List.concat cmd.envs values,
}
envs = \@Cmd cmd, key_values ->
values = key_values |> List.joinMap \(key, value) -> [key, value]
@Cmd { cmd & envs: List.concat cmd.envs values }

## Clear all environment variables, and prevent inheriting from parent, only
## the environment variables provided to command are available to the child.
Expand Down
16 changes: 8 additions & 8 deletions platform/Env.roc
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ decode! : Str => Result val [VarNotFound, DecodeErr DecodeError] where val imple
decode! = \name ->
when Host.env_var! name is
Err {} -> Err VarNotFound
Ok varStr ->
Str.toUtf8 varStr
Ok var_str ->
Str.toUtf8 var_str
|> Decode.fromBytes (EnvDecoding.format {})
|> Result.mapErr (\_ -> DecodeErr TooShort)

Expand Down Expand Up @@ -138,22 +138,22 @@ OS : [LINUX, MACOS, WINDOWS, OTHER Str]
platform! : {} => { arch : ARCH, os : OS }
platform! = \{} ->

fromRust = Host.current_arch_os! {}
from_rust = Host.current_arch_os! {}

arch =
when fromRust.arch is
when from_rust.arch is
"x86" -> X86
"x86_64" -> X64
"arm" -> ARM
"aarch64" -> AARCH64
_ -> OTHER fromRust.arch
_ -> OTHER from_rust.arch

os =
when fromRust.os is
when from_rust.os is
"linux" -> LINUX
"macos" -> MACOS
"windows" -> WINDOWS
_ -> OTHER fromRust.os
_ -> OTHER from_rust.os

{ arch, os }

Expand All @@ -169,4 +169,4 @@ platform! = \{} ->
temp_dir! : {} => Path
temp_dir! = \{} ->
Host.temp_dir! {}
|> \pathOSStringBytes -> InternalPath.from_os_bytes pathOSStringBytes
|> InternalPath.from_os_bytes
132 changes: 67 additions & 65 deletions platform/EnvDecoding.roc
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ module [

EnvFormat := {} implements [
DecoderFormatting {
u8: envU8,
u16: envU16,
u32: envU32,
u64: envU64,
u128: envU128,
i8: envI8,
i16: envI16,
i32: envI32,
i64: envI64,
i128: envI128,
f32: envF32,
f64: envF64,
dec: envDec,
bool: envBool,
string: envString,
list: envList,
record: envRecord,
tuple: envTuple,
u8: env_u8,
u16: env_u16,
u32: env_u32,
u64: env_u64,
u128: env_u128,
i8: env_i8,
i16: env_i16,
i32: env_i32,
i64: env_i64,
i128: env_i128,
f32: env_f32,
f64: env_f64,
dec: env_dec,
bool: env_bool,
string: env_string,
list: env_list,
record: env_record,
tuple: env_tuple,
},
]

format : {} -> EnvFormat
format = \{} -> @EnvFormat {}

decodeBytesToNum = \bytes, transformer ->
decode_bytes_to_num = \bytes, transformer ->
when Str.fromUtf8 bytes is
Ok s ->
when transformer s is
Expand All @@ -38,69 +38,71 @@ decodeBytesToNum = \bytes, transformer ->

Err _ -> { result: Err TooShort, rest: bytes }

envU8 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toU8
envU16 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toU16
envU32 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toU32
envU64 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toU64
envU128 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toU128
envI8 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toI8
envI16 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toI16
envI32 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toI32
envI64 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toI64
envI128 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toI128
envF32 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toF32
envF64 = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toF64
envDec = Decode.custom \bytes, @EnvFormat {} -> decodeBytesToNum bytes Str.toDec
envBool = Decode.custom \bytes, @EnvFormat {} ->
env_u8 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toU8
env_u16 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toU16
env_u32 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toU32
env_u64 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toU64
env_u128 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toU128
env_i8 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toI8
env_i16 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toI16
env_i32 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toI32
env_i64 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toI64
env_i128 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toI128
env_f32 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toF32
env_f64 = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toF64
env_dec = Decode.custom \bytes, @EnvFormat {} -> decode_bytes_to_num bytes Str.toDec

env_bool = Decode.custom \bytes, @EnvFormat {} ->
when Str.fromUtf8 bytes is
Ok "true" -> { result: Ok Bool.true, rest: [] }
Ok "false" -> { result: Ok Bool.false, rest: [] }
_ -> { result: Err TooShort, rest: bytes }
envString = Decode.custom \bytes, @EnvFormat {} ->

env_string = Decode.custom \bytes, @EnvFormat {} ->
when Str.fromUtf8 bytes is
Ok s -> { result: Ok s, rest: [] }
Err _ -> { result: Err TooShort, rest: bytes }

envList = \decodeElem -> Decode.custom \bytes, @EnvFormat {} ->
# Per our supported methods of decoding, this is either a list of strings or
# a list of numbers; in either case, the list of bytes must be Utf-8
# decodable. So just parse it as a list of strings and pass each chunk to
# the element decoder. By construction, our element decoders expect to parse
# a whole list of bytes anyway.
decodeElems = \allBytes, accum ->
{ toParse, remainder } =
when List.splitFirst allBytes (Num.toU8 ',') is
Ok { before, after } ->
{ toParse: before, remainder: Some after }
env_list = \decode_elem -> Decode.custom \bytes, @EnvFormat {} ->
# Per our supported methods of decoding, this is either a list of strings or
# a list of numbers; in either case, the list of bytes must be Utf-8
# decodable. So just parse it as a list of strings and pass each chunk to
# the element decoder. By construction, our element decoders expect to parse
# a whole list of bytes anyway.
decode_elems = \all_bytes, accum ->
{ to_parse, remainder } =
when List.splitFirst all_bytes (Num.toU8 ',') is
Ok { before, after } ->
{ to_parse: before, remainder: Some after }

Err NotFound ->
{ toParse: allBytes, remainder: None }
Err NotFound ->
{ to_parse: all_bytes, remainder: None }

when Decode.decodeWith toParse decodeElem (@EnvFormat {}) is
{ result, rest } ->
when result is
Ok val ->
when remainder is
Some restBytes -> decodeElems restBytes (List.append accum val)
None -> Done (List.append accum val)
when Decode.decodeWith to_parse decode_elem (@EnvFormat {}) is
{ result, rest } ->
when result is
Ok val ->
when remainder is
Some rest_bytes -> decode_elems rest_bytes (List.append accum val)
None -> Done (List.append accum val)

Err e -> Errored e rest
Err e -> Errored e rest

when decodeElems bytes [] is
Errored e rest -> { result: Err e, rest }
Done vals ->
{ result: Ok vals, rest: [] }
when decode_elems bytes [] is
Errored e rest -> { result: Err e, rest }
Done vals ->
{ result: Ok vals, rest: [] }

# TODO: we must currently annotate the arrows here so that the lambda sets are
# exercised, and the solver can find an ambient lambda set for the
# specialization.
envRecord : _, (_, _ -> [Keep (Decoder _ _), Skip]), (_, _ -> _) -> Decoder _ _
envRecord = \_initialState, _stepField, _finalizer -> Decode.custom \bytes, @EnvFormat {} ->
{ result: Err TooShort, rest: bytes }
env_record : _, (_, _ -> [Keep (Decoder _ _), Skip]), (_, _ -> _) -> Decoder _ _
env_record = \_initialState, _stepField, _finalizer -> Decode.custom \bytes, @EnvFormat {} ->
{ result: Err TooShort, rest: bytes }

# TODO: we must currently annotate the arrows here so that the lambda sets are
# exercised, and the solver can find an ambient lambda set for the
# specialization.
envTuple : _, (_, _ -> [Next (Decoder _ _), TooLong]), (_ -> _) -> Decoder _ _
envTuple = \_initialState, _stepElem, _finalizer -> Decode.custom \bytes, @EnvFormat {} ->
{ result: Err TooShort, rest: bytes }
env_tuple : _, (_, _ -> [Next (Decoder _ _), TooLong]), (_ -> _) -> Decoder _ _
env_tuple = \_initialState, _stepElem, _finalizer -> Decode.custom \bytes, @EnvFormat {} ->
{ result: Err TooShort, rest: bytes }
12 changes: 6 additions & 6 deletions platform/File.roc
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ Reader := { reader : Host.FileReader, path : Path }
##
## Use [read_utf8!] if you want to get the entire file contents at once.
open_reader! : Str => Result Reader [GetFileReadErr Path Err]
open_reader! = \pathStr ->
path = Path.from_str pathStr
open_reader! = \path_str ->
path = Path.from_str path_str

# 0 means with default capacity
Host.file_reader! (Str.toUtf8 pathStr) 0
Host.file_reader! (Str.toUtf8 path_str) 0
|> Result.mapErr \err -> GetFileReadErr path (InternalIOErr.handle_err err)
|> Result.map \reader -> @Reader { reader, path }

Expand All @@ -238,10 +238,10 @@ open_reader! = \pathStr ->
##
## Use [read_utf8!] if you want to get the entire file contents at once.
open_reader_with_capacity! : Str, U64 => Result Reader [GetFileReadErr Path Err]
open_reader_with_capacity! = \pathStr, capacity ->
path = Path.from_str pathStr
open_reader_with_capacity! = \path_str, capacity ->
path = Path.from_str path_str

Host.file_reader! (Str.toUtf8 pathStr) capacity
Host.file_reader! (Str.toUtf8 path_str) capacity
|> Result.mapErr \err -> GetFileReadErr path (InternalIOErr.handle_err err)
|> Result.map \reader -> @Reader { reader, path }

Expand Down
9 changes: 9 additions & 0 deletions platform/Http.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module [
default_request,
send!,
get!,
get_utf8!,
]

import InternalHttp
Expand Down Expand Up @@ -85,3 +86,11 @@ get! = \uri, fmt ->

Decode.fromBytes response.body fmt
|> Result.mapErr \_ -> HttpDecodingFailed

get_utf8! : Str => Result Str [BadBody Str]
Copy link
Collaborator

Choose a reason for hiding this comment

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

It doesn't seem like we have this error union anymore for errors, so we should maybe provide an error tag because it's lower cardinality? I'd vote [InvalidUtf8Body] instead.

get_utf8! = \uri ->
response = send! { default_request & uri }

response.body
|> Str.fromUtf8
|> Result.mapErr \_ -> BadBody "Invalid UTF-8"
Loading
Loading