Skip to content

Commit f60b75b

Browse files
committed
fixed lifetime issue
1 parent 29ca810 commit f60b75b

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

src/parse/daily_menu/daily_menu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod tests {
2525
let html =
2626
fs::read_to_string("./src/parse/html_examples/daily_menu/meals_on_date.html").unwrap();
2727
let document = scraper::Html::parse_document(&html);
28-
let meals = DailyMenu::from_html_element(document.root_element())
28+
let meals = DailyMenu::from_html_element(&document.root_element())
2929
.expect("The example html should be valid");
3030
assert_eq!(
3131
meals.date,

src/parse/locations/location.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::borrow::Cow;
1+
use std::borrow::{Borrow, Cow};
2+
use std::sync::Arc;
23

34
use crate::parse::daily_menu::DailyMenu;
45
use crate::parse::Error;
@@ -11,6 +12,7 @@ pub(super) struct Location<'a> {
1112
name: Cow<'a, str>,
1213
id: String, // ex. 40 for 9/10
1314
daily_menus: Vec<DailyMenu<'a>>,
15+
html: Arc<Html>,
1416
}
1517

1618
impl<'a> Location<'a> {
@@ -24,23 +26,23 @@ impl<'a> Location<'a> {
2426
};
2527

2628
// TODO: make static
27-
let url = Url::parse("https://nutrition.sa.ucsc.edu").expect("base url is invalid!");
28-
let Ok(url) = url.join(
29-
location_element
30-
.attr("href")
31-
.ok_or(Error::html_parse_error(
32-
"location <a> does not have a href attr",
33-
))?,
34-
) else {
29+
let url = Url::parse("https://nutrition.sa.ucsc.edu").expect("base url should be valid!");
30+
let Ok(url) =
31+
url.join(location_element.attr("href").ok_or_else(|| {
32+
Error::html_parse_error("location <a> does not have a href attr")
33+
})?)
34+
else {
3535
return Err(Error::html_parse_error("Location url is invalid"));
3636
};
3737

3838
let id = url
3939
.query_pairs()
4040
.find(|x| x.0 == "locationNum")
41-
.ok_or(Error::html_parse_error(
42-
"Location url does not include the `locationNum` query parameter",
43-
))?
41+
.ok_or_else(|| {
42+
Error::html_parse_error(
43+
"Location url does not include the `locationNum` query parameter",
44+
)
45+
})?
4446
.1
4547
.into_owned();
4648

@@ -54,18 +56,35 @@ impl<'a> Location<'a> {
5456
if name.is_empty() {
5557
return Err(Error::html_parse_error("Location name is missing"));
5658
}
57-
58-
let menu = client.get(url).header("Cookie", format!("WebInaCartDates=; WebInaCartLocation={id}; WebInaCartMeals=; WebInaCartQtys=; WebInaCartRecipes=")).send().await?.text().await?;
59+
let mut cookies = String::with_capacity(100);
60+
cookies.push_str("WebInaCartDates=; ");
61+
cookies.push_str("WebInaCartLocation=");
62+
cookies.push_str(&id);
63+
cookies.push_str("; WebInaCartMeals=; WebInaCartQtys=; WebInaCartRecipes=");
64+
let menu = client
65+
.get(url)
66+
.header("Cookie", cookies)
67+
.send()
68+
.await?
69+
.text()
70+
.await?;
71+
let menu = Arc::new(menu);
5972

6073
let html = Html::parse_document(&menu);
6174

62-
// let daily_menus = DailyMenu::from_html_element(html.root_element());
75+
let html = Arc::new(html);
6376

64-
Ok(Self {
77+
let out = Self {
6578
name,
6679
id,
67-
daily_menus: vec![], // TODO
68-
})
80+
daily_menus: vec![],
81+
html,
82+
};
83+
84+
let daily_menu = DailyMenu::from_html_element(out.html.root_element())?;
85+
let daily_menus = vec![daily_menu];
86+
87+
Ok(out)
6988
}
7089
}
7190

0 commit comments

Comments
 (0)