aboutsummaryrefslogtreecommitdiff
path: root/examples/full-todolist
diff options
context:
space:
mode:
Diffstat (limited to 'examples/full-todolist')
-rw-r--r--examples/full-todolist/src/help.rs99
-rw-r--r--examples/full-todolist/src/main.rs40
-rw-r--r--examples/full-todolist/src/todolist.rs22
3 files changed, 91 insertions, 70 deletions
diff --git a/examples/full-todolist/src/help.rs b/examples/full-todolist/src/help.rs
index ab33176..2f8228a 100644
--- a/examples/full-todolist/src/help.rs
+++ b/examples/full-todolist/src/help.rs
@@ -1,15 +1,16 @@
//! This module provides help information for the `todolist` command line program
-use mingling::macros::{help, r_println};
-
use crate::{EntryAdd, EntryClean, EntryComplete, EntryList, ErrorDispatcherNotFound};
+use mingling::{RenderResult, macros::help};
+use std::io::Write;
+/// Shows the global help message.
#[help]
-pub fn help_global(_p: ErrorDispatcherNotFound) {
- r_println!(
- "{}",
- r"
-Usage: todolist [command] [args]
+pub fn help_global(_p: ErrorDispatcherNotFound) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(
+ render_result,
+ r"Usage: todolist [command] [args]
Commands:
add -- Add a new task
@@ -20,74 +21,78 @@ Commands:
Args:
-h, --help -- Show this help message
-V, --version -- Show the version
- -A, --all -- All tasks (Clean all / List all)
- "
- .trim()
- );
+ -A, --all -- All tasks (Clean all / List all)"
+ )
+ .ok();
+ render_result
}
+/// Shows help for the `add` command.
#[help]
-pub fn help_add(_p: EntryAdd) {
- r_println!(
- "{}",
- r"
-Usage: todolist add [task description]
+pub fn help_add(_p: EntryAdd) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(
+ render_result,
+ r"Usage: todolist add [task description]
Add a new task to the todo list.
Example:
todolist add 'Buy groceries'
- todolist add 'Finish Rust project'
- "
- .trim()
- );
+ todolist add 'Finish Rust project'"
+ )
+ .ok();
+ render_result
}
+/// Shows help for the `list` command.
#[help]
-pub fn help_list(_p: EntryList) {
- r_println!(
- "{}",
- r"
-Usage: todolist list
+pub fn help_list(_p: EntryList) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(
+ render_result,
+ r"Usage: todolist list
List all tasks.
Example:
- todolist list
- "
- .trim()
- );
+ todolist list"
+ )
+ .ok();
+ render_result
}
+/// Shows help for the `complete` command.
#[help]
-pub fn help_complete(_p: EntryComplete) {
- r_println!(
- "{}",
- r"
-Usage: todolist complete [task_id]
+pub fn help_complete(_p: EntryComplete) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(
+ render_result,
+ r"Usage: todolist complete [task_id]
Mark a task as complete by its ID.
Example:
todolist complete 1
- todolist complete 3
- "
- .trim()
- );
+ todolist complete 3"
+ )
+ .ok();
+ render_result
}
+/// Shows help for the `clean` command.
#[help]
-pub fn help_clean(_p: EntryClean) {
- r_println!(
- "{}",
- r"
-Usage: todolist clean
+pub fn help_clean(_p: EntryClean) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(
+ render_result,
+ r"Usage: todolist clean
Remove all completed tasks from the list.
Example:
- todolist clean
- "
- .trim()
- );
+ todolist clean"
+ )
+ .ok();
+ render_result
}
diff --git a/examples/full-todolist/src/main.rs b/examples/full-todolist/src/main.rs
index e4c5aa6..0748832 100644
--- a/examples/full-todolist/src/main.rs
+++ b/examples/full-todolist/src/main.rs
@@ -7,12 +7,13 @@
//! > This is truly a cliché example, as common as `Hello World`!
use mingling::{
+ LazyInit, LazyRes,
macros::route,
prelude::*,
res::ResExitCode,
- setup::{ExitCodeSetup, StructuralRendererSetup, HelpFlagSetup},
- LazyInit, LazyRes,
+ setup::{ExitCodeSetup, HelpFlagSetup, StructuralRendererSetup},
};
+use std::io::Write;
mod help;
pub use help::*;
@@ -160,38 +161,47 @@ fn handle_clean(
}
}
+/// Renders error when no task description is provided.
#[renderer]
-fn render_error_no_task_description_provided(
+pub fn render_error_no_task_description_provided(
_err: ErrorNoTaskDescriptionProvided,
// ExitCode
ec: &mut ResExitCode,
-) {
- r_println!("No task description provided!");
- r_println!("");
- r_println!("Use `todolist add <desc>` to add a task");
+) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(render_result, "No task description provided!").ok();
+ writeln!(render_result).ok();
+ writeln!(render_result, "Use `todolist add <desc>` to add a task").ok();
ec.exit_code = 1;
+ render_result
}
+/// Renders error when no index is provided.
#[renderer]
-fn render_error_no_index_provided(
+pub fn render_error_no_index_provided(
_err: ErrorNoIndexProvided,
// ExitCode
ec: &mut ResExitCode,
-) {
- r_println!("No index provided!");
- r_println!("");
- r_println!("Use `todolist list` to query indexes");
+) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(render_result, "No index provided!").ok();
+ writeln!(render_result).ok();
+ writeln!(render_result, "Use `todolist list` to query indexes").ok();
ec.exit_code = 2;
+ render_result
}
+/// Renders error when index is out of bounds.
#[renderer]
-fn render_error_index_out_of_bounds(
+pub fn render_error_index_out_of_bounds(
_err: ErrorIndexOutOfBounds,
// ExitCode
ec: &mut ResExitCode,
-) {
- r_println!("Index out of bounds!");
+) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(render_result, "Index out of bounds!").ok();
ec.exit_code = 3;
+ render_result
}
gen_program!();
diff --git a/examples/full-todolist/src/todolist.rs b/examples/full-todolist/src/todolist.rs
index e447f5d..71338c3 100644
--- a/examples/full-todolist/src/todolist.rs
+++ b/examples/full-todolist/src/todolist.rs
@@ -1,10 +1,8 @@
//! Data structures, read and write logic for the todo list
-use mingling::{
- macros::{r_println, renderer},
- Groupped,
-};
+use mingling::{Groupped, RenderResult, macros::renderer};
use serde::{Deserialize, Serialize};
+use std::io::Write;
use std::{env::current_dir, path::PathBuf};
use crate::ResProgramFlags;
@@ -37,22 +35,30 @@ pub fn read_todo_list() -> ResTodoList {
serde_json::from_reader(reader).unwrap_or_default()
}
+/// Renders the todo list.
#[renderer]
-pub fn render_res_todo_list(todo_list: ResTodoList, program_flags: &ResProgramFlags) {
+pub fn render_res_todo_list(
+ todo_list: ResTodoList,
+ program_flags: &ResProgramFlags,
+) -> RenderResult {
+ let mut render_result = RenderResult::new();
let mut idx = 0;
- r_println!("TODO: ");
+ writeln!(render_result, "TODO: ").ok();
for item in &todo_list.items {
if item.completed && !program_flags.all {
idx += 1;
continue;
}
- r_println!(
+ writeln!(
+ render_result,
" {idx}. [{}] - \"{}\"",
if item.completed { "x" } else { " " },
item.item
- );
+ )
+ .ok();
idx += 1;
}
+ render_result
}
pub fn write_todo_list(todo_list: ResTodoList) {