aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2021-05-22 19:58:09 +0200
committerTeddy Wing2021-05-22 19:58:09 +0200
commit85f6e0a660439ca7ce0e42220fab9491d2d5a857 (patch)
treeb619aeeee2f2ab38e7f55c445b6d234661a851a4 /src
parent79f53af864312e4171aaa047a7dabd46375b2987 (diff)
downloadgoogle-calendar-rsvp-85f6e0a660439ca7ce0e42220fab9491d2d5a857.tar.bz2
eid_from_email(): Return a `Result`
Diffstat (limited to 'src')
-rw-r--r--src/main.rs20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 8428b90..f46c745 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -95,7 +95,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
let mut email_input: Vec<u8> = Vec::new();
stdin.read_to_end(&mut email_input)?;
- email_eid = eid_from_email(&email_input);
+ email_eid = eid_from_email(&email_input)?;
event_ids.push(&email_eid);
}
@@ -205,26 +205,28 @@ fn event_id_from_base64(event_id: &str) -> anyhow::Result<String> {
Ok(id)
}
-fn eid_from_email(email: &[u8]) -> String {
- let email = mailparse::parse_mail(&email).unwrap();
- let re = Regex::new("eid=([^&]+)&").unwrap();
+fn eid_from_email(email: &[u8]) -> anyhow::Result<String> {
+ let email = mailparse::parse_mail(&email)?;
+ let re = Regex::new("eid=([^&]+)&")?;
// Assume email is multipart/alternative.
for part in &email.subparts {
if part.ctype.mimetype == "multipart/alternative" {
for part in &part.subparts {
if part.ctype.mimetype == "text/plain" {
- let body = part.get_body().unwrap();
- let captures = re.captures(&body).unwrap();
- let eid = captures.get(1).unwrap();
+ let body = part.get_body()?;
+ let captures = re.captures(&body)
+ .ok_or(anyhow::anyhow!("no matches for event ID"))?;
+ let eid = captures.get(1)
+ .ok_or(anyhow::anyhow!("event ID not found"))?;
- return eid.as_str().to_owned();
+ return Ok(eid.as_str().to_owned());
}
}
}
}
- todo!();
+ Err(anyhow::anyhow!("unable to extract event ID from email"))
}
fn print_event(event: &Event) -> anyhow::Result<()> {