summaryrefslogtreecommitdiffstats
path: root/librepology/src/v1/buffer.rs
blob: 534ab4c15180d0fae2363a01c1f63ffc6882f9fc (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
37
38
39
40
41
use std::io::Read;

use crate::v1::error::Result;
use crate::v1::error::RepologyError as Error;
use crate::v1::types::Problem;
use crate::v1::types::Package;
use crate::v1::api::Api;

#[derive(Debug)]
pub struct BufferApi {
    buf: String,
}

impl BufferApi
{
    pub fn read_from<R>(mut input: R) -> Result<BufferApi>
        where R: Read,
    {
        let mut buf = String::new();
        let read = input.read_to_string(&mut buf)?;
        trace!("Read {} bytes from stdin", read);
        Ok(BufferApi { buf })
    }
}

impl Api for BufferApi {

    fn project<N: AsRef<str>>(&self, _name: N) -> Result<Vec<Package>> {
        serde_json::de::from_str(&self.buf).map_err(Error::from)
    }

    fn problems_for_repo<R: AsRef<str>>(&self, _repo: R) -> Result<Vec<Problem>> {
        serde_json::de::from_str(&self.buf).map_err(Error::from)
    }

    fn problems_for_maintainer<M: AsRef<str>>(&self, _maintainer: M) -> Result<Vec<Problem>> {
        serde_json::de::from_str(&self.buf).map_err(Error::from)
    }

}