Skip to content

Commit 3d4698d

Browse files
committed
refactor: Add LifeCycle
1 parent d3934f2 commit 3d4698d

File tree

4 files changed

+106
-15
lines changed

4 files changed

+106
-15
lines changed

src/commands/update/lifecycle.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use super::event::Event;
2+
use uuid;
3+
use uuid::Uuid;
4+
5+
pub struct LifeCycle {
6+
id: Uuid,
7+
file_name: String,
8+
}
9+
10+
impl LifeCycle {
11+
pub(crate) fn new(file_name: &str) -> Self {
12+
Self {
13+
id: Uuid::new_v4(),
14+
file_name: file_name.to_string(),
15+
}
16+
}
17+
18+
pub(crate) fn start(&self) -> Event {
19+
Event::Started(self.id, self.file_name.clone())
20+
}
21+
22+
pub(crate) const fn succeed(&self) -> Event {
23+
Event::Succeeded(self.id)
24+
}
25+
26+
pub(crate) fn fail(&self, reason: &str) -> Event {
27+
Event::Failed(self.id, reason.to_string())
28+
}
29+
}
30+
31+
#[cfg(test)]
32+
mod tests {
33+
use super::Event;
34+
use super::*;
35+
36+
#[test]
37+
fn start_returns_a_started_event() {
38+
let life_cycle = LifeCycle::new("file.txt");
39+
assert!(
40+
matches!(life_cycle.start(), Event::Started(_, file_name) if file_name == "file.txt")
41+
);
42+
}
43+
44+
#[test]
45+
fn succeed_returns_a_succeed_event() {
46+
let life_cycle = LifeCycle::new("file.txt");
47+
assert!(matches!(life_cycle.succeed(), Event::Succeeded(_)));
48+
}
49+
50+
#[test]
51+
fn fail_returns_a_fail_event() {
52+
let life_cycle = LifeCycle::new("file.txt");
53+
assert!(
54+
matches!(life_cycle.fail("error message"), Event::Failed(_, reason) if reason == "error message")
55+
);
56+
}
57+
58+
#[test]
59+
#[allow(clippy::panic)]
60+
fn new_returns_instances_with_unique_ids() {
61+
let life_cycle1 = LifeCycle::new("file1.txt");
62+
let life_cycle2 = LifeCycle::new("file2.txt");
63+
match (life_cycle1.start(), life_cycle2.start()) {
64+
(Event::Started(id1, _), Event::Started(id2, _)) => {
65+
assert_ne!(id1, id2);
66+
}
67+
_ => panic!("Unexpected events returned from start()"),
68+
}
69+
}
70+
71+
#[test]
72+
#[allow(clippy::panic)]
73+
fn ids_are_consistent_within_a_lifecycle() {
74+
let life_cycle = LifeCycle::new("file.txt");
75+
let started = life_cycle.start();
76+
let succeeded = life_cycle.succeed();
77+
let failed = life_cycle.fail("error");
78+
79+
match (started, succeeded, failed) {
80+
(
81+
Event::Started(start_id, _),
82+
Event::Succeeded(succeed_id),
83+
Event::Failed(failed_id, _),
84+
) => {
85+
assert_eq!(start_id, succeed_id);
86+
assert_eq!(start_id, failed_id);
87+
}
88+
_ => panic!("Unexpected events returned from start()"),
89+
}
90+
}
91+
}

src/commands/update/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
mod command;
22
mod event;
3+
mod lifecycle;
34
mod state;
45
mod updater;
56

67
pub use command::Command;
78
pub use event::{Event, Stream as EventStream};
9+
pub use lifecycle::LifeCycle;
810
pub use updater::Updater;

src/location_types/cargo/location.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use std::process::{Child, Command, ExitStatus, Stdio};
33

44
use anyhow::anyhow;
55
use async_stream::stream;
6-
use uuid::Uuid;
76

87
use crate::commands::update;
9-
use crate::commands::update::Updater;
8+
use crate::commands::update::{LifeCycle, Updater};
109

1110
pub struct Location;
1211

@@ -19,17 +18,17 @@ impl Location {
1918
impl Updater for Location {
2019
fn update(&self, version: String) -> update::EventStream {
2120
Box::pin(stream! {
22-
let toml_id = Uuid::new_v4();
23-
let lock_id = Uuid::new_v4();
24-
yield update::Event::Started(toml_id, "Cargo.toml".to_string());
21+
let toml_life_cycle = LifeCycle::new("Cargo.toml");
22+
yield toml_life_cycle.start();
2523
match update_cargo_version(&version) {
2624
Ok(()) => {
27-
yield update::Event::Succeeded(toml_id);
28-
yield update::Event::Started(lock_id, "Cargo.lock".to_string());
29-
yield update::Event::Succeeded(lock_id);
25+
yield toml_life_cycle.succeed();
26+
let lock_life_cycle = LifeCycle::new("Cargo.lock");
27+
yield lock_life_cycle.start();
28+
yield lock_life_cycle.succeed();
3029
}
3130
Err(err) => {
32-
yield update::Event::Failed(toml_id, err.to_string());
31+
yield toml_life_cycle.fail(&err.to_string())
3332
}
3433
}
3534
})

src/location_types/string_pattern/location.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::path::Path;
22

33
use async_stream::stream;
4-
use uuid::Uuid;
54

6-
use crate::commands::update::{Event, EventStream, Updater};
5+
use crate::commands::update::{EventStream, LifeCycle, Updater};
76

87
use super::config::Config;
98
use super::pattern::Pattern;
@@ -25,11 +24,11 @@ impl Updater for Location {
2524
let config = self.config.clone();
2625

2726
Box::pin(stream! {
28-
let id = Uuid::new_v4();
29-
yield Event::Started(id, config.file.clone());
27+
let life_cycle = LifeCycle::new(&config.file);
28+
yield life_cycle.start();
3029
match replace_version(&config, &version) {
31-
Ok(()) => yield Event::Succeeded(id),
32-
Err(err) => yield Event::Failed(id, err.to_string()),
30+
Ok(()) => yield life_cycle.succeed(),
31+
Err(err) => yield life_cycle.fail(&err.to_string()),
3332
}
3433
})
3534
}

0 commit comments

Comments
 (0)