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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Creates an abstraction layer to I/O operations

use std::fmt::{self, Arguments};
use std::fs;
use std::io::{self, Stdout, Stderr, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;

use name::{NameDisplay, NameStore};

/// Contains global shared I/O objects
pub struct GlobalIo {
    /// Shared standard output writer
    pub stdout: Rc<SharedWrite>,
}

impl GlobalIo {
    /// Creates a `GlobalIo` instance using the given `stdout` writer.
    pub fn new(stdout: Rc<SharedWrite>) -> GlobalIo {
        GlobalIo{
            stdout: stdout,
        }
    }

    /// Creates a `GlobalIo` instance whose `stdout` ignores all output.
    pub fn null() -> GlobalIo {
        GlobalIo::new(Rc::new(Sink))
    }
}

impl Default for GlobalIo {
    /// Creates a `GlobalIo` instance using standard output writer.
    fn default() -> GlobalIo {
        GlobalIo::new(Rc::new(io::stdout()))
    }
}

/// Describes the cause of an `io::Error`.
#[derive(Debug)]
pub struct IoError {
    /// Error value
    pub err: io::Error,
    /// Path to file whose operation produced the error
    pub path: PathBuf,
    /// I/O mode that produced the error
    pub mode: IoMode,
}

impl IoError {
    /// Creates a new `IoError`.
    pub fn new(mode: IoMode, path: &Path, err: io::Error) -> IoError {
        IoError{
            mode: mode,
            path: path.to_owned(),
            err: err,
        }
    }
}

impl fmt::Display for IoError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "failed to {} file `{}`: {}",
            self.mode, self.path.display(), self.err)
    }
}

impl NameDisplay for IoError {
    fn fmt(&self, _names: &NameStore, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// Indicates the type of I/O operation that generated an error.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum IoMode {
    /// Creating a new file
    Create,
    /// Opening an existing file
    Open,
    /// Reading data from an open file
    Read,
    /// Accessing file metadata
    Stat,
    /// Writing data to an open file
    Write,
}

impl fmt::Display for IoMode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match *self {
            IoMode::Create => "create",
            IoMode::Open => "open",
            IoMode::Read => "read",
            IoMode::Stat => "stat",
            IoMode::Write => "write",
        })
    }
}

/// A writer object that can operate using shared references.
pub trait SharedWrite {
    /// Analogous to `std::io::Write::write_all`; writes all bytes
    /// or returns an error.
    fn write_all(&self, buf: &[u8]) -> Result<(), IoError>;

    /// Analogous to `std::io::Write::write_all`; writes formatted arguments
    /// or returns an error.
    fn write_fmt(&self, fmt: Arguments) -> Result<(), IoError>;

    /// Analogous to `std::io::Write::flush`; flushes the output stream.
    fn flush(&self) -> Result<(), IoError>;
}

macro_rules! shared_write {
    ( $ty:ty => $name:expr ) => {
        impl SharedWrite for $ty {
            fn write_all(&self, buf: &[u8]) -> Result<(), IoError> {
                let mut lock = self.lock();
                Write::write_all(&mut lock, buf)
                    .map_err(|e| IoError::new(
                        IoMode::Write, Path::new($name), e))
            }

            fn write_fmt(&self, fmt: Arguments) -> Result<(), IoError> {
                let mut lock = self.lock();
                Write::write_fmt(&mut lock, fmt)
                    .map_err(|e| IoError::new(
                        IoMode::Write, Path::new($name), e))
            }

            fn flush(&self) -> Result<(), IoError> {
                let mut lock = self.lock();
                Write::flush(&mut lock)
                    .map_err(|e| IoError::new(
                        IoMode::Write, Path::new($name), e))
            }
        }
    }
}

shared_write!{ Stdout => "<stdout>" }
shared_write!{ Stderr => "<stderr>" }

/// A shared writer which sends all data into the void.
pub struct Sink;

impl SharedWrite for Sink {
    fn write_all(&self, _buf: &[u8]) -> Result<(), IoError> { Ok(()) }
    fn write_fmt(&self, _fmt: Arguments) -> Result<(), IoError> { Ok(()) }
    fn flush(&self) -> Result<(), IoError> { Ok(()) }
}

/// Wraps a `fs::File` as a shared writer, providing a path for error values.
pub struct File {
    file: fs::File,
    path: PathBuf,
}

impl File {
    /// Creates a new `File` from an open filehandle and path.
    pub fn new(file: fs::File, path: PathBuf) -> File {
        File{
            file: file,
            path: path,
        }
    }
}

impl SharedWrite for File {
    fn write_all(&self, buf: &[u8]) -> Result<(), IoError> {
        Write::write_all(&mut &self.file, buf)
            .map_err(|e| IoError::new(IoMode::Write, &self.path, e))
    }

    fn write_fmt(&self, fmt: Arguments) -> Result<(), IoError> {
        Write::write_fmt(&mut &self.file, fmt)
            .map_err(|e| IoError::new(IoMode::Write, &self.path, e))
    }

    fn flush(&self) -> Result<(), IoError> {
        Write::flush(&mut &self.file)
            .map_err(|e| IoError::new(IoMode::Write, &self.path, e))
    }
}