Skip to main content

steer_workspace/local/
environment.rs

1use async_trait::async_trait;
2use std::path::PathBuf;
3
4use crate::error::EnvironmentManagerResult;
5use crate::manager::{
6    CreateEnvironmentRequest, EnvironmentDeletePolicy, EnvironmentDescriptor, EnvironmentManager,
7};
8use crate::{EnvironmentId, EnvironmentInfo};
9
10/// Local environment manager (single implicit environment).
11#[derive(Debug, Clone)]
12pub struct LocalEnvironmentManager {
13    root: PathBuf,
14    environment_id: EnvironmentId,
15}
16
17impl LocalEnvironmentManager {
18    pub fn new(root: PathBuf) -> Self {
19        Self {
20            root,
21            environment_id: EnvironmentId::local(),
22        }
23    }
24
25    pub fn environment_id(&self) -> EnvironmentId {
26        self.environment_id
27    }
28}
29
30#[async_trait]
31impl EnvironmentManager for LocalEnvironmentManager {
32    async fn create_environment(
33        &self,
34        _request: CreateEnvironmentRequest,
35    ) -> EnvironmentManagerResult<EnvironmentDescriptor> {
36        Ok(EnvironmentDescriptor {
37            environment_id: self.environment_id,
38            root: self.root.clone(),
39        })
40    }
41
42    async fn get_environment(
43        &self,
44        environment_id: EnvironmentId,
45    ) -> EnvironmentManagerResult<EnvironmentDescriptor> {
46        if environment_id != self.environment_id {
47            return Err(crate::error::EnvironmentManagerError::NotFound(
48                environment_id.as_uuid().to_string(),
49            ));
50        }
51
52        Ok(EnvironmentDescriptor {
53            environment_id: self.environment_id,
54            root: self.root.clone(),
55        })
56    }
57
58    async fn delete_environment(
59        &self,
60        _environment_id: EnvironmentId,
61        _policy: EnvironmentDeletePolicy,
62    ) -> EnvironmentManagerResult<()> {
63        // Local environments are implicit and not deletable.
64        Err(crate::error::EnvironmentManagerError::NotSupported(
65            "local environment cannot be deleted".to_string(),
66        ))
67    }
68
69    async fn environment_info(
70        &self,
71        environment_id: EnvironmentId,
72    ) -> EnvironmentManagerResult<EnvironmentInfo> {
73        if environment_id != self.environment_id {
74            return Err(crate::error::EnvironmentManagerError::NotFound(
75                environment_id.as_uuid().to_string(),
76            ));
77        }
78
79        Ok(EnvironmentInfo::collect_for_path(&self.root)?)
80    }
81}