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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use crate::error::Error;
use std::io::{self, Read, Write};
use url::Url;

pub trait Resource: Read + Write {
    fn new(location: Url) -> Result<Self, Error>
    where
        Self: Sized;

    fn close(&mut self);
}

pub struct Stdin {
    inp: ::std::io::Stdin,
}

impl Resource for Stdin {
    fn new(_: Url) -> Result<Stdin, Error> {
        Ok(Stdin { inp: io::stdin() })
    }

    fn close(&mut self) {}
}

impl Write for Stdin {
    fn write(&mut self, _buf: &[u8]) -> io::Result<usize> {
        Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl Read for Stdin {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let mut handle = self.inp.lock();
        handle.read(buf)
    }
}

pub struct Stdout {
    out: io::Stdout,
}

impl Resource for Stdout {
    fn new(_: Url) -> Result<Stdout, Error> {
        Ok(Stdout { out: io::stdout() })
    }

    fn close(&mut self) {}
}

impl Read for Stdout {
    fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
        Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
    }
}

impl Write for Stdout {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut handle = self.out.lock();
        handle.write_all(buf)?;

        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        let mut handle = self.out.lock();
        handle.flush()?;

        Ok(())
    }
}

pub struct Stderr {
    err: io::Stderr,
}

impl Resource for Stderr {
    fn new(_: Url) -> Result<Stderr, Error> {
        Ok(Stderr { err: io::stderr() })
    }

    fn close(&mut self) {}
}

impl Read for Stderr {
    fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
        Err(io::Error::new(io::ErrorKind::Other, "not implemented"))
    }
}

impl Write for Stderr {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut handle = self.err.lock();
        handle.write_all(buf)?;

        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        let mut handle = self.err.lock();
        handle.flush()?;

        Ok(())
    }
}