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
#![warn(dead_code)]
extern crate reqwest;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

use std::collections::HashMap;
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;

use reqwest::IntoUrl;
use serde::Serialize;

#[derive(Debug, Serialize, Deserialize)]
pub struct GistFile {
    pub content: String,
}

impl From<String> for GistFile {
    fn from(s: String) -> Self {
        GistFile{content: s}
    }
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Gist<'a> {
    pub description: &'a str,
    pub public: bool,
    pub files: HashMap<&'a str, GistFile>,
}

impl<'a> Gist<'a> {
    pub fn new() -> Self {
        Gist {
            description: "cargo gist",
            public: false,
            files: HashMap::<&'a str, GistFile>::new(),
        }
    }
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct GistResponse {
    pub html_url: String,
}

pub fn post_json<U, T>(url: U, json: &T) -> Result<reqwest::Response, reqwest::Error>
    where
        U: IntoUrl,
        T: Serialize,
{
    reqwest::Client::new().post(url).json(json).send()
}

pub fn read_file<P>(path: P, b: &mut String) -> Result<usize, io::Error>
    where
        P: AsRef<Path>,
{
    File::open(path).and_then(|mut f| f.read_to_string(b))
}