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

Add new custom type Arg, update API to main! : List Arg => Result {} [Exit I32 Str]_ #293

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

lukewilliamboswell
Copy link
Collaborator

No description provided.

Comment on lines +15 to +17
// TODO confirm this is ok... feels dangerous
let os_str =
std::ffi::OsString::from_encoded_bytes_unchecked(c_str.to_bytes().to_owned());
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Please read the docs and give me an opinion here 🙏

Copy link
Collaborator

Choose a reason for hiding this comment

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

The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8. By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit ASCII.

So I think this is incorrect, but it also seems unnecessary. This seems like we could just pass the &[u8] to ArgToAndFromHost::from(arg) and handle it there. For Unix it's handled, and for Windows we'll need to do something special anyway.

@lukewilliamboswell lukewilliamboswell marked this pull request as ready for review December 20, 2024 04:01
@@ -3,7 +3,7 @@ app [main!] { pf: platform "../platform/main.roc" }
import pf.Stdout
import pf.Cmd

main! = \{} ->
main! = \_ ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this was personal code, then ignoring the args entirely would probably be expected, but we should default to something more instructive in these examples, meaning I vote for _args instead of just _ for all these examples:

  • It tells readers what is being ignored, so they know that main! gets a list of arguments even if they only read the one example
  • It implies that naming ignored variables is good practice (which it generally is) to give context to readers

Unix -> Unix inner.unix
Windows -> Windows inner.windows

from_os_raw : InternalArg.ArgToAndFromHost -> Arg
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
from_os_raw : InternalArg.ArgToAndFromHost -> Arg
## Wrap a raw, OS-aware numeric list into an [Arg].
from_os_raw : InternalArg.ArgToAndFromHost -> Arg

Comment on lines +15 to +17
## Unwrap the raw bytes for decoding, typically this is
## consumed by a package and not an end user
to_os_raw : Arg -> [Unix (List U8), Windows (List U16)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

A U16 isn't considered a byte, so we can't call the data type "bytes".

Suggested change
## Unwrap the raw bytes for decoding, typically this is
## consumed by a package and not an end user
to_os_raw : Arg -> [Unix (List U8), Windows (List U16)]
## Unwrap an [Arg] into a raw, OS-aware numeric list.
##
## This is a good way to pass [Arg]s to Roc packages.
to_os_raw : Arg -> [Unix (List U8), Windows (List U16)]

Comment on lines +10 to +13
# os_str's are not necessarily valid utf-8 or utf-16
# so we store as raw bytes internally to avoid
# common mistakes
Arg := InternalArg.ArgToAndFromHost
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# os_str's are not necessarily valid utf-8 or utf-16
# so we store as raw bytes internally to avoid
# common mistakes
Arg := InternalArg.ArgToAndFromHost
## An OS-aware representation of a command-line argument.
##
## Though we tend to think of args as Unicode strings, most operating systems
## represent command-line arguments as lists of bytes that aren't necessarily
## UTF-8 encoded. Windows doesn't even use bytes, but U16s.
##
## Most of the time, you will pass these to packages and they will handle the
## encoding for you, but for quick-and-dirty code you can use [display] to
## convert these to [Str] in a lossy way.
Arg := InternalArg.ArgToAndFromHost

from_os_raw : InternalArg.ArgToAndFromHost -> Arg
from_os_raw = \raw -> @Arg raw

## Convert an Arg to a `Str` for display purposes
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
## Convert an Arg to a `Str` for display purposes
## Convert an Arg to a `Str` for display purposes.

Unix -> Unix inner.unix
Windows -> Windows inner.windows

from_os_raw : InternalArg.ArgToAndFromHost -> Arg
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, this should probably take [Unix (List U8), Windows (List U16)] to not expose internals.

Comment on lines +15 to +17
// TODO confirm this is ok... feels dangerous
let os_str =
std::ffi::OsString::from_encoded_bytes_unchecked(c_str.to_bytes().to_owned());
Copy link
Collaborator

Choose a reason for hiding this comment

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

The byte encoding is an unspecified, platform-specific, self-synchronizing superset of UTF-8. By being a self-synchronizing superset of UTF-8, this encoding is also a superset of 7-bit ASCII.

So I think this is incorrect, but it also seems unnecessary. This seems like we could just pass the &[u8] to ArgToAndFromHost::from(arg) and handle it there. For Unix it's handled, and for Windows we'll need to do something special anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants