forked from oscope-dev/scope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix.rs
144 lines (128 loc) · 3.79 KB
/
fix.rs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use anyhow::Result;
use std::path::Path;
use crate::{
prelude::{DoctorFixPromptSpec, DoctorFixSpec},
shared::prelude::*,
};
use derive_builder::Builder;
#[derive(Debug, PartialEq, Clone, Builder)]
#[builder(setter(into))]
pub struct DoctorFix {
#[builder(default)]
pub command: Option<DoctorCommand>,
#[builder(default)]
pub help_text: Option<String>,
#[builder(default)]
pub help_url: Option<String>,
#[builder(default)]
pub prompt: Option<DoctorFixPrompt>,
}
impl DoctorFix {
pub fn empty() -> Self {
DoctorFix {
command: None,
help_text: None,
help_url: None,
prompt: None,
}
}
pub fn from_spec(containing_dir: &Path, working_dir: &str, fix: DoctorFixSpec) -> Result<Self> {
let commands = DoctorCommand::from_commands(containing_dir, working_dir, &fix.commands)?;
let help_text = fix
.help_text
.as_ref()
.map(|st| st.trim().to_string())
.clone();
let help_url = fix.help_url.clone();
let prompt = fix.prompt.map(DoctorFixPrompt::from);
Ok(DoctorFix {
command: Some(commands),
help_text,
help_url,
prompt,
})
}
}
#[derive(Debug, PartialEq, Clone, Builder)]
#[builder(setter(into))]
pub struct DoctorFixPrompt {
#[builder(default)]
pub text: String,
#[builder(default)]
pub extra_context: Option<String>,
}
impl From<DoctorFixPromptSpec> for DoctorFixPrompt {
fn from(value: DoctorFixPromptSpec) -> Self {
DoctorFixPrompt {
text: value.text,
extra_context: value.extra_context,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn prompt_from_spec() {
let spec = DoctorFixPromptSpec {
text: "do you want to do the thing?".to_string(),
extra_context: None,
};
let actual = DoctorFixPrompt::from(spec);
assert_eq!(
DoctorFixPrompt {
text: "do you want to do the thing?".to_string(),
extra_context: None
},
actual
)
}
#[test]
fn empty_returns_a_fix_full_of_none() {
// I can argue that we should use Option<DoctorFix> instead,
// but for now, this is where we're at.
assert_eq!(
DoctorFix {
command: None,
help_text: None,
help_url: None,
prompt: None,
},
DoctorFix::empty()
)
}
#[test]
fn from_spec_translates_to_fix() {
let spec = DoctorFixSpec {
commands: vec![
"some/command",
"./other_command",
"{{ working_dir }}/.foo.sh",
]
.iter()
.map(|cmd| cmd.to_string())
.collect(),
help_text: Some("text".to_string()),
help_url: Some("https.example.com".to_string()),
prompt: Some(DoctorFixPromptSpec {
text: "do you want to do the thing?".to_string(),
extra_context: Some("additional context".to_string()),
}),
};
let expected = DoctorFix {
command: Some(
DoctorCommand::from_commands(
Path::new("/some/dir"),
"/some/work/dir",
&spec.commands,
)
.unwrap(),
),
help_text: spec.help_text.clone(),
help_url: spec.help_url.clone(),
prompt: Some(DoctorFixPrompt::from(spec.prompt.clone().unwrap())),
};
let actual = DoctorFix::from_spec(Path::new("/some/dir"), "/some/work/dir", spec).unwrap();
assert_eq!(expected, actual)
}
}