Skip to content

Commit 09f2adc

Browse files
committed
Add locate funcs.
1 parent 15dc038 commit 09f2adc

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

crates/pdk/src/toolchain.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use extism_pdk::{config, json};
22
use moon_common::Id;
3+
use moon_pdk_api::VirtualPath;
34
use moon_project::ProjectFragment;
45
use serde::de::DeserializeOwned;
56
use warpgate_pdk::{AnyResult, get_plugin_id};
@@ -34,7 +35,13 @@ pub fn parse_toolchain_config_schema<T: schematic::Config>(value: json::Value) -
3435
use schematic::{ConfigLoader, Format};
3536

3637
match ConfigLoader::<T>::new()
37-
.code(json::to_string(&value)?, Format::Json)?
38+
.code(
39+
match value {
40+
json::Value::Null => "{}".to_owned(),
41+
_ => json::to_string(&value)?,
42+
},
43+
Format::Json,
44+
)?
3845
.load()
3946
{
4047
Ok(result) => Ok(result.config),
@@ -54,3 +61,41 @@ pub fn is_project_toolchain_enabled(project: &ProjectFragment) -> bool {
5461
pub fn is_project_toolchain_enabled_for(project: &ProjectFragment, id: impl AsRef<str>) -> bool {
5562
project.toolchains.contains(&Id::raw(id.as_ref()))
5663
}
64+
65+
/// Locate the root directory that contains the provided file name,
66+
/// by traversing upwards from the starting directory.
67+
pub fn locate_root(starting_dir: &VirtualPath, file_name: &str) -> Option<VirtualPath> {
68+
let mut current_dir = Some(starting_dir.to_owned());
69+
70+
while let Some(dir) = current_dir {
71+
if dir.join(file_name).exists() {
72+
return Some(dir);
73+
}
74+
75+
current_dir = dir.parent();
76+
}
77+
78+
None
79+
}
80+
81+
/// Locate the root directory that contains the provided file name,
82+
/// by traversing upwards from the starting directory and running
83+
/// the check function on each directory. If the check returns true,
84+
/// the traversal will stop.
85+
pub fn locate_root_with_check(
86+
starting_dir: &VirtualPath,
87+
file_name: &str,
88+
mut check: impl FnMut(&VirtualPath) -> AnyResult<bool>,
89+
) -> AnyResult<()> {
90+
let mut current_dir = Some(starting_dir.to_owned());
91+
92+
while let Some(dir) = current_dir {
93+
if dir.join(file_name).exists() && check(&dir)? {
94+
return Ok(());
95+
}
96+
97+
current_dir = dir.parent();
98+
}
99+
100+
Ok(())
101+
}

0 commit comments

Comments
 (0)