tada 0.1.2

a todo list manager
Documentation
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
//! Types related to individual tasks.
//!
//! # Examples
//!
//! ```
//! use chrono::NaiveDate;
//! use tada::item::{Importance, Item, TshirtSize, Urgency};
//!
//! let mut i = Item::parse("(A) clean my @home @L");
//!
//! assert_eq!(Some(Importance::A), i.importance());
//! assert_eq!("clean my @home @L", i.description());
//! assert!(i.has_context("home"));
//! assert!(i.has_context("l"));
//! assert_eq!(Some(TshirtSize::Large), i.tshirt_size());
//!
//! i.set_completion(true);
//! i.set_completion_date(NaiveDate::from_ymd_opt(2022, 12, 1).unwrap());
//! println!("{}", i);
//! ```

use chrono::{Datelike, Duration, NaiveDate, Utc, Weekday};
use date_time_parser::DateParser as NaturalDateParser;
use freezebox::FreezeBox;
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashMap;
use std::fmt;

lazy_static! {
	/// Regular expression to capture the parts of a tada list line.
	static ref RE_TADA_ITEM: Regex = Regex::new(r##"(?x)
		^                               # start
		( x \s+ )?                      # capture: optional "x"
		( [(] [A-Z] [)] \s+ )?          # capture: optional priority letter
		( \d{4} - \d{2} - \d{2} \s+ )?  # capture: optional date
		( \d{4} - \d{2} - \d{2} \s+ )?  # capture: optional date
		( .+ )                          # capture: description
		$                               # end
	"##)
	.unwrap();

	/// Regular expression to find key-value tags within a description.
	static ref RE_KV: Regex = Regex::new(r##"(?x)
		([^\s:]+)                       # capture: key
		:                               # colon
		([^\s:]+)                       # capture: value
	"##)
	.unwrap();

	/// Regular expression to find tags within a description.
	static ref RE_TAG: Regex = Regex::new(r##"(?x)
		(?:^|\s)                        # whitespace or start of string
		[+]                             # plus sign
		(\S+)                           # capture: tag
	"##)
	.unwrap();

	/// Regular expression to find contexts within a description.
	static ref RE_CONTEXT: Regex = Regex::new(r##"(?x)
		(?:^|\s)                        # whitespace or start of string
		[@]                             # at sign
		(\S+)                           # capture: context
	"##)
	.unwrap();

	/// Regular expression to match contexts indicating a small tshirt size.
	static ref RE_SMALL: Regex  = Regex::new("(?i)^X*S$").unwrap();

	/// Regular expression to match contexts indicating a medium tshirt size.
	static ref RE_MEDIUM: Regex = Regex::new("(?i)^X*M$").unwrap();

	/// Regular expression to match contexts indicating a large tshirt size.
	static ref RE_LARGE: Regex  = Regex::new("(?i)^X*L$").unwrap();

	/// Constant for today's date.
	///
	/// These date constants are evaluated once to ensure predictable behaviour
	/// when the application is run at midnight.
	///
	/// This may cause issues later on if we want to run a persistent tadalist
	/// process.
	static ref DATE_TODAY: NaiveDate = Utc::now().date_naive();

	/// Constant representing "soon".
	///
	/// Tomorrow or overmorrow.
	static ref DATE_SOON: NaiveDate = Utc::now().date_naive() + Duration::days(2);

	/// Constant representing the end of this week.
	///
	/// Weeks end on Sunday.
	static ref DATE_WEEKEND: NaiveDate = Utc::now().date_naive().week(Weekday::Mon).last_day();

	/// Constant representing the end of next week.
	static ref DATE_NEXT_WEEKEND: NaiveDate = Utc::now().date_naive().week(Weekday::Mon).last_day() + Duration::days(7);

	/// Constant representing the end of next month.
	///
	/// Who cares when *this* month ends?!
	static ref DATE_NEXT_MONTH: NaiveDate = {
		let date = Utc::now().date_naive();
		match date.month() {
			11 => NaiveDate::from_ymd_opt(date.year() + 1, 1, 1),
			12 => NaiveDate::from_ymd_opt(date.year() + 1, 2, 1),
			_ => NaiveDate::from_ymd_opt(date.year(), date.month() + 2, 1),
		}
		.unwrap()
		.pred_opt()
		.unwrap()
	};
}

/// Five levels of importance are defined.
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum Importance {
	/// Critical
	A,
	/// Important
	B,
	/// Semi-important
	C,
	/// Normal
	D,
	/// Unimportant
	E,
}

impl Importance {
	/// Get an importance from a letter.
	pub fn from_char(c: char) -> Option<Self> {
		match c {
			'A' => Some(Self::A),
			'B' => Some(Self::B),
			'C' => Some(Self::C),
			'D' => Some(Self::D),
			'E'..='Z' => Some(Self::E),
			_ => None,
		}
	}

	/// Returns a letter representing this importance.
	pub fn to_char(&self) -> char {
		match self {
			Self::A => 'A',
			Self::B => 'B',
			Self::C => 'C',
			Self::D => 'D',
			Self::E => 'E',
		}
	}

	/// Returns a heading suitable for items of this importance.
	pub fn to_string(&self) -> &str {
		match self {
			Self::A => "Critical",
			Self::B => "Important",
			Self::C => "Semi-important",
			Self::D => "Normal",
			Self::E => "Unimportant",
		}
	}

	/// Returns a list of known importances, in a sane order.
	pub fn all() -> Vec<Self> {
		Vec::from([Self::A, Self::B, Self::C, Self::D, Self::E])
	}
}

impl Default for Importance {
	/// Default is D.
	fn default() -> Self {
		Self::D
	}
}

/// Seven levels of urgency are defined.
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum Urgency {
	/// A due date earlier than today.
	Overdue,
	/// A due date today.
	Today,
	/// A due date tomorrow or overmorrow.
	Soon,
	/// A due date by the end of this week. Note that if it is Friday or later, any
	/// tasks due this week will fall into the `Today` or `Soon` urgencies instead.
	ThisWeek,
	/// A due date by the end of next week.
	NextWeek,
	/// A due date by the end of next month.
	///
	/// There is no `ThisMonth` urgency because for almost half the month any tasks
	/// would fall into the `ThisWeek` or `NextWeek` urgencies instead, making
	/// `ThisMonth` fairly useless.
	NextMonth,
	/// Any due date after the end of next month.
	Later,
}

impl Urgency {
	/// Calculate urgency from a due date.
	pub fn from_due_date(due: NaiveDate) -> Self {
		if due < *DATE_TODAY {
			Self::Overdue
		} else if due == *DATE_TODAY {
			Self::Today
		} else if due <= *DATE_SOON {
			Self::Soon
		} else if due <= *DATE_WEEKEND {
			Self::ThisWeek
		} else if due <= *DATE_NEXT_WEEKEND {
			Self::NextWeek
		} else if due <= *DATE_NEXT_MONTH {
			Self::NextMonth
		} else {
			Self::Later
		}
	}

	/// Returns a heading suitable for items of this urgency.
	pub fn to_string(&self) -> &str {
		match self {
			Self::Overdue => "Overdue",
			Self::Today => "Today",
			Self::Soon => "Soon",
			Self::ThisWeek => "This week",
			Self::NextWeek => "Next week",
			Self::NextMonth => "Next month",
			Self::Later => "Later",
		}
	}

	/// Returns a list of known urgencies, in a sane order.
	pub fn all() -> Vec<Self> {
		Vec::from([
			Self::Overdue,
			Self::Today,
			Self::Soon,
			Self::ThisWeek,
			Self::NextWeek,
			Self::NextMonth,
			Self::Later,
		])
	}
}

impl Default for Urgency {
	/// Default is soon, but you should rely on the default as little as possible.
	/// It is useful when sorting tasks by urgency.
	fn default() -> Self {
		Self::Soon
	}
}

/// Three sizes are defined.
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
pub enum TshirtSize {
	Small,
	Medium,
	Large,
}

impl TshirtSize {
	/// Returns a heading suitable for items of this size.
	pub fn to_string(&self) -> &str {
		match self {
			Self::Small => "Small",
			Self::Medium => "Medium",
			Self::Large => "Large",
		}
	}

	/// Returns a list of known sizes, in a sane order.
	pub fn all() -> Vec<Self> {
		Vec::from([Self::Small, Self::Medium, Self::Large])
	}
}

impl Default for TshirtSize {
	/// Default is medium.
	fn default() -> Self {
		Self::Medium
	}
}

/// An item in a todo list.
///
/// # Examples
///
/// ```
/// use tada::{Importance, Item};
/// let i = Item::parse("(A) clean my @home");
/// assert_eq!(Some(Importance::A), i.importance());
/// assert_eq!("clean my @home", i.description());
/// assert!(i.has_context("home"));
/// ```
pub struct Item {
	line_number: usize,
	completion: bool,
	priority: char,
	completion_date: Option<NaiveDate>,
	creation_date: Option<NaiveDate>,
	description: String,
	_importance: FreezeBox<Option<Importance>>,
	_due_date: FreezeBox<Option<NaiveDate>>,
	_start_date: FreezeBox<Option<NaiveDate>>,
	_urgency: FreezeBox<Option<Urgency>>,
	_tshirt_size: FreezeBox<Option<TshirtSize>>,
	_tags: FreezeBox<Vec<String>>,
	_contexts: FreezeBox<Vec<String>>,
	_kv: FreezeBox<HashMap<String, String>>,
}

impl Item {
	pub fn new() -> Item {
		Item {
			line_number: 0,
			completion: false,
			priority: '\0',
			completion_date: None,
			creation_date: None,
			description: String::new(),
			_importance: FreezeBox::default(),
			_due_date: FreezeBox::default(),
			_start_date: FreezeBox::default(),
			_urgency: FreezeBox::default(),
			_tshirt_size: FreezeBox::default(),
			_tags: FreezeBox::default(),
			_contexts: FreezeBox::default(),
			_kv: FreezeBox::default(),
		}
	}

	/// Parse an item from a line of text.
	///
	/// Assumes the [todo.txt](https://github.com/todotxt/todo.txt) format.
	pub fn parse(text: &str) -> Item {
		let caps = RE_TADA_ITEM.captures(text).unwrap();
		let blank = Self::new();

		Item {
			completion: caps.get(1).is_some(),
			priority: match caps.get(2) {
				Some(p) => p.as_str().chars().nth(1).unwrap(),
				None => '\0',
			},
			completion_date: if caps.get(3).is_some() && caps.get(4).is_some() {
				let cap3 = caps.get(3).unwrap().as_str();
				NaiveDate::parse_from_str(cap3.trim(), "%Y-%m-%d").ok()
			} else {
				None
			},
			creation_date: if caps.get(3).is_some() && caps.get(4).is_some() {
				let cap4 = caps.get(4).unwrap().as_str();
				NaiveDate::parse_from_str(cap4.trim(), "%Y-%m-%d").ok()
			} else if caps.get(3).is_some() {
				let cap3 = caps.get(3).unwrap().as_str();
				NaiveDate::parse_from_str(cap3.trim(), "%Y-%m-%d").ok()
			} else {
				None
			},
			description: match caps.get(5) {
				Some(m) => String::from(m.as_str().trim()),
				None => String::from(""),
			},
			..blank
		}
	}

	/// Create a version of this item but representing a completed task.
	pub fn but_done(&self, include_date: bool) -> Item {
		let mut i = self.clone();
		i.set_completion(true);
		if include_date {
			i.set_completion_date(*DATE_TODAY);
			if i.creation_date().is_none() {
				i.set_creation_date(*DATE_TODAY);
			}
		}
		i
	}

	/// Provide zen-like calm by rescheduling an overdue task.
	pub fn zen(&self) -> Item {
		if self.urgency() == Some(Urgency::Overdue) {
			let mut new = self.clone();
			let important = matches!(
				new.importance(),
				Some(Importance::A) | Some(Importance::B)
			);
			let small = matches!(new.tshirt_size(), Some(TshirtSize::Small));
			let new_urgency = if important && small {
				Urgency::Soon
			} else if important || small {
				Urgency::NextWeek
			} else {
				Urgency::NextMonth
			};
			new.set_urgency(new_urgency);
			return new;
		}
		self.clone()
	}

	/// Pull a task forward to being done with a new urgency, also clearing any start date.
	pub fn but_pull(&self, new_urgency: Urgency) -> Item {
		let mut new = self.clone();
		if new.completion() {
			return new;
		}
		new.set_urgency(new_urgency);

		let re = Regex::new(r"start:(?:[^\s:]+)").unwrap();
		let new_start = format!("start:{}", DATE_TODAY.format("%Y-%m-%d"));
		new.set_description(format!(
			"{}",
			re.replace(&new.description, new_start)
		));

		new
	}

	/// Performs a bunch of small fixes on the item syntax.
	pub fn fixup(&self, warnings: bool) -> Item {
		let maybe_warn = |w| {
			if warnings {
				eprintln!("{w}");
			}
		};
		let mut new = self.clone();

		if new.priority() == '\0' {
			maybe_warn(String::from("Hint: a task can be given an importance be prefixing it with a parenthesized capital letter, like `(A)`."));
		}

		for slot in ["due", "start"] {
			match new.kv().get(slot) {
				Some(given_date) => {
					if NaiveDate::parse_from_str(given_date, "%Y-%m-%d")
						.is_err()
					{
						let processed_date = given_date.replace('_', " ");
						if let Some(naive_date) =
							NaturalDateParser::parse(&processed_date)
						{
							new.set_description(new.description().replace(
								&format!("{slot}:{given_date}"),
								&format!(
									"{}:{}",
									slot,
									naive_date.format("%Y-%m-%d")
								),
							));
							maybe_warn(format!(
								"Notice: {} date `{}` changed to `{}`.",
								slot,
								given_date,
								naive_date.format("%Y-%m-%d")
							));
						} else {
							maybe_warn(format!("Notice: {slot} date `{given_date}` should be in YYYY-MM-DD format."));
						}
					}
				}
				None => {
					if slot == "due" {
						maybe_warn(format!("Hint: a task can be given a {slot} date by including `{slot}:YYYY-MM-DD`."));
					}
				}
			}
		}

		if new.tshirt_size().is_none() {
			maybe_warn(String::from("Hint: a task can be given a size by including `@S`, `@M`, or `@L`."));
		}

		if new.description().len() > 120 {
			maybe_warn(String::from("Hint: long descriptions can make a task list slower to skim read."));
		} else if new.description().len() < 30 {
			maybe_warn(String::from("Hint: short descriptions can make it hard to remember what a task means!"));
		}

		new
	}

	/// Whether the task is complete.
	pub fn completion(&self) -> bool {
		self.completion
	}

	/// Set indicator of whether the task is complete.
	pub fn set_completion(&mut self, x: bool) {
		self.completion = x;
	}

	/// Line number indicator (sometimes zero).
	pub fn line_number(&self) -> usize {
		self.line_number
	}

	/// Set line number indicator for the task.
	pub fn set_line_number(&mut self, x: usize) {
		self.line_number = x;
	}

	/// Task priority/importance as given in a todo.txt file.
	///
	/// A is highest, then B and C. D should be considered normal. E is low priority.
	/// Any uppercase letter is valid, but letters after E are not especially meaningful.
	///
	/// The importance() method is better.
	pub fn priority(&self) -> char {
		self.priority
	}

	/// Set task priority.
	pub fn set_priority(&mut self, x: char) {
		self.priority = x;
	}

	/// Completion date.
	///
	/// Often none.
	pub fn completion_date(&self) -> Option<NaiveDate> {
		self.completion_date
	}

	/// Set the completion date to a given date.
	pub fn set_completion_date(&mut self, x: NaiveDate) {
		self.completion_date = Some(x);
	}

	/// Set the completion date to None.
	pub fn clear_completion_date(&mut self) {
		self.completion_date = None;
	}

	/// Task creation date.
	///
	/// Often none.
	pub fn creation_date(&self) -> Option<NaiveDate> {
		self.creation_date
	}

	/// Set the task creation date to a given date.
	pub fn set_creation_date(&mut self, x: NaiveDate) {
		self.creation_date = Some(x);
	}

	/// Set the task creation date to None.
	pub fn clear_creation_date(&mut self) {
		self.creation_date = None;
	}

	/// Task description.
	pub fn description(&self) -> String {
		self.description.clone()
	}

	/// Set the task description.
	///
	/// Internally clears cached tags, etc.
	pub fn set_description(&mut self, x: String) {
		self._importance = FreezeBox::default();
		self._due_date = FreezeBox::default();
		self._urgency = FreezeBox::default();
		self._tshirt_size = FreezeBox::default();
		self._tags = FreezeBox::default();
		self._contexts = FreezeBox::default();
		self._kv = FreezeBox::default();
		self.description = x;
	}

	/// Return the importance of this task.
	///
	/// Basically the same as priority, except it's an enum and all letters after E
	/// are treated as being the same as E.
	pub fn importance(&self) -> Option<Importance> {
		if !self._importance.is_initialized() {
			self._importance
				.lazy_init(self._build_importance());
		}
		*self._importance
	}

	fn _build_importance(&self) -> Option<Importance> {
		Importance::from_char(self.priority)
	}

	/// Set the item's importance.
	pub fn set_importance(&mut self, i: Importance) {
		self.priority = i.to_char();
		self._importance = FreezeBox::default();
	}

	/// Set the item's importance.
	pub fn clear_importance(&mut self) {
		self.priority = '\0';
		self._importance = FreezeBox::default();
	}

	/// Return the date when this task is due by.
	pub fn due_date(&self) -> Option<NaiveDate> {
		if !self._due_date.is_initialized() {
			self._due_date.lazy_init(self._build_due_date());
		}
		*self._due_date
	}

	fn _build_due_date(&self) -> Option<NaiveDate> {
		match self.kv().get("due") {
			Some(dd) => NaiveDate::parse_from_str(dd, "%Y-%m-%d").ok(),
			None => None,
		}
	}

	/// Return the date when this task may be started.
	pub fn start_date(&self) -> Option<NaiveDate> {
		if !self._start_date.is_initialized() {
			self._start_date
				.lazy_init(self._build_start_date());
		}
		*self._start_date
	}

	fn _build_start_date(&self) -> Option<NaiveDate> {
		match self.kv().get("start") {
			Some(dd) => NaiveDate::parse_from_str(dd, "%Y-%m-%d").ok(),
			None => None,
		}
	}

	/// A task is startable if it doesn't have a start date which is in the future.
	pub fn is_startable(&self) -> bool {
		match self.start_date() {
			Some(day) => day <= *DATE_TODAY,
			None => true,
		}
	}

	/// Classify how urgent this task is.
	pub fn urgency(&self) -> Option<Urgency> {
		if !self._urgency.is_initialized() {
			self._urgency.lazy_init(self._build_urgency());
		}
		*self._urgency
	}

	fn _build_urgency(&self) -> Option<Urgency> {
		self.due_date().map(Urgency::from_due_date)
	}

	/// Set task urgency.
	pub fn set_urgency(&mut self, urg: Urgency) {
		let mut d = match urg {
			Urgency::Overdue => DATE_TODAY.pred_opt().unwrap(),
			Urgency::Today => *DATE_TODAY,
			Urgency::Soon => *DATE_SOON,
			Urgency::ThisWeek => *DATE_WEEKEND,
			Urgency::NextWeek => *DATE_NEXT_WEEKEND,
			Urgency::NextMonth => *DATE_NEXT_MONTH,
			Urgency::Later => *DATE_TODAY + Duration::days(183), // about 6 months
		};
		// Work and school tasks should be rescheduled from Saturday/Sunday.
		if urg > Urgency::Today
			&& (self.has_context("work") || self.has_context("school"))
		{
			d = match format!("{}", d.format("%u")).as_str() {
				"6" => d.pred_opt().unwrap(),
				"7" => d.pred_opt().unwrap().pred_opt().unwrap(),
				_ => d,
			};
		}

		let formatted = d.format("%Y-%m-%d");

		match self.kv().get("due") {
			Some(str) => {
				self.set_description(self.description().replace(
					&format!("due:{str}"),
					&format!("due:{formatted}"),
				))
			}
			None => self.set_description(format!(
				"{} due:{formatted}",
				self.description()
			)),
		}
	}

	/// Return the size of this task.
	pub fn tshirt_size(&self) -> Option<TshirtSize> {
		if !self._tshirt_size.is_initialized() {
			self._tshirt_size
				.lazy_init(self._build_tshirt_size());
		}
		*self._tshirt_size
	}

	fn _build_tshirt_size(&self) -> Option<TshirtSize> {
		let ctx = self.contexts();

		let mut tmp = ctx.iter().filter(|x| RE_SMALL.is_match(x));
		if tmp.next().is_some() {
			return Some(TshirtSize::Small);
		}

		let mut tmp = ctx.iter().filter(|x| RE_MEDIUM.is_match(x));
		if tmp.next().is_some() {
			return Some(TshirtSize::Medium);
		}

		let mut tmp = ctx.iter().filter(|x| RE_LARGE.is_match(x));
		if tmp.next().is_some() {
			return Some(TshirtSize::Large);
		}

		None
	}

	/// Tags.
	#[allow(dead_code)]
	pub fn tags(&self) -> Vec<String> {
		if !self._tags.is_initialized() {
			self._tags.lazy_init(self._build_tags());
		}
		// Need to return a copy
		(*self._tags).to_vec()
	}

	fn _build_tags(&self) -> Vec<String> {
		let mut tags: Vec<String> = Vec::new();
		for cap in RE_TAG.captures_iter(&self.description) {
			tags.push(cap[1].to_string());
		}
		tags
	}

	/// Boolean indicating whether a task has a particular tag.
	pub fn has_tag(&self, tag: &str) -> bool {
		let real_tag = match tag.chars().next() {
			Some('+') => tag.get(1..).unwrap(),
			_ => tag,
		};
		let real_tag = real_tag.to_lowercase();
		self.tags()
			.iter()
			.any(|t| t.to_lowercase().as_str() == real_tag)
	}

	/// Contexts.
	pub fn contexts(&self) -> Vec<String> {
		if !self._contexts.is_initialized() {
			self._contexts.lazy_init(self._build_contexts());
		}
		// Need to return a copy
		(*self._contexts).to_vec()
	}

	fn _build_contexts(&self) -> Vec<String> {
		let mut tags: Vec<String> = Vec::new();
		for cap in RE_CONTEXT.captures_iter(&self.description) {
			tags.push(cap[1].to_string());
		}
		tags
	}

	/// Boolean indicating whether a task has a particular context.
	pub fn has_context(&self, ctx: &str) -> bool {
		let real_ctx = match ctx.chars().next() {
			Some('@') => ctx.get(1..).unwrap(),
			_ => ctx,
		};
		let real_ctx = real_ctx.to_lowercase();
		self.contexts()
			.iter()
			.any(|c| c.to_lowercase().as_str() == real_ctx)
	}

	/// Key-Value Tags.
	pub fn kv(&self) -> HashMap<String, String> {
		if !self._kv.is_initialized() {
			self._kv.lazy_init(self._build_kv());
		}
		// Need to return a copy
		let mut kv_clone: HashMap<String, String> = HashMap::new();
		for (k, v) in &*self._kv {
			kv_clone.insert(k.clone(), v.clone());
		}
		kv_clone
	}

	fn _build_kv(&self) -> HashMap<String, String> {
		let mut kv: HashMap<String, String> = HashMap::new();
		for cap in RE_KV.captures_iter(&self.description) {
			kv.insert(cap[1].to_string(), cap[2].to_string());
		}
		kv
	}

	/// Key used for smart sorting
	pub fn smart_key(&self) -> (Urgency, Importance, TshirtSize) {
		(
			self.urgency().unwrap_or_default(),
			self.importance().unwrap_or_default(),
			self.tshirt_size().unwrap_or_default(),
		)
	}
}

impl Default for Item {
	fn default() -> Self {
		Self::new()
	}
}

impl Clone for Item {
	fn clone(&self) -> Self {
		Item {
			line_number: self.line_number,
			completion: self.completion,
			priority: self.priority,
			completion_date: self.completion_date,
			creation_date: self.creation_date,
			description: self.description.clone(),
			..Item::new()
		}
	}
}

impl fmt::Debug for Item {
	/// Debugging output; used for format!("{:?}")
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.debug_struct("Item")
			.field("completion", &self.completion)
			.field("priority", &self.priority)
			.field("completion_date", &self.completion_date)
			.field("creation_date", &self.creation_date)
			.field("description", &self.description)
			.finish()
	}
}

impl fmt::Display for Item {
	/// File-ready output; used for format!("{}")
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		if self.completion {
			write!(f, "x ")?;
		}

		if self.priority != '\0' {
			write!(f, "({}) ", self.priority)?;
		}

		if self.completion {
			if let Some(d) = self.completion_date {
				write!(f, "{} ", d.format("%Y-%m-%d"))?;
			}
		}

		if let Some(d) = self.creation_date {
			write!(f, "{} ", d.format("%Y-%m-%d"))?;
		}

		write!(f, "{}", self.description)
	}
}

#[cfg(test)]
mod tests_item {
	use super::*;
	use chrono::NaiveDate;

	#[test]
	fn test_debug() {
		let b = Item::new();
		let i = Item {
			completion: false,
			priority: '\0',
			completion_date: None,
			creation_date: None,
			description: "foo bar baz".to_string(),
			..b
		};
		let dbug = format!("{i:?}");
		assert!(dbug.len() > 1);
	}

	#[test]
	fn test_display() {
		let b = Item::new();
		let i = Item {
			description: "foo bar baz".to_string(),
			..b
		};

		assert_eq!("foo bar baz", format!("{i}"));

		let b = Item::new();
		let i = Item {
			completion: true,
			priority: 'B',
			completion_date: Some(NaiveDate::from_ymd_opt(2010, 1, 1).unwrap()),
			creation_date: Some(NaiveDate::from_ymd_opt(2000, 12, 31).unwrap()),
			description: "foo bar baz".to_string(),
			..b
		};

		assert_eq!("x (B) 2010-01-01 2000-12-31 foo bar baz", format!("{i}"));
	}

	#[test]
	fn test_parse() {
		// Parse a complex line
		let i = Item::parse("x (B) 2010-01-01 2000-12-31 foo bar baz");

		assert_eq!(true, i.completion);
		assert_eq!('B', i.priority);
		assert_eq!(
			NaiveDate::from_ymd_opt(2010, 1, 1).unwrap(),
			i.completion_date.unwrap()
		);
		assert_eq!(
			NaiveDate::from_ymd_opt(2000, 12, 31).unwrap(),
			i.creation_date.unwrap()
		);
		assert_eq!("foo bar baz".to_string(), i.description);
		assert!(i.urgency().is_none());
		assert_eq!(Some(Importance::B), i.importance());
		assert_eq!(None, i.tshirt_size());
		assert_eq!(Vec::<String>::new(), i.tags());
		assert_eq!(Vec::<String>::new(), i.contexts());
		assert_eq!(HashMap::<String, String>::new(), i.kv());

		// Parse a misleading line
		let i = Item::parse("2010-01-01 (A) foo bar baz");

		assert!(!i.completion);
		assert_eq!('\0', i.priority);
		assert!(i.completion_date.is_none());
		assert_eq!(
			NaiveDate::from_ymd_opt(2010, 1, 1).unwrap(),
			i.creation_date.unwrap()
		);
		assert_eq!("(A) foo bar baz".to_string(), i.description);
	}

	#[test]
	fn test_kv() {
		let i = Item::parse("(A) foo bar abc:xyz def:123");
		let expected_kv = HashMap::from([
			("abc".to_string(), "xyz".to_string()),
			("def".to_string(), "123".to_string()),
		]);

		assert_eq!('A', i.priority);
		assert_eq!("foo bar abc:xyz def:123".to_string(), i.description);
		assert_eq!(expected_kv, i.kv());
		assert_eq!(expected_kv, i.kv());
	}

	#[test]
	fn test_due_date() {
		let i = Item::parse("(A) foo bar due:1980-06-01");

		assert_eq!(
			NaiveDate::from_ymd_opt(1980, 6, 1).unwrap(),
			i.due_date().unwrap()
		);
	}

	#[test]
	fn test_urgency() {
		let i = Item::parse("(A) foo bar due:1970-06-01");
		assert_eq!(Urgency::Overdue, i.urgency().unwrap());

		let i = Item::parse(&format!(
			"(A) foo bar due:{}",
			Utc::now().date_naive().format("%Y-%m-%d")
		));
		assert_eq!(Urgency::Today, i.urgency().unwrap());

		let i = Item::parse(&format!(
			"(A) foo bar due:{}",
			(Utc::now().date_naive() + Duration::days(1)).format("%Y-%m-%d")
		));
		assert_eq!(Urgency::Soon, i.urgency().unwrap());

		let i = Item::parse(&format!(
			"(A) foo bar due:{}",
			(Utc::now().date_naive() + Duration::days(18)).format("%Y-%m-%d")
		));
		assert_eq!(Urgency::NextMonth, i.urgency().unwrap());

		let i = Item::parse("(A) foo bar due:3970-06-01");
		assert_eq!(Urgency::Later, i.urgency().unwrap());
	}

	#[test]
	fn test_tags() {
		let i = Item::parse("(A) +Foo +foo bar+baz +bam");
		let expected_tags = Vec::from([
			"Foo".to_string(),
			"foo".to_string(),
			"bam".to_string(),
		]);
		assert_eq!(expected_tags, i.tags());
		assert!(i.has_tag("Foo"));
		assert!(i.has_tag("fOO"));
		assert!(!i.has_tag("Fool"));
	}

	#[test]
	fn test_contexts() {
		let i = Item::parse("(A) @Foo @foo bar@baz @bam");
		let expected_ctx = Vec::from([
			"Foo".to_string(),
			"foo".to_string(),
			"bam".to_string(),
		]);
		assert_eq!(expected_ctx, i.contexts());
		assert!(i.has_context("Foo"));
		assert!(i.has_context("fOO"));
		assert!(!i.has_context("Fool"));
	}

	#[test]
	fn test_tshirt_size() {
		let i = Item::parse("@M Barble");
		assert_eq!(TshirtSize::Medium, i.tshirt_size().unwrap());

		let i = Item::parse("(A) Fooble @XxL Barble");
		assert_eq!(TshirtSize::Large, i.tshirt_size().unwrap());

		let i = Item::parse("Barble");
		assert!(i.tshirt_size().is_none());
	}
}