-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdestroyed.rs
More file actions
98 lines (86 loc) · 3.21 KB
/
Copy pathdestroyed.rs
File metadata and controls
98 lines (86 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Destroyed State
//!
//! Terminal state - Environment has been destroyed
//!
//! All infrastructure resources have been released and the environment no longer
//! exists. This is the final state in the lifecycle.
//!
//! **No Valid Transitions:** This is a terminal state.
use serde::{Deserialize, Serialize};
use crate::domain::environment::state::{AnyEnvironmentState, StateTypeError};
use crate::domain::environment::Environment;
/// Terminal state - Environment has been destroyed
///
/// All infrastructure resources have been released and the environment no longer
/// exists. This is the final state in the lifecycle.
///
/// **No Valid Transitions:** This is a terminal state.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Destroyed;
// Type Erasure: Typed → Runtime conversion (into_any)
impl Environment<Destroyed> {
/// Converts typed `Environment<Destroyed>` into type-erased `AnyEnvironmentState`
#[must_use]
pub fn into_any(self) -> AnyEnvironmentState {
AnyEnvironmentState::Destroyed(self)
}
}
// Type Restoration: Runtime → Typed conversion (try_into_destroyed)
impl AnyEnvironmentState {
/// Attempts to convert `AnyEnvironmentState` to `Environment<Destroyed>`
///
/// # Errors
///
/// Returns `StateTypeError::UnexpectedState` if the environment is not in `Destroyed` state.
pub fn try_into_destroyed(self) -> Result<Environment<Destroyed>, StateTypeError> {
match self {
Self::Destroyed(env) => Ok(env),
other => Err(StateTypeError::UnexpectedState {
expected: "destroyed",
actual: other.state_name().to_string(),
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_create_destroyed_state() {
let state = Destroyed;
assert_eq!(state, Destroyed);
}
mod conversion_tests {
use super::*;
use crate::adapters::ssh::SshCredentials;
use crate::domain::environment::name::EnvironmentName;
use crate::shared::Username;
use std::path::PathBuf;
fn create_test_ssh_credentials() -> SshCredentials {
let username = Username::new("test-user".to_string()).unwrap();
SshCredentials::new(
PathBuf::from("/tmp/test_key"),
PathBuf::from("/tmp/test_key.pub"),
username,
)
}
fn create_test_environment_destroyed() -> Environment<Destroyed> {
let name = EnvironmentName::new("test-env".to_string()).unwrap();
let ssh_creds = create_test_ssh_credentials();
Environment::new(name, ssh_creds, 22).destroy()
}
#[test]
fn it_should_convert_destroyed_environment_into_any() {
let env = create_test_environment_destroyed();
let any_env = env.into_any();
assert!(matches!(any_env, AnyEnvironmentState::Destroyed(_)));
}
#[test]
fn it_should_convert_any_to_destroyed_successfully() {
let env = create_test_environment_destroyed();
let any_env = env.into_any();
let result = any_env.try_into_destroyed();
assert!(result.is_ok());
}
}
}