-
Notifications
You must be signed in to change notification settings - Fork 32
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
base: main
Are you sure you want to change the base?
Conversation
// TODO confirm this is ok... feels dangerous | ||
let os_str = | ||
std::ffi::OsString::from_encoded_bytes_unchecked(c_str.to_bytes().to_owned()); |
There was a problem hiding this comment.
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 🙏
There was a problem hiding this comment.
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.
@@ -3,7 +3,7 @@ app [main!] { pf: platform "../platform/main.roc" } | |||
import pf.Stdout | |||
import pf.Cmd | |||
|
|||
main! = \{} -> | |||
main! = \_ -> |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from_os_raw : InternalArg.ArgToAndFromHost -> Arg | |
## Wrap a raw, OS-aware numeric list into an [Arg]. | |
from_os_raw : InternalArg.ArgToAndFromHost -> Arg |
## 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)] |
There was a problem hiding this comment.
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".
## 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)] |
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## 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 |
There was a problem hiding this comment.
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.
// TODO confirm this is ok... feels dangerous | ||
let os_str = | ||
std::ffi::OsString::from_encoded_bytes_unchecked(c_str.to_bytes().to_owned()); |
There was a problem hiding this comment.
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.
No description provided.