-
Notifications
You must be signed in to change notification settings - Fork 5
/
eval.rs
37 lines (34 loc) · 1.05 KB
/
eval.rs
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
use crate::command::{CommandError, NixCmd, NixCmdError};
use super::url::FlakeUrl;
/// Run `nix eval <url> --json` and parse its JSON
///
/// If the attribute is missing, return None.
pub async fn nix_eval_attr<T>(cmd: &NixCmd, url: &FlakeUrl) -> Result<Option<T>, NixCmdError>
where
T: Default + serde::de::DeserializeOwned,
{
let result = cmd
.run_with_args_expecting_json(&["eval", &url.0, "--json"])
.await;
match result {
Ok(v) => Ok(Some(v)),
Err(err) if error_is_missing_attribute(&err) => {
Ok(None) // Attr is missing
}
Err(err) => Err(err),
}
}
/// Check that [NixCmdError] is a missing attribute error
fn error_is_missing_attribute(err: &NixCmdError) -> bool {
match err {
NixCmdError::CmdError(CommandError::ProcessFailed { stderr, .. }) => {
if let Some(stderr) = stderr {
if stderr.contains("does not provide attribute") {
return true;
}
}
false
}
_ => false,
}
}