primitives.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi::{util::time::Timestamp, Result};
  19. use crate::due_as_timestamp;
  20. pub(crate) use taud::task_info::{State, TaskEvent, TaskInfo};
  21. #[derive(Clone, Debug)]
  22. pub struct BaseTask {
  23. pub title: String,
  24. pub tags: Vec<String>,
  25. pub desc: Option<String>,
  26. pub assign: Vec<String>,
  27. pub project: Vec<String>,
  28. pub due: Option<u64>,
  29. pub rank: Option<f32>,
  30. }
  31. impl From<BaseTask> for TaskInfo {
  32. fn from(value: BaseTask) -> Self {
  33. Self {
  34. ref_id: String::default(),
  35. workspace: String::default(),
  36. id: u32::default(),
  37. title: value.title,
  38. tags: value.tags,
  39. desc: String::default(),
  40. owner: String::default(),
  41. assign: value.assign,
  42. project: value.project,
  43. due: value.due.map(Timestamp),
  44. rank: value.rank,
  45. created_at: Timestamp(u64::default()),
  46. state: String::default(),
  47. events: vec![],
  48. comments: vec![],
  49. }
  50. }
  51. }
  52. pub fn task_from_cli(values: Vec<String>) -> Result<BaseTask> {
  53. let mut title = String::new();
  54. let mut tags = vec![];
  55. let mut desc = None;
  56. let mut project = vec![];
  57. let mut assign = vec![];
  58. let mut due = None;
  59. let mut rank = None;
  60. for val in values {
  61. let field: Vec<&str> = val.split(':').collect();
  62. if field.len() == 1 {
  63. if field[0].starts_with('+') || field[0].starts_with('-') {
  64. tags.push(field[0].into());
  65. continue
  66. }
  67. if field[0].starts_with('@') {
  68. assign.push(field[0].into());
  69. continue
  70. }
  71. title.push_str(field[0]);
  72. title.push(' ');
  73. continue
  74. }
  75. if field.len() != 2 {
  76. continue
  77. }
  78. if field[0] == "project" {
  79. project = field[1].split(',').map(|s| s.into()).collect();
  80. }
  81. if field[0] == "desc" {
  82. desc = Some(field[1].into());
  83. }
  84. if field[0] == "due" {
  85. due = due_as_timestamp(field[1])
  86. }
  87. if field[0] == "rank" {
  88. rank = Some(field[1].parse::<f32>()?);
  89. }
  90. }
  91. let title = title.trim().into();
  92. Ok(BaseTask { title, tags, desc, project, assign, due, rank })
  93. }