about summary refs log tree commit diff
path: root/tvix/glue/src/builtins/utils.rs
blob: 586169beeb6992f4f41775bf928354a29712d80c (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
use bstr::ByteSlice;
use tvix_eval::{
    generators::{self, GenCo},
    CatchableErrorKind, CoercionKind, ErrorKind, NixAttrs, NixString, Value,
};

pub(super) async fn strong_importing_coerce_to_string(
    co: &GenCo,
    val: Value,
) -> Result<NixString, CatchableErrorKind> {
    let val = generators::request_force(co, val).await;
    generators::request_string_coerce(
        co,
        val,
        CoercionKind {
            strong: true,
            import_paths: true,
        },
    )
    .await
}

pub(super) async fn select_string(
    co: &GenCo,
    attrs: &NixAttrs,
    key: &str,
) -> Result<Result<Option<String>, CatchableErrorKind>, ErrorKind> {
    if let Some(attr) = attrs.select(key) {
        match strong_importing_coerce_to_string(co, attr.clone()).await {
            Err(cek) => return Ok(Err(cek)),
            Ok(str) => return Ok(Ok(Some(str.to_str()?.to_owned()))),
        }
    }

    Ok(Ok(None))
}