use std::{io, path::PathBuf, sync::Arc};
use tempfile::{TempDir, tempdir};
#[derive(Debug, Clone)]
pub struct StateStore {
root: PathBuf,
_temp_dir_drop: Option<Arc<TempDir>>,
}
impl StateStore {
fn from_path(root: impl Into<PathBuf>) -> Self {
Self {
root: root.into(),
_temp_dir_drop: None,
}
}
pub fn temp() -> Result<Self, io::Error> {
let temp_dir = tempdir()?;
Ok(Self {
root: temp_dir.path().to_path_buf(),
_temp_dir_drop: Some(Arc::new(temp_dir)),
})
}
pub fn bucket(&self, state_bucket: StateBucket) -> PathBuf {
self.root.join(state_bucket.to_str())
}
pub fn from_settings(state_dir: Option<PathBuf>) -> Result<Self, io::Error> {
if let Some(state_dir) = state_dir {
Ok(Self::from_path(state_dir))
} else if let Some(data_dir) = uv_dirs::legacy_user_state_dir().filter(|dir| dir.exists()) {
Ok(Self::from_path(data_dir))
} else if let Some(data_dir) = uv_dirs::user_state_dir() {
Ok(Self::from_path(data_dir))
} else {
Ok(Self::from_path(".uv"))
}
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum StateBucket {
ManagedPython,
Tools,
Credentials,
}
impl StateBucket {
fn to_str(self) -> &'static str {
match self {
Self::ManagedPython => "python",
Self::Tools => "tools",
Self::Credentials => "credentials",
}
}
}