mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
Merge branch 'main' of github.com:roc-lang/roc into simplify_examples
This commit is contained in:
commit
eaacb86ad9
15 changed files with 271 additions and 170 deletions
22
Cargo.lock
generated
22
Cargo.lock
generated
|
@ -1336,19 +1336,6 @@ dependencies = [
|
|||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"humantime",
|
||||
"log",
|
||||
"regex",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.2.8"
|
||||
|
@ -1766,12 +1753,6 @@ version = "3.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "iced-x86"
|
||||
version = "1.17.0"
|
||||
|
@ -3057,7 +3038,7 @@ version = "1.0.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "588f6378e4dd99458b60ec275b4477add41ce4fa9f64dcba6f15adccb19b50d6"
|
||||
dependencies = [
|
||||
"env_logger 0.8.4",
|
||||
"env_logger",
|
||||
"log",
|
||||
"rand",
|
||||
]
|
||||
|
@ -3609,7 +3590,6 @@ dependencies = [
|
|||
"colored",
|
||||
"confy",
|
||||
"copypasta",
|
||||
"env_logger 0.9.0",
|
||||
"fs_extra",
|
||||
"futures",
|
||||
"glyph_brush",
|
||||
|
|
|
@ -66,6 +66,7 @@ mod cli_run {
|
|||
executable_filename: &'a str,
|
||||
stdin: &'a [&'a str],
|
||||
arguments: &'a [Arg<'a>],
|
||||
env: &'a [(&'a str, &'a str)],
|
||||
expected_ending: &'a str,
|
||||
use_valgrind: bool,
|
||||
}
|
||||
|
@ -115,12 +116,14 @@ mod cli_run {
|
|||
compile_out
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn check_output_with_stdin(
|
||||
file: &Path,
|
||||
stdin: &[&str],
|
||||
executable_filename: &str,
|
||||
flags: &[&str],
|
||||
roc_app_args: &[String],
|
||||
extra_env: &[(&str, &str)],
|
||||
expected_ending: &str,
|
||||
use_valgrind: bool,
|
||||
) {
|
||||
|
@ -193,16 +196,31 @@ mod cli_run {
|
|||
file.with_file_name(executable_filename).to_str().unwrap(),
|
||||
stdin.iter().copied(),
|
||||
roc_app_args,
|
||||
extra_env.iter().copied(),
|
||||
)
|
||||
}
|
||||
}
|
||||
CliMode::Roc => run_roc_on(file, flags.clone(), stdin, roc_app_args),
|
||||
CliMode::RocRun => run_roc_on(
|
||||
CliMode::Roc => {
|
||||
if !extra_env.is_empty() {
|
||||
// TODO: environment is not currently forwarded by Roc to the target
|
||||
// binary, so this would fail!
|
||||
continue;
|
||||
}
|
||||
run_roc_on(file, flags.clone(), stdin, roc_app_args)
|
||||
}
|
||||
CliMode::RocRun => {
|
||||
if !extra_env.is_empty() {
|
||||
// TODO: environment is not currently forwarded by Roc to the target
|
||||
// binary, so this would fail!
|
||||
continue;
|
||||
}
|
||||
run_roc_on(
|
||||
file,
|
||||
iter::once(CMD_RUN).chain(flags.clone()),
|
||||
stdin,
|
||||
roc_app_args,
|
||||
),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
if !&out.stdout.ends_with(expected_ending) {
|
||||
|
@ -216,12 +234,33 @@ mod cli_run {
|
|||
}
|
||||
}
|
||||
|
||||
// when you don't need args, stdin or extra_env
|
||||
fn test_roc_app_slim(
|
||||
dir_name: &str,
|
||||
roc_filename: &str,
|
||||
executable_filename: &str,
|
||||
expected_ending: &str,
|
||||
use_valgrind: bool,
|
||||
) {
|
||||
test_roc_app(
|
||||
dir_name,
|
||||
roc_filename,
|
||||
executable_filename,
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
expected_ending,
|
||||
use_valgrind
|
||||
)
|
||||
}
|
||||
|
||||
fn test_roc_app(
|
||||
dir_name: &str,
|
||||
roc_filename: &str,
|
||||
executable_filename: &str,
|
||||
stdin: &[&str],
|
||||
args: &[Arg],
|
||||
extra_env: &[(&str, &str)],
|
||||
expected_ending: &str,
|
||||
use_valgrind: bool,
|
||||
) {
|
||||
|
@ -288,6 +327,7 @@ mod cli_run {
|
|||
executable_filename,
|
||||
&custom_flags,
|
||||
&roc_app_args,
|
||||
extra_env,
|
||||
expected_ending,
|
||||
use_valgrind,
|
||||
);
|
||||
|
@ -302,6 +342,7 @@ mod cli_run {
|
|||
executable_filename,
|
||||
&custom_flags,
|
||||
&roc_app_args,
|
||||
extra_env,
|
||||
expected_ending,
|
||||
use_valgrind,
|
||||
);
|
||||
|
@ -315,6 +356,7 @@ mod cli_run {
|
|||
executable_filename,
|
||||
&[LINKER_FLAG, "legacy"],
|
||||
&roc_app_args,
|
||||
extra_env,
|
||||
expected_ending,
|
||||
use_valgrind,
|
||||
);
|
||||
|
@ -323,12 +365,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn hello_world() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"examples",
|
||||
"helloWorld.roc",
|
||||
"helloWorld",
|
||||
&[],
|
||||
&[],
|
||||
"Hello, World!\n",
|
||||
true,
|
||||
)
|
||||
|
@ -337,12 +377,10 @@ mod cli_run {
|
|||
#[test]
|
||||
// uses C platform
|
||||
fn platform_switching_main() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"crates/cli_testing_examples/platform-switching",
|
||||
"main.roc",
|
||||
"rocLovesPlatforms",
|
||||
&[],
|
||||
&[],
|
||||
"Which platform am I running on now?\n",
|
||||
true,
|
||||
)
|
||||
|
@ -354,12 +392,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn platform_switching_rust() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"crates/cli_testing_examples/platform-switching",
|
||||
"rocLovesRust.roc",
|
||||
"rocLovesRust",
|
||||
&[],
|
||||
&[],
|
||||
"Roc <3 Rust!\n",
|
||||
true,
|
||||
)
|
||||
|
@ -367,12 +403,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn platform_switching_zig() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"crates/cli_testing_examples/platform-switching",
|
||||
"rocLovesZig.roc",
|
||||
"rocLovesZig",
|
||||
&[],
|
||||
&[],
|
||||
"Roc <3 Zig!\n",
|
||||
true,
|
||||
)
|
||||
|
@ -380,12 +414,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn platform_switching_wasm() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"crates/cli_testing_examples/platform-switching",
|
||||
"rocLovesWebAssembly.roc",
|
||||
"rocLovesWebAssembly",
|
||||
&[],
|
||||
&[],
|
||||
"Roc <3 Web Assembly!\n",
|
||||
true,
|
||||
)
|
||||
|
@ -393,12 +425,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn platform_switching_swift() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"crates/cli_testing_examples/platform-switching",
|
||||
"rocLovesSwift.roc",
|
||||
"rocLovesSwift",
|
||||
&[],
|
||||
&[],
|
||||
"Roc <3 Swift!\n",
|
||||
true,
|
||||
)
|
||||
|
@ -406,12 +436,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn ruby_interop() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"examples/ruby-interop",
|
||||
"main.roc",
|
||||
"libhello",
|
||||
&[],
|
||||
&[],
|
||||
"",
|
||||
true,
|
||||
)
|
||||
|
@ -419,12 +447,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn fibonacci() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"crates/cli_testing_examples/algorithms",
|
||||
"fibonacci.roc",
|
||||
"fibonacci",
|
||||
&[],
|
||||
&[],
|
||||
"",
|
||||
true,
|
||||
)
|
||||
|
@ -432,12 +458,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn hello_gui() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"examples/gui",
|
||||
"hello.roc",
|
||||
"hello-gui",
|
||||
&[],
|
||||
&[],
|
||||
"",
|
||||
false,
|
||||
)
|
||||
|
@ -445,12 +469,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn breakout() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"examples/gui/breakout",
|
||||
"breakout.roc",
|
||||
"breakout",
|
||||
&[],
|
||||
&[],
|
||||
"",
|
||||
false,
|
||||
)
|
||||
|
@ -458,12 +480,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn quicksort() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"crates/cli_testing_examples/algorithms",
|
||||
"quicksort.roc",
|
||||
"quicksort",
|
||||
&[],
|
||||
&[],
|
||||
"[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2]\n",
|
||||
true,
|
||||
)
|
||||
|
@ -483,6 +503,7 @@ mod cli_run {
|
|||
Arg::PlainText("--num"),
|
||||
Arg::PlainText("81"),
|
||||
],
|
||||
&[],
|
||||
"4\n",
|
||||
false,
|
||||
)
|
||||
|
@ -496,6 +517,7 @@ mod cli_run {
|
|||
"effects",
|
||||
&["hi there!"],
|
||||
&[],
|
||||
&[],
|
||||
"hi there!\nIt is known\n",
|
||||
true,
|
||||
)
|
||||
|
@ -510,6 +532,7 @@ mod cli_run {
|
|||
"tui",
|
||||
&["foo\n"], // NOTE: adding more lines leads to memory leaks
|
||||
&[],
|
||||
&[],
|
||||
"Hello Worldfoo!\n",
|
||||
true,
|
||||
)
|
||||
|
@ -523,6 +546,7 @@ mod cli_run {
|
|||
"false",
|
||||
&[],
|
||||
&[Arg::ExamplePath("examples/hello.false")],
|
||||
&[],
|
||||
"Hello, World!\n",
|
||||
false,
|
||||
)
|
||||
|
@ -530,12 +554,10 @@ mod cli_run {
|
|||
|
||||
#[test]
|
||||
fn swift_ui() {
|
||||
test_roc_app(
|
||||
test_roc_app_slim(
|
||||
"examples/swiftui",
|
||||
"main.roc",
|
||||
"swiftui",
|
||||
&[],
|
||||
&[],
|
||||
"",
|
||||
false,
|
||||
)
|
||||
|
@ -549,11 +571,28 @@ mod cli_run {
|
|||
"static-site",
|
||||
&[],
|
||||
&[Arg::ExamplePath("input"), Arg::ExamplePath("output")],
|
||||
&[],
|
||||
"Processed 3 files with 3 successes and 0 errors\n",
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn with_env_vars() {
|
||||
test_roc_app(
|
||||
"examples/cli",
|
||||
"env.roc",
|
||||
"env",
|
||||
&[],
|
||||
&[],
|
||||
&[("EDITOR", "roc-editor"), ("SHLVL", "3"), ("LETTERS", "a,c,e,j")],
|
||||
"Your favorite editor is roc-editor!\n\
|
||||
Your current shell level is 3!\n\
|
||||
Your favorite letters are: a c e j\n",
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
// TODO not sure if this cfg should still be here: #[cfg(not(debug_assertions))]
|
||||
// this is for testing the benchmarks, to perform proper benchmarks see crates/cli/benches/README.md
|
||||
mod test_benchmarks {
|
||||
|
@ -623,6 +662,7 @@ mod cli_run {
|
|||
executable_filename,
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
expected_ending,
|
||||
use_valgrind,
|
||||
);
|
||||
|
@ -641,6 +681,7 @@ mod cli_run {
|
|||
executable_filename,
|
||||
&[PREBUILT_PLATFORM],
|
||||
&[],
|
||||
&[],
|
||||
expected_ending,
|
||||
use_valgrind,
|
||||
);
|
||||
|
@ -652,6 +693,7 @@ mod cli_run {
|
|||
executable_filename,
|
||||
&[PREBUILT_PLATFORM, OPTIMIZE_FLAG],
|
||||
&[],
|
||||
&[],
|
||||
expected_ending,
|
||||
use_valgrind,
|
||||
);
|
||||
|
@ -843,6 +885,7 @@ mod cli_run {
|
|||
"multi-dep-str",
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
"I am Dep2.str2\n",
|
||||
true,
|
||||
);
|
||||
|
@ -857,6 +900,7 @@ mod cli_run {
|
|||
"multi-dep-str",
|
||||
&[OPTIMIZE_FLAG],
|
||||
&[],
|
||||
&[],
|
||||
"I am Dep2.str2\n",
|
||||
true,
|
||||
);
|
||||
|
@ -871,6 +915,7 @@ mod cli_run {
|
|||
"multi-dep-thunk",
|
||||
&[],
|
||||
&[],
|
||||
&[],
|
||||
"I am Dep2.value2\n",
|
||||
true,
|
||||
);
|
||||
|
@ -885,6 +930,7 @@ mod cli_run {
|
|||
"multi-dep-thunk",
|
||||
&[OPTIMIZE_FLAG],
|
||||
&[],
|
||||
&[],
|
||||
"I am Dep2.value2\n",
|
||||
true,
|
||||
);
|
||||
|
|
|
@ -48,12 +48,12 @@ fn check_cmd_output(
|
|||
let out = if cmd_str.contains("cfold") {
|
||||
let child = thread::Builder::new()
|
||||
.stack_size(CFOLD_STACK_SIZE)
|
||||
.spawn(move || run_cmd(&cmd_str, [stdin_str], &[]))
|
||||
.spawn(move || run_cmd(&cmd_str, [stdin_str], &[], []))
|
||||
.unwrap();
|
||||
|
||||
child.join().unwrap()
|
||||
} else {
|
||||
run_cmd(&cmd_str, [stdin_str], &[])
|
||||
run_cmd(&cmd_str, [stdin_str], &[], [])
|
||||
};
|
||||
|
||||
if !&out.stdout.ends_with(expected_ending) {
|
||||
|
@ -96,13 +96,14 @@ fn bench_cmd<T: Measurement>(
|
|||
|
||||
if let Some(bench_group) = bench_group_opt {
|
||||
bench_group.bench_function(&format!("Benchmarking {:?}", executable_filename), |b| {
|
||||
b.iter(|| run_cmd(black_box(&cmd_str), black_box([stdin_str]), &[]))
|
||||
b.iter(|| run_cmd(black_box(&cmd_str), black_box([stdin_str]), &[], []))
|
||||
});
|
||||
} else {
|
||||
run_cmd(
|
||||
black_box(file.with_file_name(executable_filename).to_str().unwrap()),
|
||||
black_box([stdin_str]),
|
||||
&[],
|
||||
[],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -161,10 +161,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
pub fn run_cmd<'a, I: IntoIterator<Item = &'a str>>(
|
||||
pub fn run_cmd<'a, I: IntoIterator<Item = &'a str>, E: IntoIterator<Item = (&'a str, &'a str)>>(
|
||||
cmd_name: &str,
|
||||
stdin_vals: I,
|
||||
args: &[String],
|
||||
env: E,
|
||||
) -> Out {
|
||||
let mut cmd = Command::new(cmd_name);
|
||||
|
||||
|
@ -172,6 +173,10 @@ pub fn run_cmd<'a, I: IntoIterator<Item = &'a str>>(
|
|||
cmd.arg(arg);
|
||||
}
|
||||
|
||||
for (env, val) in env.into_iter() {
|
||||
cmd.env(env, val);
|
||||
}
|
||||
|
||||
let mut child = cmd
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
|
|
|
@ -3732,7 +3732,13 @@ fn send_header<'a>(
|
|||
// Also build a list of imported_values_to_expose (like `bar` above.)
|
||||
for (qualified_module_name, exposed_idents, region) in imported.into_iter() {
|
||||
let cloned_module_name = qualified_module_name.module.clone();
|
||||
let pq_module_name = match qualified_module_name.opt_package {
|
||||
let pq_module_name = if qualified_module_name.is_builtin() {
|
||||
// If this is a builtin, it must be unqualified, and we should *never* prefix it
|
||||
// with the package shorthand! The user intended to import the module as-is here.
|
||||
debug_assert!(qualified_module_name.opt_package.is_none());
|
||||
PQModuleName::Unqualified(qualified_module_name.module)
|
||||
} else {
|
||||
match qualified_module_name.opt_package {
|
||||
None => match opt_shorthand {
|
||||
Some(shorthand) => {
|
||||
PQModuleName::Qualified(shorthand, qualified_module_name.module)
|
||||
|
@ -3740,6 +3746,7 @@ fn send_header<'a>(
|
|||
None => PQModuleName::Unqualified(qualified_module_name.module),
|
||||
},
|
||||
Some(package) => PQModuleName::Qualified(package, cloned_module_name),
|
||||
}
|
||||
};
|
||||
|
||||
let module_id = module_ids.get_or_insert(&pq_module_name);
|
||||
|
|
|
@ -42,7 +42,6 @@ wgpu = "0.12.0"
|
|||
wgpu_glyph = "0.16.0"
|
||||
glyph_brush = "0.7.5"
|
||||
log = "0.4.14"
|
||||
env_logger = "0.9.0"
|
||||
futures = "0.3.24"
|
||||
cgmath = "0.18.0"
|
||||
snafu = { version = "0.7.1", features = ["backtraces"] }
|
||||
|
|
|
@ -59,8 +59,6 @@ pub fn launch(project_dir_path_opt: Option<&Path>) -> io::Result<()> {
|
|||
}
|
||||
|
||||
fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
// Open window and create a surface
|
||||
let mut event_loop = winit::event_loop::EventLoop::new();
|
||||
|
||||
|
|
1
examples/cli/.gitignore
vendored
1
examples/cli/.gitignore
vendored
|
@ -6,3 +6,4 @@ form
|
|||
tui
|
||||
http-get
|
||||
file-io
|
||||
env
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
interface Env
|
||||
exposes [cwd, dict, var]
|
||||
imports [Task.{ Task }, Path.{ Path }, InternalPath, Effect, InternalTask]
|
||||
exposes [cwd, dict, var, decode] imports [Task.{ Task }, Path.{ Path }, InternalPath, Effect, InternalTask, EnvDecoding]
|
||||
|
||||
## Reads the [current working directory](https://en.wikipedia.org/wiki/Working_directory)
|
||||
## from the environment. File operations on relative [Path]s are relative to this directory.
|
||||
|
@ -33,31 +32,40 @@ var = \name ->
|
|||
|> Effect.map (\result -> Result.mapErr result \{} -> VarNotFound)
|
||||
|> InternalTask.fromEffect
|
||||
|
||||
# ## Reads the given environment variable and attempts to decode it.
|
||||
# ##
|
||||
# ## The type being decoded into will be determined by type inference. For example,
|
||||
# ## if this ends up being used like a `Task U16 …` then the environment variable
|
||||
# ## will be decoded as a string representation of a `U16`.
|
||||
# ##
|
||||
# ## getU16Var : Str -> Task U16 [VarNotFound, DecodeErr DecodeError]* [Env]*
|
||||
# ## getU16Var = \var -> Env.decode var
|
||||
# ## # If the environment contains a variable NUM_THINGS=123, then calling
|
||||
# ## # (getU16Var "NUM_THINGS") would return a task which succeeds with the U16 number 123.
|
||||
# ## #
|
||||
# ## # However, if the NUM_THINGS environment variable was set to 1234567, then
|
||||
# ## # (getU16Var "NUM_THINGS") would fail because that number is too big to fit in a U16.
|
||||
# ##
|
||||
# ## Supported types:
|
||||
# ## - strings
|
||||
# ## - numbers, as long as they contain only numeric digits, up to one `.`, and an optional `-` at the front for negative numbers
|
||||
# ## - comma-separated lists (of either strings or numbers), as long as there are no spaces after the commas
|
||||
# ##
|
||||
# ## Trying to decode into any other types will always fail with a `DecodeErr`.
|
||||
# decode : Str -> Task val [VarNotFound, DecodeErr DecodeError]* [Env]*
|
||||
# | val has Decode
|
||||
# decode = \var ->
|
||||
# Effect.envVar var
|
||||
# |> InternalTask.fromEffect
|
||||
## Reads the given environment variable and attempts to decode it.
|
||||
##
|
||||
## The type being decoded into will be determined by type inference. For example,
|
||||
## if this ends up being used like a `Task U16 …` then the environment variable
|
||||
## will be decoded as a string representation of a `U16`.
|
||||
##
|
||||
## getU16Var : Str -> Task U16 [VarNotFound, DecodeErr DecodeError]* [Env]*
|
||||
## getU16Var = \var -> Env.decode var
|
||||
## # If the environment contains a variable NUM_THINGS=123, then calling
|
||||
## # (getU16Var "NUM_THINGS") would return a task which succeeds with the U16 number 123.
|
||||
## #
|
||||
## # However, if the NUM_THINGS environment variable was set to 1234567, then
|
||||
## # (getU16Var "NUM_THINGS") would fail because that number is too big to fit in a U16.
|
||||
##
|
||||
## Supported types:
|
||||
## - strings
|
||||
## - numbers, as long as they contain only numeric digits, up to one `.`, and an optional `-` at the front for negative numbers
|
||||
## - comma-separated lists (of either strings or numbers), as long as there are no spaces after the commas
|
||||
##
|
||||
## Trying to decode into any other types will always fail with a `DecodeErr`.
|
||||
decode : Str -> Task val [VarNotFound, DecodeErr DecodeError]* [Env]* | val has Decoding
|
||||
decode = \name ->
|
||||
Effect.envVar name
|
||||
|> Effect.map
|
||||
(
|
||||
\result ->
|
||||
result
|
||||
|> Result.mapErr (\{} -> VarNotFound)
|
||||
|> Result.try
|
||||
(\varStr ->
|
||||
Decode.fromBytes (Str.toUtf8 varStr) (EnvDecoding.format {})
|
||||
|> Result.mapErr (\_ -> DecodeErr TooShort)))
|
||||
|> InternalTask.fromEffect
|
||||
|
||||
## Reads all the process's environment variables into a [Dict].
|
||||
##
|
||||
## If any key or value contains invalid Unicode, the [Unicode replacement character](https://unicode.org/glossary/#replacement_character)
|
||||
|
|
95
examples/cli/cli-platform/EnvDecoding.roc
Normal file
95
examples/cli/cli-platform/EnvDecoding.roc
Normal file
|
@ -0,0 +1,95 @@
|
|||
interface EnvDecoding exposes [EnvFormat, format] imports []
|
||||
|
||||
EnvFormat := {} has [
|
||||
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,
|
||||
},
|
||||
]
|
||||
|
||||
format : {} -> EnvFormat
|
||||
format = \{} -> @EnvFormat {}
|
||||
|
||||
decodeBytesToNum = \bytes, transformer ->
|
||||
when Str.fromUtf8 bytes is
|
||||
Ok s ->
|
||||
when transformer s is
|
||||
Ok n -> { result: Ok n, rest: [] }
|
||||
Err _ -> { result: Err TooShort, rest: bytes }
|
||||
|
||||
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 {} ->
|
||||
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 {} ->
|
||||
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 }
|
||||
|
||||
Err NotFound ->
|
||||
{ toParse: allBytes, 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)
|
||||
|
||||
Err e -> Errored e rest
|
||||
|
||||
when decodeElems 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 }
|
25
examples/cli/env.roc
Normal file
25
examples/cli/env.roc
Normal file
|
@ -0,0 +1,25 @@
|
|||
app "env"
|
||||
packages { pf: "cli-platform/main.roc" }
|
||||
imports [pf.Stdout, pf.Env, pf.Task, pf.Program.{ Program }]
|
||||
provides [main] to pf
|
||||
|
||||
main : Program
|
||||
main =
|
||||
Env.decode "EDITOR"
|
||||
|> Task.await (\editor -> Stdout.line "Your favorite editor is \(editor)!")
|
||||
|> Task.await (\{} -> Env.decode "SHLVL")
|
||||
|> Task.await
|
||||
(\lvl ->
|
||||
when lvl is
|
||||
1u8 -> Stdout.line "You're running this in a root shell!"
|
||||
n ->
|
||||
lvlStr = Num.toStr n
|
||||
|
||||
Stdout.line "Your current shell level is \(lvlStr)!")
|
||||
|> Task.await (\{} -> Env.decode "LETTERS")
|
||||
|> Task.await
|
||||
(\letters ->
|
||||
joinedLetters = Str.joinWith letters " "
|
||||
|
||||
Stdout.line "Your favorite letters are: \(joinedLetters)")
|
||||
|> Program.quick
|
31
examples/gui/breakout/platform/Cargo.lock
generated
31
examples/gui/breakout/platform/Cargo.lock
generated
|
@ -44,15 +44,6 @@ dependencies = [
|
|||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "alsa"
|
||||
version = "0.5.0"
|
||||
|
@ -752,19 +743,6 @@ version = "1.6.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"humantime",
|
||||
"log",
|
||||
"regex",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fake-simd"
|
||||
version = "0.1.2"
|
||||
|
@ -1061,7 +1039,6 @@ dependencies = [
|
|||
"colored",
|
||||
"confy",
|
||||
"copypasta",
|
||||
"env_logger",
|
||||
"fs_extra",
|
||||
"futures",
|
||||
"glyph_brush",
|
||||
|
@ -1088,12 +1065,6 @@ version = "3.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "ident_case"
|
||||
version = "1.0.1"
|
||||
|
@ -2071,8 +2042,6 @@ version = "1.5.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
|
|
|
@ -25,7 +25,6 @@ wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "0545e36" }
|
|||
wgpu_glyph = { git = "https://github.com/Anton-4/wgpu_glyph", rev = "257d109" }
|
||||
glyph_brush = "0.7.2"
|
||||
log = "0.4.14"
|
||||
env_logger = "0.9.0"
|
||||
futures = "0.3.17"
|
||||
cgmath = "0.18.0"
|
||||
snafu = { version = "0.6.10", features = ["backtraces"] }
|
||||
|
|
31
examples/gui/platform/Cargo.lock
generated
31
examples/gui/platform/Cargo.lock
generated
|
@ -44,15 +44,6 @@ dependencies = [
|
|||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "alsa"
|
||||
version = "0.5.0"
|
||||
|
@ -752,19 +743,6 @@ version = "1.6.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"humantime",
|
||||
"log",
|
||||
"regex",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fake-simd"
|
||||
version = "0.1.2"
|
||||
|
@ -1061,7 +1039,6 @@ dependencies = [
|
|||
"colored",
|
||||
"confy",
|
||||
"copypasta",
|
||||
"env_logger",
|
||||
"fs_extra",
|
||||
"futures",
|
||||
"glyph_brush",
|
||||
|
@ -1088,12 +1065,6 @@ version = "3.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a164bb2ceaeff4f42542bdb847c41517c78a60f5649671b2a07312b6e117549"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||
|
||||
[[package]]
|
||||
name = "ident_case"
|
||||
version = "1.0.1"
|
||||
|
@ -2071,8 +2042,6 @@ version = "1.5.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "0545e36" }
|
|||
wgpu_glyph = { git = "https://github.com/Anton-4/wgpu_glyph", rev = "257d109" }
|
||||
glyph_brush = "0.7.2"
|
||||
log = "0.4.14"
|
||||
env_logger = "0.9.0"
|
||||
futures = "0.3.17"
|
||||
cgmath = "0.18.0"
|
||||
snafu = { version = "0.6.10", features = ["backtraces"] }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue