Skip to content

Commit

Permalink
Helper to bulk generate prompts from exposure
Browse files Browse the repository at this point in the history
- Provide a helper that will bulk generate prompts for all paths, along
  with the intermediate ctrls that lead up to that.
- Turns out providing fully owned structs/consuming versions is needed.
- While the design works it took too much effort to figure out a viable
  approach like this one, so maybe a rewrite of the core to use some
  form of arena allocation as an intermediate persistent layer where a
  single source of truth will be some running server that sits between
  the backing store and the client.
  • Loading branch information
metatoaster committed Dec 13, 2024
1 parent 937c2a0 commit e8f28fa
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 9 deletions.
11 changes: 9 additions & 2 deletions pmrctrl/src/handle/exposure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ use pmrcore::exposure::ExposureRef;
use pmrrepo::handle::GitHandle;
use std::{
collections::HashMap,
sync::Arc,
sync::{
Arc,
OnceLock,
},
};

use crate::{
platform::Platform,
handle::ExposureFileCtrl,
handle::{
EFViewTaskTemplatesCtrl,
ExposureFileCtrl,
},
};

pub(crate) struct RawExposureCtrl<'p> {
pub(crate) platform: &'p Platform,
pub(crate) git_handle: GitHandle<'p>,
pub(crate) exposure: ExposureRef<'p>,
pub(crate) exposure_file_ctrls: Arc<Mutex<HashMap<String, ExposureFileCtrl<'p>>>>,
pub(crate) efvttcs: OnceLock<Vec<(String, Option<EFViewTaskTemplatesCtrl<'p>>)>>,
}

pub struct ExposureCtrl<'p>(pub(crate) Arc<RawExposureCtrl<'p>>);
Expand Down
78 changes: 74 additions & 4 deletions pmrctrl/src/handle/exposure/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@ use pmrcore::{
ExposureFileRef,
},
};
use pmrmodel::model::{
profile::UserPromptGroupRefs,
task_template::UserArgRefs,
};
use pmrrepo::handle::GitHandle;
use std::{
collections::HashMap,
ops::Deref,
path::PathBuf,
sync::Arc,
sync::{
Arc,
OnceLock,
},
};

use crate::{
handle::{
EFViewTaskTemplatesCtrl,
ExposureCtrl,
ExposureFileCtrl,
ExposureFileViewCtrl,
Expand Down Expand Up @@ -52,6 +60,7 @@ impl<'p> ExposureCtrl<'p> {
git_handle,
exposure,
exposure_file_ctrls: Arc::new(Mutex::new(HashMap::new())),
efvttcs: OnceLock::new(),
}))
}

