primitives.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. use std::{fmt, str::FromStr};
  2. use darkfi::{util::Timestamp, Error, Result};
  3. use crate::due_as_timestamp;
  4. pub enum State {
  5. Open,
  6. Start,
  7. Pause,
  8. Stop,
  9. }
  10. impl State {
  11. pub const fn is_start(&self) -> bool {
  12. matches!(*self, Self::Start)
  13. }
  14. pub const fn is_pause(&self) -> bool {
  15. matches!(*self, Self::Pause)
  16. }
  17. }
  18. impl fmt::Display for State {
  19. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  20. match self {
  21. State::Open => write!(f, "open"),
  22. State::Start => write!(f, "start"),
  23. State::Stop => write!(f, "stop"),
  24. State::Pause => write!(f, "pause"),
  25. }
  26. }
  27. }
  28. impl FromStr for State {
  29. type Err = Error;
  30. fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
  31. let result = match s.to_lowercase().as_str() {
  32. "open" => State::Open,
  33. "stop" => State::Stop,
  34. "start" => State::Start,
  35. "pause" => State::Pause,
  36. _ => return Err(Error::ParseFailed("unable to parse state")),
  37. };
  38. Ok(result)
  39. }
  40. }
  41. #[derive(serde::Serialize, serde::Deserialize, Debug)]
  42. pub struct BaseTask {
  43. pub title: String,
  44. pub desc: Option<String>,
  45. pub assign: Vec<String>,
  46. pub project: Vec<String>,
  47. pub due: Option<i64>,
  48. pub rank: Option<f32>,
  49. }
  50. #[derive(Clone, serde::Serialize, serde::Deserialize, Debug)]
  51. pub struct TaskInfo {
  52. pub ref_id: String,
  53. pub workspace: String,
  54. pub id: u32,
  55. pub title: String,
  56. pub desc: String,
  57. pub owner: String,
  58. pub assign: Vec<String>,
  59. pub project: Vec<String>,
  60. pub due: Option<i64>,
  61. pub rank: f32,
  62. pub created_at: i64,
  63. pub state: String,
  64. pub events: Vec<TaskEvent>,
  65. pub comments: Vec<Comment>,
  66. }
  67. #[derive(Clone, serde::Serialize, serde::Deserialize, Debug)]
  68. pub struct TaskEvent {
  69. pub action: String,
  70. pub author: String,
  71. pub content: String,
  72. pub timestamp: Timestamp,
  73. }
  74. impl std::fmt::Display for TaskEvent {
  75. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  76. write!(f, "action: {}, timestamp: {}", self.action, self.timestamp)
  77. }
  78. }
  79. impl Default for TaskEvent {
  80. fn default() -> Self {
  81. Self {
  82. action: State::Open.to_string(),
  83. author: "".to_string(),
  84. content: "".to_string(),
  85. timestamp: Timestamp::current_time(),
  86. }
  87. }
  88. }
  89. #[derive(Clone, serde::Serialize, serde::Deserialize, Debug)]
  90. pub struct Comment {
  91. content: String,
  92. author: String,
  93. timestamp: Timestamp,
  94. }
  95. impl std::fmt::Display for Comment {
  96. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  97. write!(f, "{} author: {}, content: {} ", self.timestamp, self.author, self.content)
  98. }
  99. }
  100. pub fn task_from_cli(values: Vec<String>) -> Result<BaseTask> {
  101. let mut title = String::new();
  102. let mut desc = None;
  103. let mut project = vec![];
  104. let mut assign = vec![];
  105. let mut due = None;
  106. let mut rank = None;
  107. for val in values {
  108. let field: Vec<&str> = val.split(':').collect();
  109. if field.len() == 1 {
  110. title = field[0].into();
  111. continue
  112. }
  113. if field.len() != 2 {
  114. continue
  115. }
  116. if field[0] == "project" {
  117. project = field[1].split(',').map(|s| s.into()).collect();
  118. }
  119. if field[0] == "desc" {
  120. desc = Some(field[1].into());
  121. }
  122. if field[0] == "assign" {
  123. assign = field[1].split(',').map(|s| s.into()).collect();
  124. }
  125. if field[0] == "due" {
  126. due = due_as_timestamp(field[1])
  127. }
  128. if field[0] == "rank" {
  129. rank = Some(field[1].parse::<f32>()?);
  130. }
  131. }
  132. Ok(BaseTask { title, desc, project, assign, due, rank })
  133. }