-
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?
Changes from all commits
c6cbda1
57a82fe
f0628cd
d3eceeb
d66414e
c84593f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use roc_std::{roc_refcounted_noop_impl, RocList, RocRefcounted}; | ||
use std::ffi::OsString; | ||
|
||
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
#[repr(C)] | ||
pub struct ArgToAndFromHost { | ||
pub unix: RocList<u8>, | ||
pub windows: RocList<u16>, | ||
pub tag: ArgTag, | ||
} | ||
|
||
impl From<OsString> for ArgToAndFromHost { | ||
#[cfg(target_os = "macos")] | ||
fn from(os_str: OsString) -> Self { | ||
ArgToAndFromHost { | ||
unix: RocList::from_slice(os_str.as_encoded_bytes()), | ||
windows: RocList::empty(), | ||
tag: ArgTag::Unix, | ||
} | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn from(os_str: OsString) -> Self { | ||
ArgToAndFromHost { | ||
unix: RocList::from_slice(os_str.as_encoded_bytes()), | ||
windows: RocList::empty(), | ||
tag: ArgTag::Unix, | ||
} | ||
} | ||
|
||
#[cfg(target_os = "windows")] | ||
fn from(os_str: OsString) -> Self { | ||
todo!() | ||
// use something like | ||
// https://docs.rs/widestring/latest/widestring/ | ||
// to support Windows | ||
} | ||
} | ||
|
||
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)] | ||
#[repr(u8)] | ||
pub enum ArgTag { | ||
Unix = 0, | ||
Windows = 1, | ||
} | ||
|
||
impl core::fmt::Debug for ArgTag { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
Self::Unix => f.write_str("ArgTag::Unix"), | ||
Self::Windows => f.write_str("ArgTag::Windows"), | ||
} | ||
} | ||
} | ||
|
||
roc_refcounted_noop_impl!(ArgTag); | ||
|
||
impl roc_std::RocRefcounted for ArgToAndFromHost { | ||
fn inc(&mut self) { | ||
self.unix.inc(); | ||
self.windows.inc(); | ||
} | ||
fn dec(&mut self) { | ||
self.unix.dec(); | ||
self.windows.dec(); | ||
} | ||
fn is_refcounted() -> bool { | ||
true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ path = "src/main.rs" | |
[dependencies] | ||
roc_std.workspace = true | ||
roc_host.workspace = true | ||
roc_env.workspace = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
use roc_std::{ReadOnlyRocList, ReadOnlyRocStr, RocList, RocStr}; | ||
use std::borrow::Borrow; | ||
use roc_env::arg::ArgToAndFromHost; | ||
|
||
fn main() { | ||
let mut args: RocList<ReadOnlyRocStr> = std::env::args_os() | ||
.map(|os_str| { | ||
let roc_str = RocStr::from(os_str.to_string_lossy().borrow()); | ||
ReadOnlyRocStr::from(roc_str) | ||
}) | ||
.collect(); | ||
unsafe { args.set_readonly() }; | ||
std::process::exit(roc_host::rust_main(ReadOnlyRocList::from(args))); | ||
let args = std::env::args_os().map(ArgToAndFromHost::from).collect(); | ||
|
||
let exit_code = roc_host::rust_main(args); | ||
|
||
std::process::exit(exit_code); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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
|
||
try status_example! {} | ||
|
||
try output_example! {} | ||
|
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.
So I think this is incorrect, but it also seems unnecessary. This seems like we could just pass the
&[u8]
toArgToAndFromHost::from(arg)
and handle it there. For Unix it's handled, and for Windows we'll need to do something special anyway.