rl_lang/docs/entries/tutorial/t2/
p_a5.rs1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2
3pub static ADV_CSV_IO: ConceptEntry = ConceptEntry {
4 name: "5. reading and writing CSV files",
5 summary: "reading and writing CSV files",
6 category: ConceptCategory::Tooling,
7 prerequisites: &[],
8 descriptions: &[
9 DescriptionEntry {
10 kind: DescriptionKind::Explanation,
11 title: None,
12 description: "you know the parser works on strings. now connect it to the filesystem. read_file from std::io gives you the file contents as a string - pass it straight to csv_parse",
13 examples: &[
14 "// csv.rl\nget read_file, write_file from std::io\nget path_exists from std::path\n\nfn csv_load(string path) -> arr[arr[string]] {\n if (!path_exists(path)) { return [] }\n dec string raw = read_file(path)\n return csv_parse(raw)\n}\n\nfn csv_save(string path, arr[arr[string]] rows) {\n write_file(path, csv_serialize(rows))\n}",
15 ],
16 expected_output: &[],
17 },
18 DescriptionEntry {
19 kind: DescriptionKind::Explanation,
20 title: None,
21 description: "path_exists is important here - if the file does not exist yet (first run of the program) you want to return an empty array, not crash",
22 examples: &[
23 "get path_exists from std::path\n\nfn csv_load(string path) -> arr[arr[string]] {\n if (!path_exists(path)) {\n return [] // first run, no file yet\n }\n return csv_parse(read_file(path))\n}",
24 ],
25 expected_output: &[],
26 },
27 DescriptionEntry {
28 kind: DescriptionKind::Explanation,
29 title: None,
30 description: "exercise: add csv_load and csv_save to csv.rl. in main.rl load tasks.csv, print how many rows it has, add a test row, save it back, then load again and verify the count increased\n\nexpected output (first run):\n loaded 0 rows\n saved 1 row\n reloaded 1 row",
31 examples: &[
32 "// main.rl\nget csv\nget arr_push, len from std::array\nget format from std::str\n\nCONST string TASKS_FILE = \"tasks.csv\"\n\nfn main() {\n dec arr[arr[string]] rows = csv_load(TASKS_FILE)\n println(format(\"loaded {} rows\", len(rows)))\n\n rows = arr_push(rows, [\"1\", \"pending\", \"1750000000\", \"test task\"])\n csv_save(TASKS_FILE, rows)\n println(format(\"saved {} row\", len(rows)))\n\n dec arr[arr[string]] reloaded = csv_load(TASKS_FILE)\n println(format(\"reloaded {} row\", len(reloaded)))\n}",
33 ],
34 expected_output: &[],
35 },
36 ],
37 pitfalls: &[],
38 related: &[],
39 related_stdlib: &[],
40 since: None,
41};