use crate::path_ref::PathRef;
#[derive(Clone, Debug)]
pub struct FavoriteRef(pub PathRef);
impl std::str::FromStr for FavoriteRef {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse::<PathRef>().map(FavoriteRef)
}
}
impl std::fmt::Display for FavoriteRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl FavoriteRef {
pub async fn resolve<F, Fut>(
self,
get_favorites: F,
) -> Result<objectiveai_sdk::RemotePathCommitOptional, crate::error::Error>
where
F: FnOnce() -> Fut,
Fut: std::future::Future<Output = Vec<objectiveai_sdk::filesystem::config::Favorite>>,
{
if let Some(fav_name) = self.0.favorite {
let favorites = get_favorites().await;
let fav = favorites
.into_iter()
.find(|f| f.get_name() == fav_name)
.ok_or_else(|| crate::error::Error::FavoriteNotFound(fav_name))?;
Ok(fav.path.clone())
} else {
self.0.resolve()
}
}
}