about summary refs log tree commit diff
path: root/pkgs/test/nixpkgs-check-by-name/src/check_result.rs
blob: cc004e0129313bb82efae697c9e75b4b461a3573 (plain) (blame)
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
use crate::ErrorWriter;
use itertools::{Either, Itertools};
use std::fmt;
use std::io;
use std::path::PathBuf;

pub enum CheckError {
    SearchPath {
        relative_package_dir: PathBuf,
        subpath: PathBuf,
        line: usize,
        text: String,
    },
    OutsidePathReference {
        relative_package_dir: PathBuf,
        subpath: PathBuf,
        line: usize,
        text: String,
    },
    UnresolvablePathReference {
        relative_package_dir: PathBuf,
        subpath: PathBuf,
        line: usize,
        text: String,
        io_error: io::Error,
    },
}

impl CheckError {
    pub fn into_result<A>(self) -> CheckResult<A> {
        Ok(Either::Left(vec![self]))
    }
}

impl fmt::Display for CheckError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CheckError::SearchPath { relative_package_dir, subpath, line, text } =>
                write!(
                    f,
                    "{}: File {} at line {line} contains the nix search path expression \"{}\" which may point outside the directory of that package.",
                    relative_package_dir.display(),
                    subpath.display(),
                    text
                ),
            CheckError::OutsidePathReference { relative_package_dir, subpath, line, text } =>
                write!(
                    f,
                    "{}: File {} at line {line} contains the path expression \"{}\" which may point outside the directory of that package.",
                    relative_package_dir.display(),
                    subpath.display(),
                    text,
                ),
            CheckError::UnresolvablePathReference { relative_package_dir, subpath, line, text, io_error } =>
                write!(
                    f,
                    "{}: File {} at line {line} contains the path expression \"{}\" which cannot be resolved: {io_error}.",
                    relative_package_dir.display(),
                    subpath.display(),
                    text,
                ),
        }
    }
}

pub fn write_check_result<A, W: io::Write>(
    error_writer: &mut ErrorWriter<W>,
    check_result: CheckResult<A>,
) -> anyhow::Result<Option<A>> {
    match check_result? {
        Either::Left(errors) => {
            for error in errors {
                error_writer.write(&error.to_string())?
            }
            Ok(None)
        }
        Either::Right(value) => Ok(Some(value)),
    }
}

pub fn pass<A>(value: A) -> CheckResult<A> {
    Ok(Either::Right(value))
}

pub type CheckResult<A> = anyhow::Result<Either<Vec<CheckError>, A>>;

pub fn flatten_check_results<I, O>(
    check_results: impl IntoIterator<Item = CheckResult<I>>,
    value_transform: impl Fn(Vec<I>) -> O,
) -> CheckResult<O> {
    let (errors, values): (Vec<_>, Vec<_>) = check_results
        .into_iter()
        .collect::<anyhow::Result<Vec<_>>>()?
        .into_iter()
        .partition_map(|r| r);

    // To combine the errors from the results we flatten all the error Vec's into a new Vec
    // This is not very efficient, but doesn't matter because generally we should have no errors
    let flattened_errors = errors.into_iter().flatten().collect::<Vec<_>>();

    if flattened_errors.is_empty() {
        Ok(Either::Right(value_transform(values)))
    } else {
        Ok(Either::Left(flattened_errors))
    }
}