pyoxidizerlib/starlark/
util.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use {
6    starlark::values::{none::NoneType, Value},
7    std::{os::raw::c_ulong, path::PathBuf},
8};
9
10pub trait ToValue {
11    fn to_value(&self) -> Value;
12}
13
14impl ToValue for Option<bool> {
15    fn to_value(&self) -> Value {
16        match self {
17            Some(value) => Value::from(*value),
18            None => Value::from(NoneType::None),
19        }
20    }
21}
22
23impl ToValue for Option<&str> {
24    fn to_value(&self) -> Value {
25        match self {
26            Some(value) => Value::from(value.to_string()),
27            None => Value::from(NoneType::None),
28        }
29    }
30}
31
32impl ToValue for Option<String> {
33    fn to_value(&self) -> Value {
34        match self {
35            Some(value) => Value::from(value.clone()),
36            None => Value::from(NoneType::None),
37        }
38    }
39}
40
41impl ToValue for Option<PathBuf> {
42    fn to_value(&self) -> Value {
43        match self {
44            Some(value) => Value::from(format!("{}", value.display())),
45            None => Value::from(NoneType::None),
46        }
47    }
48}
49
50impl ToValue for Option<c_ulong> {
51    fn to_value(&self) -> Value {
52        match self {
53            Some(value) => Value::from((*value) as u64),
54            None => Value::from(NoneType::None),
55        }
56    }
57}
58
59impl ToValue for Option<Vec<String>> {
60    fn to_value(&self) -> Value {
61        match self {
62            Some(value) => Value::from(value.clone()),
63            None => Value::from(NoneType::None),
64        }
65    }
66}
67
68impl ToValue for Option<Vec<PathBuf>> {
69    fn to_value(&self) -> Value {
70        match self {
71            Some(value) => Value::from(
72                value
73                    .iter()
74                    .map(|x| format!("{}", x.display()))
75                    .collect::<Vec<_>>(),
76            ),
77            None => Value::from(NoneType::None),
78        }
79    }
80}