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
//! Labels interface

extern crate serde_json;

use self::super::{Github, Result};
use rep::{Label, LabelOptions};

pub struct Labels<'a> {
    github: &'a Github<'a>,
    owner: String,
    repo: String,
}

impl<'a> Labels<'a> {
    pub fn new<O, R>(github: &'a Github<'a>, owner: O, repo: R) -> Labels<'a>
        where O: Into<String>,
              R: Into<String>
    {
        Labels {
            github: github,
            owner: owner.into(),
            repo: repo.into(),
        }
    }

    fn path(&self, more: &str) -> String {
        format!("/repos/{}/{}/labels{}", self.owner, self.repo, more)
    }

    pub fn create(&self, lab: &LabelOptions) -> Result<Label> {
        let data = try!(serde_json::to_string(&lab));
        self.github.post::<Label>(&self.path(""), data.as_bytes())
    }

    pub fn update(&self, prevname: &str, lab: &LabelOptions) -> Result<Label> {
        let data = try!(serde_json::to_string(&lab));
        self.github.patch::<Label>(&self.path(&format!("/{}", prevname)), data.as_bytes())
    }

    pub fn delete(&self, name: &str) -> Result<()> {
        self.github
            .delete(&self.path(&format!("/{}", name)))
            .map(|_| ())
    }

    pub fn list(&self) -> Result<Vec<Label>> {
        self.github.get::<Vec<Label>>(&self.path(""))
    }
}