Skip to content

Commit 5ded604

Browse files
authored
Sheets cell functions don't return Results (#28)
They no longer actually return errors, so stop callers from having to handle those errors.
1 parent c0a82cc commit 5ded604

5 files changed

Lines changed: 27 additions & 63 deletions

File tree

src/github_accounts.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,15 @@ fn trainees_from_sheet(sheet: &Sheet) -> Result<BTreeMap<GithubLogin, Trainee>,
7878
return Err(Error::Fatal(anyhow::anyhow!("Reading trainee data from Google Sheets API, row {} didn't have at least 5 columns", row_index)));
7979
}
8080

81-
let github_login = GithubLogin::from(
82-
cell_string(&cells[3]).context("Failed to read trainee github login")?,
83-
);
81+
let github_login = GithubLogin::from(cell_string(&cells[3]));
8482

85-
let email = cell_string(&cells[4]).context("Failed to read trainee email")?;
83+
let email = cell_string(&cells[4]);
8684

8785
trainees.insert(
8886
github_login.clone(),
8987
Trainee {
90-
name: cell_string(&cells[1]).context("Failed to read trainee name")?,
91-
region: Region(
92-
cell_string(&cells[2]).context("Failed to read trainee region")?,
93-
),
88+
name: cell_string(&cells[1]),
89+
region: Region(cell_string(&cells[2])),
9490
github_login,
9591
email: new_case_insensitive_email_address(&email)
9692
.with_context(|| format!("Failed to parse trainee email {}", email))?,

src/mentoring.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,7 @@ pub async fn get_mentoring_records(
9494
continue;
9595
}
9696
if row_number == 0 {
97-
let headings = cells
98-
.iter()
99-
.take(6)
100-
.enumerate()
101-
.map(|(col_number, cell)| {
102-
cell_string(cell)
103-
.with_context(|| format!("Failed to get row 0 column {}", col_number))
104-
})
105-
.collect::<Result<Vec<_>, _>>()?;
97+
let headings = cells.iter().take(6).map(cell_string).collect::<Vec<_>>();
10698
if headings != ["Name", "Region", "Date", "Staff", "Status", "Notes"] {
10799
return Err(Error::Fatal(anyhow::anyhow!(
108100
"Mentoring data sheet contained wrong headings: {}",
@@ -113,8 +105,7 @@ pub async fn get_mentoring_records(
113105
if cells[0].effective_value.is_none() {
114106
break;
115107
}
116-
let name = cell_string(&cells[0])
117-
.with_context(|| format!("Failed to read name from row {}", row_number + 1))?;
108+
let name = cell_string(&cells[0]);
118109
let date = cell_date(&cells[2])
119110
.with_context(|| format!("Failed to parse date from row {}", row_number + 1))?;
120111
let entry = mentoring_records.records.entry(name);

src/register.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ fn read_module(
125125
for (row_number, row) in data.row_data.into_iter().enumerate() {
126126
let cells = row.values;
127127
// Some sheets have documentation or pivot table
128-
if row_number == 0
129-
&& !cells.is_empty()
130-
&& cell_string(&cells[0]).unwrap_or_default() != "Name"
131-
{
128+
if row_number == 0 && !cells.is_empty() && cell_string(&cells[0]) != "Name" {
132129
continue 'sheet;
133130
}
134131
if cells.len() < 7 {
@@ -140,15 +137,7 @@ fn read_module(
140137
));
141138
}
142139
if row_number == 0 {
143-
let headings = cells
144-
.iter()
145-
.take(7)
146-
.enumerate()
147-
.map(|(col_number, cell)| {
148-
cell_string(cell)
149-
.with_context(|| format!("Failed to get row 0 column {}", col_number))
150-
})
151-
.collect::<Result<Vec<_>, _>>()?;
140+
let headings = cells.iter().take(7).map(cell_string).collect::<Vec<_>>();
152141
if headings
153142
!= [
154143
"Name",
@@ -200,18 +189,13 @@ fn read_row(
200189
cells: &[CellData],
201190
register_url: String,
202191
) -> Result<(usize, Attendance), anyhow::Error> {
203-
let sprint_number = extract_sprint_number(
204-
&cell_string(&cells[5]).context("Couldn't get sprint value from column 5")?,
205-
)?;
206-
let name = cell_string(&cells[0]).context("Failed to read name")?;
207-
let email = new_case_insensitive_email_address(
208-
&cell_string(&cells[1]).context("Failed to read email")?,
209-
)?;
210-
let timestamp =
211-
DateTime::parse_from_rfc3339(&cell_string(&cells[2]).context("Failed to read timestamp")?)
212-
.context("Failed to parse timestamp")?
213-
.to_utc();
214-
let region = cell_string(&cells[6]).context("Failed to read region")?;
192+
let sprint_number = extract_sprint_number(&cell_string(&cells[5]))?;
193+
let name = cell_string(&cells[0]);
194+
let email = new_case_insensitive_email_address(&cell_string(&cells[1]))?;
195+
let timestamp = DateTime::parse_from_rfc3339(&cell_string(&cells[2]))
196+
.context("Failed to parse timestamp")?
197+
.to_utc();
198+
let region = cell_string(&cells[6]);
215199
Ok((
216200
sprint_number,
217201
Attendance {

src/reviewer_staff_info.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::BTreeMap;
22

3-
use anyhow::Context;
43
use sheets::types::Sheet;
54

65
use crate::{
@@ -70,19 +69,14 @@ fn reviewer_staff_detail_from_sheet(
7069
continue;
7170
}
7271

73-
let github_login = GithubLogin::from(
74-
cell_string(&cells[0]).context("Failed to read reviewer github login")?,
75-
);
72+
let github_login = GithubLogin::from(cell_string(&cells[0]));
7673

7774
let notes = match cells.get(6) {
78-
Some(cell) => cell_string(cell).context("Failed to read reviewer notes")?,
75+
Some(cell) => cell_string(cell),
7976
None => String::new(),
8077
};
8178

82-
let checked = match (
83-
cell_bool(&cells[3]).context("Failed to read reviewer checked")?,
84-
cell_bool(&cells[4]).context("Failed to read reviewer check again")?,
85-
) {
79+
let checked = match (cell_bool(&cells[3]), cell_bool(&cells[4])) {
8680
(true, false) => CheckStatus::CheckedAndOk,
8781
(true, true) => CheckStatus::CheckedAndCheckAgain,
8882
(false, _) => CheckStatus::Unchecked,
@@ -91,11 +85,10 @@ fn reviewer_staff_detail_from_sheet(
9185
reviewers.insert(
9286
github_login.clone(),
9387
ReviewerStaffOnlyDetails {
94-
name: cell_string(&cells[1]).context("Failed to read reviewer name")?,
95-
attended_training: cell_bool(&cells[2])
96-
.context("Failed to read reviewer attended training state")?,
88+
name: cell_string(&cells[1]),
89+
attended_training: cell_bool(&cells[2]),
9790
checked,
98-
quality: cell_string(&cells[5]).context("Failed to read reviewer quality")?,
91+
quality: cell_string(&cells[5]),
9992
notes,
10093
},
10194
);

src/sheets.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ use crate::{
88
Error, ServerState,
99
};
1010

11-
pub(crate) fn cell_string(cell: &CellData) -> Result<String, anyhow::Error> {
11+
pub(crate) fn cell_string(cell: &CellData) -> String {
1212
let value = cell.effective_value.clone();
1313
if let Some(value) = value {
14-
Ok(value.string_value)
14+
value.string_value
1515
} else {
16-
Ok(String::new())
16+
String::new()
1717
}
1818
}
1919

20-
pub(crate) fn cell_bool(cell: &CellData) -> Result<bool, anyhow::Error> {
20+
pub(crate) fn cell_bool(cell: &CellData) -> bool {
2121
let value = cell.effective_value.clone();
2222
if let Some(value) = value {
23-
Ok(value.bool_value)
23+
value.bool_value
2424
} else {
25-
Ok(false)
25+
false
2626
}
2727
}
2828

0 commit comments

Comments
 (0)