Expand Down Expand Up @@ -135,15 +144,15 @@ impl<'p> ExposureCtrl<'p> {
/// being provided.
pub async fn ctrl_path(
&'p self,
workspace_file_path: &'p str,
workspace_file_path: impl AsRef<str> + ToString,
) -> Result<
ExposureFileCtrl<'p>,
PlatformError
> {
// quick failing here.
let pathinfo = self.0.git_handle.pathinfo(
Some(self.0.exposure.commit_id()),
Some(workspace_file_path),
Some(workspace_file_path.as_ref()),
)?;
// FIXME What if pathinfo is a tree? There is currently no way
// to provide a ctrl for that.
Expand All @@ -152,7 +161,7 @@ impl<'p> ExposureCtrl<'p> {
// TODO need to check if already present in exposure_file_ctrls
let exposure_file = self.0.platform.mc_platform.get_exposure_file_by_id_path(
self.0.exposure.id(),
workspace_file_path,
workspace_file_path.as_ref(),
).await?;
let exposure_file = ExposureFileCtrl::new(
self.0.platform,
Expand Down Expand Up @@ -321,6 +330,67 @@ impl<'p> ExposureCtrl<'p> {
)
}

pub async fn list_files_efcs(
&'p self,
) -> Result<Vec<(String, Option<ExposureFileCtrl<'p>>)>, PlatformError> {
let mut result = Vec::new();
for (path, cond) in self.list_files_info().await?.into_iter() {
let item = if cond {
Some(self.ctrl_path(&path).await?)
} else {
None
};
result.push((path, item));
}
Ok(result)
}

pub async fn list_files_efvttcs(
&'p self,
) -> Result<&'p [(String, Option<EFViewTaskTemplatesCtrl<'p>>)], PlatformError> {
Ok(match self.0.efvttcs.get() {
Some(efvttcs) => efvttcs,
None => {
let efcs = self.list_files_efcs().await?;
let mut efvttcs = Vec::new();
for (path, value) in efcs.into_iter() {
let item = if let Some(efc) = value {
Some(efc.try_into_vttc().await?)
} else {
None
};
efvttcs.push((path, item))
}
self.0.efvttcs.set(efvttcs)
.unwrap_or_else(|_| log::warn!(
"concurrent call to the same \
ExposureCtrl.list_files_efvttcs()"
));
self.0.efvttcs.get()
.expect("efvttsc has just been set!")
}
})
}

pub async fn list_files_prompts(
&'p self,
) -> Result<Vec<(&'p str, Option<(UserPromptGroupRefs<'p>, UserArgRefs<'p>)>)>, PlatformError> {
let efvttsc = self.list_files_efvttcs().await?;
let mut result = Vec::new();
for (path, value) in efvttsc.iter() {
let item = if let Some(efvttsc) = value {
Some((
efvttsc.create_user_prompt_groups()?,
efvttsc.create_user_arg_refs()?,
))
} else {
None
};
result.push((path.as_str(), item))
}
Ok(result)
}

pub fn exposure(&self) -> &ExposureRef<'p> {
&self.0.exposure
}
Expand Down
14 changes: 13 additions & 1 deletion pmrctrl/src/handle/exposure_file/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ impl<'p> ExposureFileCtrl<'p> {
/// this particular instance of ExposureFileCtrl.
pub async fn build_vttc(
&'p self,
) -> Result<EFViewTaskTemplatesCtrl<'p>, PlatformError> {
self.clone().try_into_vttc().await
}

/// Try into a EFViewTaskTemplatesCtrl.
///
/// This could be an impl on async TryFrom.
///
/// Note that this would freeze the view templates associated with
/// this particular instance of ExposureFileCtrl.
pub async fn try_into_vttc(
self,
) -> Result<EFViewTaskTemplatesCtrl<'p>, PlatformError> {
let mut vtts = ExposureTaskTemplateBackend::get_file_templates(
self.0.platform.mc_platform.as_ref(),
Expand All @@ -188,7 +200,7 @@ impl<'p> ExposureFileCtrl<'p> {
))
})).await?;
Ok(EFViewTaskTemplatesCtrl::new(
self.clone(),
self,
vtts.into(),
))
}
Expand Down
2 changes: 1 addition & 1 deletion pmrctrl/tests/pmrctrl_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1749,5 +1749,5 @@ fn test_send_sync_ctrl() {
is_send_sync::<pmrctrl::handle::ExposureCtrl>();
is_send_sync::<pmrctrl::handle::ExposureFileCtrl>();
is_send_sync::<pmrctrl::handle::ExposureFileViewCtrl>();
// is_send_sync::<pmrctrl::handle::EFViewTaskTemplatesCtrl>();
is_send_sync::<pmrctrl::handle::EFViewTaskTemplatesCtrl>();
}
2 changes: 1 addition & 1 deletion pmrmodel/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod cache;
pub use crate::registry::prepared::PreparedChoiceRegistry;
pub use crate::registry::cache::ChoiceRegistryCache;

pub trait ChoiceRegistry<T> {
pub trait ChoiceRegistry<T>: Send + Sync {
fn register(&mut self, name: &str, registry: T);
fn select_keys(&mut self, name: &str, keys: Vec<String>);
fn lookup<'a>(&'a self, name: &str) -> Option<MapToArgRef<'a>>;
Expand Down

0 comments on commit e8f28fa

Please sign in to comment.