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
use std::{
    fmt::Display,
    fs::File,
    io::{self, BufWriter, Write},
    path::{Path, PathBuf},
};

use serde::Serialize;

use crate::{io::FromFileError, types::Directory};

#[derive(Debug)]
#[non_exhaustive]
pub struct WriteError {
    pub path: PathBuf,
    pub kind: WriteErrorKind,
}

impl Display for WriteError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "unable to write file {:?}", self.path)
    }
}

impl std::error::Error for WriteError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match &self.kind {
            WriteErrorKind::OpenFile(err) => Some(err),
            WriteErrorKind::Serialize(err) => Some(err),
            WriteErrorKind::Stream(err) => Some(err),
        }
    }
}

#[derive(Debug)]
pub enum WriteErrorKind {
    OpenFile(io::Error),
    Serialize(serde_json::Error),
    Stream(io::Error),
}

// REFACTOR: most of this impl is the same across all types
/// Configuration file for some component of the monorepo.
pub trait ConfigurationFile: Sized {
    type Contents: Serialize;

    /// Basename of the configuration file.
    const FILENAME: &'static str;

    /// Create an instance of this configuration file by reading
    /// the specified file from this directory on disk.
    fn from_directory(
        monorepo_root: &Directory,
        relative_directory: Directory,
    ) -> Result<Self, FromFileError>;

    /// Relative path to directory containing this configuration file,
    /// from monorepo root.
    fn directory(&self) -> &Directory;

    /// Relative path to this configuration file from the monorepo root.
    fn path(&self) -> PathBuf;

    fn contents(&self) -> &Self::Contents;

    fn write(
        monorepo_root: &Path,
        configuration_file: impl ConfigurationFile,
    ) -> Result<(), WriteError> {
        let filename = monorepo_root.join(configuration_file.path());
        let file = File::create(filename.clone()).map_err(|err| WriteError {
            path: filename.clone(),
            kind: WriteErrorKind::OpenFile(err),
        })?;
        let mut writer = BufWriter::new(file);
        (|| {
            let s = serde_json::to_string_pretty(configuration_file.contents())
                .map_err(WriteErrorKind::Serialize)?;
            writeln!(writer, "{}", s).map_err(WriteErrorKind::Stream)
        })()
        .map_err(|kind| WriteError {
            path: filename,
            kind,
        })?;
        Ok(())
    }
}