1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
// usage calls // TODO: Move struct and type definitions to own file/module // Type definitions pub mod types { use std::fmt; /// List of classes in planner pub struct ClassList(pub Vec<Class>); // Let us print out Tasklist nicely impl fmt::Display for ClassList { /* * This implementation of fmt is cool because we are chaning the child types fmt:Display * implementation through our iterator, meaning we only have to change the root Display once * and it will automatically change it up here. */ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Iterate through list for class in &self.0 { // Write to formater using class implementation of fmt() writeln!(f, "{}", class)?; } // Return okay Ok(()) } } /// List of tasks in Class // This has to be a struct to implement Display pub struct TaskList(pub Vec<Task>); // Let us print out Tasklist nicely impl fmt::Display for TaskList { /* * This implementation of fmt is cool because we are chaning the child types fmt:Display * implementation through our iterator, meaning we only have to change the root Display once * and it will automatically change it up here. */ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Iterate through list for task in &self.0 { // Write to formater using task implementation of fmt() writeln!(f, "{}", task)?; } // Return okay Ok(()) } } // Struct definitions /// Main planner structure, contains class list and subtypes pub struct Planner { /// Name of planner (i.e. "School") pub name: String, /// default Calendar for writing pub calendar: String, /// List of classes for a planner pub class_list: ClassList } impl fmt::Display for Planner { // Printable classes fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Planner: {}\nCalendar: {}\nClasses:\n{}", self.name, self.calendar, self.class_list) } } /// Class structure, contains list of assignemnts pub struct Class { /// Name of class (i.e "La", "Math", etc.) pub name: String, /// List of task in class, can be HW or inclass work, pub tasks: TaskList, } impl fmt::Display for Class { // Printable classes fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Class: {}\nAssignments:\n{}", self.name, self.tasks) } } /// Task structure, a given assigment for a Class pub struct Task { /// Name of task pub name: String, /// Due date of task (i.e. "1/3/2019 8:45 AM") pub due_date: usize, /// Priority of task pub priority: usize, /// Extra information on task pub notes: String } // Implement Display for Task (fancy printing) impl fmt::Display for Task { // Implement Display so we can use println! macros fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // Write out our task to the formatter write!(f, "{} | Due: {} | Priority: {}", self.name, self.due_date, self.priority) } } } pub mod prelude { pub use crate::types::*; }