aboutsummaryrefslogtreecommitdiff
path: root/ticket/src/actions.rs
blob: 381c9f72370a24f9547cbef94d09ee83be06e1ea (plain)
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
use crate::Ticket;
use anyhow::Result;
use log::*;
use shared::find_root;
use std::{
  fs,
  path::PathBuf,
};

pub fn get_open_tickets() -> Result<Vec<Ticket>> {
  get_tickets(ticket_root()?.join("open"))
}

pub fn get_closed_tickets() -> Result<Vec<Ticket>> {
  get_tickets(ticket_root()?.join("closed"))
}

fn get_tickets(path: PathBuf) -> Result<Vec<Ticket>> {
  let mut out = Vec::new();
  debug!("Looking for ticket.");
  for entry in fs::read_dir(&path)? {
    let entry = entry?;
    let path = entry.path();
    trace!("Looking at entry {}.", path.display());
    if path.is_file() {
      trace!("Entry is a file.");
      out.push(toml::from_slice::<Ticket>(&fs::read(&path)?)?);
    }
  }
  out.sort_by(|a, b| a.number.cmp(&b.number));
  Ok(out)
}

pub fn ticket_root() -> Result<PathBuf> {
  Ok(find_root()?.join(".dev-suite").join("ticket"))
}