diff --git a/src/admin/model.rs b/src/admin/model.rs index f04482d..0a5141d 100644 --- a/src/admin/model.rs +++ b/src/admin/model.rs @@ -49,7 +49,7 @@ impl Admin { ":password": password, }, )?; - Ok(Admin::select_by_password(password, conn)?) + Admin::select_by_password(password, conn) } pub fn select_by_id(id: i64, conn: &Connection) -> Result> { @@ -119,7 +119,7 @@ impl Admin { ":allowed_actions": serde_json::to_string(allowed_actions)?, }, )?; - Admin::select_by_id(id, &conn) + Admin::select_by_id(id, conn) } const fn mapper() -> fn(&Row) -> rusqlite::Result { @@ -132,7 +132,7 @@ impl Admin { allowed_actions: allowed_actions .as_array() .unwrap() - .into_iter() + .iter() .map(|it| it.as_str().unwrap().into()) .collect(), created_at: row.get(4)?, diff --git a/src/admin/service.rs b/src/admin/service.rs index 3101d03..0424e9e 100644 --- a/src/admin/service.rs +++ b/src/admin/service.rs @@ -32,9 +32,9 @@ pub async fn check_rpc(password: &str, action: &str, pool: &Pool) -> Result geometries.push(v), }; - return geometries; + geometries } } diff --git a/src/area/service.rs b/src/area/service.rs index e7de7da..63e40cc 100644 --- a/src/area/service.rs +++ b/src/area/service.rs @@ -40,10 +40,10 @@ pub fn insert(tags: Map, conn: &Connection) -> Result { if geo_json.is_err() { Err(Error::InvalidInput("Invalid geo_json".into()))? } - if Area::select_by_alias(url_alias, &conn)?.is_some() { + if Area::select_by_alias(url_alias, conn)?.is_some() { Err(Error::Conflict("This url_alias is already in use".into()))? } - let area = Area::insert(geo_json.unwrap(), tags, url_alias, &conn)?.unwrap(); + let area = Area::insert(geo_json.unwrap(), tags, url_alias, conn)?.unwrap(); let area_elements = element::service::find_in_area(&area, conn)?; element::service::generate_areas_mapping_old(&area_elements, conn)?; Ok(area) @@ -108,71 +108,63 @@ pub fn get_trending_areas( period_end: &OffsetDateTime, conn: &Connection, ) -> Result> { - let events = Event::select_created_between(&period_start, &period_end, &conn)?; + let events = Event::select_created_between(period_start, period_end, conn)?; let mut areas_to_events: HashMap> = HashMap::new(); for event in &events { - let element = Element::select_by_id(event.element_id, &conn)?.unwrap(); + let element = Element::select_by_id(event.element_id, conn)?.unwrap(); let element_area_ids: Vec = AreaElement::select_by_element_id(element.id, conn)? .into_iter() .map(|it| it.area_id) .collect(); for element_area_id in element_area_ids { - if !areas_to_events.contains_key(&element_area_id) { - areas_to_events.insert(element_area_id, vec![]); - } + areas_to_events.entry(element_area_id).or_default(); let area_events = areas_to_events.get_mut(&element_area_id).unwrap(); area_events.push(event); } } - let comments = ElementComment::select_created_between(&period_start, &period_end, &conn)?; + let comments = ElementComment::select_created_between(period_start, period_end, conn)?; let mut areas_to_comments: HashMap> = HashMap::new(); for comment in &comments { - let element = Element::select_by_id(comment.element_id, &conn)?.unwrap(); + let element = Element::select_by_id(comment.element_id, conn)?.unwrap(); let element_area_ids: Vec = AreaElement::select_by_element_id(element.id, conn)? .into_iter() .map(|it| it.area_id) .collect(); for element_area_id in element_area_ids { - if !areas_to_comments.contains_key(&element_area_id) { - areas_to_comments.insert(element_area_id, vec![]); - } + areas_to_comments.entry(element_area_id).or_default(); let area_comments = areas_to_comments.get_mut(&element_area_id).unwrap(); area_comments.push(comment); } } let mut areas: HashSet = HashSet::new(); - for (area_id, _) in &areas_to_events { + for area_id in areas_to_events.keys() { areas.insert(*area_id); } - for (area_id, _) in &areas_to_comments { + for area_id in areas_to_comments.keys() { areas.insert(*area_id); } let areas: Vec<_> = areas .into_iter() - .map(|it| Area::select_by_id(it, &conn).unwrap().unwrap()) + .map(|it| Area::select_by_id(it, conn).unwrap().unwrap()) .collect(); let mut res: Vec = areas .into_iter() .filter(|it| it.tags.contains_key("type") && it.tags["type"].as_str() == Some(r#type)) .map(|it| { - if !areas_to_events.contains_key(&it.id) { - areas_to_events.insert(it.id, vec![]); - } + areas_to_events.entry(it.id).or_default(); let events = areas_to_events.get(&it.id).unwrap(); let mut created: Vec<&Event> = vec![]; let mut updated: Vec<&Event> = vec![]; let mut deleted: Vec<&Event> = vec![]; for event in events { match event.r#type.as_str() { - "create" => created.push(&event), - "update" => updated.push(&event), - "delete" => deleted.push(&event), + "create" => created.push(event), + "update" => updated.push(event), + "delete" => deleted.push(event), _ => {} } } - if !areas_to_comments.contains_key(&it.id) { - areas_to_comments.insert(it.id, vec![]); - } + areas_to_comments.entry(it.id).or_default(); let comments = areas_to_comments.get(&it.id).unwrap(); TrendingArea { id: it.id, diff --git a/src/element/model.rs b/src/element/model.rs index 83135aa..832b540 100644 --- a/src/element/model.rs +++ b/src/element/model.rs @@ -49,8 +49,8 @@ impl Element { &query, named_params! { ":overpass_data": serde_json::to_string(overpass_data)?}, )?; - Ok(Element::select_by_id(conn.last_insert_rowid(), &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Element::select_by_id(conn.last_insert_rowid(), conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn select_all(limit: Option, conn: &Connection) -> Result> { @@ -209,8 +209,8 @@ impl Element { ":tags": &serde_json::to_string(tags)?, }, )?; - Ok(Element::select_by_id(id, &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Element::select_by_id(id, conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn set_overpass_data( @@ -235,8 +235,8 @@ impl Element { ":overpass_data": serde_json::to_string(overpass_data)?, }, )?; - Ok(Element::select_by_id(self.id, &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Element::select_by_id(self.id, conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn set_tag(id: i64, name: &str, value: &Value, conn: &Connection) -> Result { @@ -264,8 +264,8 @@ impl Element { }, )?; info!("Removed {name} tag from element {id}"); - Ok(Element::select_by_id(id, &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Element::select_by_id(id, conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } #[cfg(test)] @@ -341,8 +341,8 @@ impl Element { conn.execute(&query, named_params! { ":id": self.id })?; } }; - Ok(Element::select_by_id(self.id, &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Element::select_by_id(self.id, conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn tag(&self, name: &str) -> &Value { @@ -361,7 +361,7 @@ const fn mapper() -> fn(&Row) -> rusqlite::Result { let tags: String = row.get(2)?; Ok(Element { id: row.get(0)?, - overpass_data: overpass_data, + overpass_data, tags: serde_json::from_str(&tags).unwrap(), created_at: row.get(3)?, updated_at: row.get(4)?, diff --git a/src/element/service.rs b/src/element/service.rs index 6cd57a5..6958fc8 100644 --- a/src/element/service.rs +++ b/src/element/service.rs @@ -17,10 +17,7 @@ use time::OffsetDateTime; use tracing::info; pub fn find_in_area(area: &Area, conn: &Connection) -> Result> { - let all_elements: Vec = Element::select_all(None, &conn)? - .into_iter() - .filter(|it| it.deleted_at == None) - .collect(); + let all_elements: Vec = Element::select_all_except_deleted(conn)?; filter_by_area(&all_elements, area) } @@ -65,7 +62,7 @@ pub fn generate_areas_mapping_old( conn: &Connection, ) -> Result> { let mut res: Vec = vec![]; - let all_areas: Vec = Area::select_all(&conn)?; + let all_areas: Vec = Area::select_all(conn)?; for element in elements { let element_areas = find_areas(element, &all_areas)?; let element_areas = areas_to_areas_tag(element_areas); @@ -153,7 +150,7 @@ pub fn generate_issues(elements: Vec<&Element>, conn: &Connection) -> Result Vec { }; if let Some(issue) = crate::element::service::get_out_of_date_issue(element) { res.push(issue); - } else { - if let Some(issue) = crate::element::service::get_soon_out_of_date_issue(element) { - res.push(issue); - }; + } else if let Some(issue) = crate::element::service::get_soon_out_of_date_issue(element) { + res.push(issue); }; res } @@ -208,7 +203,7 @@ fn get_date_format_issues(element: &Element) -> Vec { let mut res: Vec = vec![]; let date_format = format_description!("[year]-[month]-[day]"); let survey_date = element.overpass_data.tag("survey:date"); - if survey_date.len() > 0 && Date::parse(survey_date, &date_format).is_err() { + if !survey_date.is_empty() && Date::parse(survey_date, &date_format).is_err() { res.push(Issue { r#type: "date_format".into(), severity: 600, @@ -216,7 +211,7 @@ fn get_date_format_issues(element: &Element) -> Vec { }); } let check_date = element.overpass_data.tag("check_date"); - if check_date.len() > 0 && Date::parse(check_date, &date_format).is_err() { + if !check_date.is_empty() && Date::parse(check_date, &date_format).is_err() { res.push(Issue { r#type: "date_format".into(), severity: 600, @@ -224,7 +219,7 @@ fn get_date_format_issues(element: &Element) -> Vec { }); } let check_date_currency_xbt = element.overpass_data.tag("check_date:currency:XBT"); - if check_date_currency_xbt.len() > 0 + if !check_date_currency_xbt.is_empty() && Date::parse(check_date_currency_xbt, &date_format).is_err() { res.push(Issue { @@ -239,7 +234,7 @@ fn get_date_format_issues(element: &Element) -> Vec { fn get_misspelled_tag_issues(element: &Element) -> Vec { let mut res: Vec = vec![]; let payment_lighting = element.overpass_data.tag("payment:lighting"); - if payment_lighting.len() > 0 { + if !payment_lighting.is_empty() { res.push(Issue { r#type: "misspelled_tag".into(), severity: 500, @@ -247,7 +242,7 @@ fn get_misspelled_tag_issues(element: &Element) -> Vec { }); } let payment_lightning_contacless = element.overpass_data.tag("payment:lightning_contacless"); - if payment_lightning_contacless.len() > 0 { + if !payment_lightning_contacless.is_empty() { res.push(Issue { r#type: "misspelled_tag".into(), severity: 500, @@ -255,7 +250,7 @@ fn get_misspelled_tag_issues(element: &Element) -> Vec { }); } let payment_lighting_contactless = element.overpass_data.tag("payment:lighting_contactless"); - if payment_lighting_contactless.len() > 0 { + if !payment_lighting_contactless.is_empty() { res.push(Issue { r#type: "misspelled_tag".into(), severity: 500, diff --git a/src/element/v2.rs b/src/element/v2.rs index 6e178ef..938b71a 100644 --- a/src/element/v2.rs +++ b/src/element/v2.rs @@ -39,26 +39,25 @@ pub struct GetItem { pub deleted_at: String, } -impl Into for Element { - fn into(self) -> GetItem { +impl From for GetItem { + fn from(val: Element) -> GetItem { GetItem { - id: self.overpass_data.btcmap_id(), - osm_json: self.overpass_data, - tags: self.tags, - created_at: self.created_at, - updated_at: self.updated_at, - deleted_at: self + id: val.overpass_data.btcmap_id(), + osm_json: val.overpass_data, + tags: val.tags, + created_at: val.created_at, + updated_at: val.updated_at, + deleted_at: val .deleted_at .map(|it| it.format(&Rfc3339).unwrap()) - .unwrap_or_default() - .into(), + .unwrap_or_default(), } } } -impl Into> for Element { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: Element) -> Json { + Json(val.into()) } } diff --git a/src/element/v3.rs b/src/element/v3.rs index 08f611b..83a3bbe 100644 --- a/src/element/v3.rs +++ b/src/element/v3.rs @@ -38,31 +38,31 @@ pub struct GetItem { pub deleted_at: Option, } -impl Into for Element { - fn into(self) -> GetItem { - let overpass_data = if self.deleted_at.is_none() { - Some(self.overpass_data) +impl From for GetItem { + fn from(val: Element) -> GetItem { + let overpass_data = if val.deleted_at.is_none() { + Some(val.overpass_data) } else { None }; - let tags = if self.deleted_at.is_none() { - Some(self.tags) + let tags = if val.deleted_at.is_none() { + Some(val.tags) } else { None }; GetItem { - id: self.id, + id: val.id, osm_data: overpass_data, tags, - updated_at: self.updated_at, - deleted_at: self.deleted_at, + updated_at: val.updated_at, + deleted_at: val.deleted_at, } } } -impl Into> for Element { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: Element) -> Json { + Json(val.into()) } } diff --git a/src/event/model.rs b/src/event/model.rs index 6c5217f..d10a676 100644 --- a/src/event/model.rs +++ b/src/event/model.rs @@ -68,8 +68,8 @@ impl Event { ":type": r#type, }, )?; - Ok(Event::select_by_id(conn.last_insert_rowid(), &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Event::select_by_id(conn.last_insert_rowid(), conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn select_all( @@ -304,8 +304,7 @@ impl Event { ":tags": &serde_json::to_string(tags)?, }, )?; - Ok(Event::select_by_id(id, &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Event::select_by_id(id, conn)?.ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } #[cfg(test)] @@ -365,7 +364,7 @@ mod test { use super::Event; use crate::{ element::Element, - osm::{osm::OsmUser, overpass::OverpassElement}, + osm::{api::OsmUser, overpass::OverpassElement}, test::mock_conn, user::User, Result, diff --git a/src/event/service.rs b/src/event/service.rs index 85508ea..718448a 100644 --- a/src/event/service.rs +++ b/src/event/service.rs @@ -1,6 +1,6 @@ use crate::discord; use crate::event::Event; -use crate::osm::osm; +use crate::osm; use crate::user::User; use crate::Result; use rusqlite::Connection; @@ -13,7 +13,7 @@ use tracing::info; use tracing::warn; pub async fn on_new_event(event: &Event, conn: &Connection) -> Result<()> { - let user = User::select_by_id(event.user_id, &conn)?.unwrap(); + let user = User::select_by_id(event.user_id, conn)?.unwrap(); let message = match event.r#type.as_str() { "create" => format!( @@ -53,7 +53,7 @@ pub async fn on_new_event(event: &Event, conn: &Connection) -> Result<()> { } } - match osm::get_user(user.osm_data.id).await { + match osm::api::get_user(user.osm_data.id).await { Ok(new_osm_data) => match new_osm_data { Some(new_osm_data) => { if new_osm_data != user.osm_data { @@ -62,18 +62,18 @@ pub async fn on_new_event(event: &Event, conn: &Connection) -> Result<()> { new_osm_data = serde_json::to_string(&new_osm_data)?, "User data changed", ); - User::set_osm_data(user.id, &new_osm_data, &conn)?; + User::set_osm_data(user.id, &new_osm_data, conn)?; } else { info!("User data didn't change") } let now = OffsetDateTime::now_utc(); let now: String = now.format(&Rfc3339)?; - User::set_tag(user.id, "osm:sync:date", &Value::String(now), &conn)?; + User::set_tag(user.id, "osm:sync:date", &Value::String(now), conn)?; } None => { warn!(user.osm_data.id, "User no longer exists on OSM"); - User::set_tag(user.id, "osm:missing", &Value::Bool(true), &conn)?; + User::set_tag(user.id, "osm:missing", &Value::Bool(true), conn)?; } }, Err(e) => error!("Failed to fetch user {} {}", user.osm_data.id, e), diff --git a/src/event/v2.rs b/src/event/v2.rs index 65d8b86..af92d46 100644 --- a/src/event/v2.rs +++ b/src/event/v2.rs @@ -41,28 +41,27 @@ pub struct GetItem { pub deleted_at: String, } -impl Into for Event { - fn into(self) -> GetItem { +impl From for GetItem { + fn from(val: Event) -> GetItem { GetItem { - id: self.id, - user_id: self.user_id, - element_id: format!("{}:{}", self.element_osm_type, self.element_osm_id), - r#type: self.r#type, - tags: self.tags, - created_at: self.created_at, - updated_at: self.updated_at, - deleted_at: self + id: val.id, + user_id: val.user_id, + element_id: format!("{}:{}", val.element_osm_type, val.element_osm_id), + r#type: val.r#type, + tags: val.tags, + created_at: val.created_at, + updated_at: val.updated_at, + deleted_at: val .deleted_at .map(|it| it.format(&Rfc3339).unwrap()) - .unwrap_or_default() - .into(), + .unwrap_or_default(), } } } -impl Into> for Event { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: Event) -> Json { + Json(val.into()) } } @@ -116,7 +115,7 @@ mod test { use crate::element::Element; use crate::event::v2::GetItem; use crate::event::Event; - use crate::osm::osm::OsmUser; + use crate::osm::api::OsmUser; use crate::osm::overpass::OverpassElement; use crate::test::mock_state; use crate::user::User; diff --git a/src/event/v3.rs b/src/event/v3.rs index d0d664c..58324a3 100644 --- a/src/event/v3.rs +++ b/src/event/v3.rs @@ -46,20 +46,20 @@ pub struct GetItem { pub deleted_at: Option, } -impl Into for Event { - fn into(self) -> GetItem { - let user_id = if self.deleted_at.is_none() { - Some(self.user_id) +impl From for GetItem { + fn from(val: Event) -> GetItem { + let user_id = if val.deleted_at.is_none() { + Some(val.user_id) } else { None }; - let element_id = if self.deleted_at.is_none() { - Some(self.element_id) + let element_id = if val.deleted_at.is_none() { + Some(val.element_id) } else { None }; - let r#type = if self.deleted_at.is_none() { - Some(match self.r#type.as_str() { + let r#type = if val.deleted_at.is_none() { + Some(match val.r#type.as_str() { "create" => 1, "update" => 2, "delete" => 3, @@ -68,32 +68,32 @@ impl Into for Event { } else { None }; - let tags = if self.deleted_at.is_none() && !self.tags.is_empty() { - Some(self.tags) + let tags = if val.deleted_at.is_none() && !val.tags.is_empty() { + Some(val.tags) } else { None }; - let created_at = if self.deleted_at.is_none() { - Some(self.created_at) + let created_at = if val.deleted_at.is_none() { + Some(val.created_at) } else { None }; GetItem { - id: self.id, + id: val.id, user_id, element_id, r#type, tags, created_at, - updated_at: self.updated_at, - deleted_at: self.deleted_at, + updated_at: val.updated_at, + deleted_at: val.deleted_at, } } } -impl Into> for Event { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: Event) -> Self { + Json(val.into()) } } @@ -135,7 +135,7 @@ pub async fn get_by_id(id: Path, pool: Data) -> Result, mod test { use crate::element::Element; use crate::event::Event; - use crate::osm::osm::OsmUser; + use crate::osm::api::OsmUser; use crate::osm::overpass::OverpassElement; use crate::test::mock_state; use crate::user::User; diff --git a/src/osm/osm.rs b/src/osm/api.rs similarity index 97% rename from src/osm/osm.rs rename to src/osm/api.rs index 0b7f8c9..a6f2681 100644 --- a/src/osm/osm.rs +++ b/src/osm/api.rs @@ -43,14 +43,14 @@ pub async fn get_element(element_type: &str, element_id: i64) -> Result Result> { if res.status().is_success() { let mut res: OsmElementResponse = res.json().await?; - return Ok(if res.elements.len() == 1 { + Ok(if res.elements.len() == 1 { Some(res.elements.pop().unwrap()) } else { None - }); + }) } else { match res.status() { - StatusCode::NOT_FOUND => return Ok(None), + StatusCode::NOT_FOUND => Ok(None), _ => Err(Error::OsmApi(format!( "Unexpected response status: {}", res.status() @@ -144,11 +144,11 @@ pub async fn get_user(id: i64) -> Result> { async fn _get_user(res: Response) -> Result> { if res.status().is_success() { let res: OsmUserResponse = res.json().await?; - return Ok(Some(res.user)); + Ok(Some(res.user)) } else { match res.status() { - StatusCode::NOT_FOUND => return Ok(None), - StatusCode::GONE => return Ok(None), + StatusCode::NOT_FOUND => Ok(None), + StatusCode::GONE => Ok(None), _ => Err(Error::OsmApi(format!( "Unexpected response status: {}", res.status() diff --git a/src/osm/mod.rs b/src/osm/mod.rs index 07344ba..6327857 100644 --- a/src/osm/mod.rs +++ b/src/osm/mod.rs @@ -1,2 +1,2 @@ -pub mod osm; +pub mod api; pub mod overpass; diff --git a/src/osm/overpass.rs b/src/osm/overpass.rs index 5855e25..46c1033 100644 --- a/src/osm/overpass.rs +++ b/src/osm/overpass.rs @@ -67,10 +67,10 @@ impl OverpassElement { } pub fn up_to_date(&self) -> bool { - return match self.days_since_verified() { + match self.days_since_verified() { Some(days) => days < 365, None => false, - }; + } } pub fn days_since_verified(&self) -> Option { @@ -162,7 +162,7 @@ pub async fn query_bitcoin_merchants() -> Result> { let response = response.json::().await?; - if response.elements.len() == 0 { + if response.elements.is_empty() { Err(Error::OverpassApi(format!( "Got suspicious response: {}", serde_json::to_string_pretty(&response)? diff --git a/src/report/model.rs b/src/report/model.rs index 384961c..19aacb0 100644 --- a/src/report/model.rs +++ b/src/report/model.rs @@ -57,8 +57,8 @@ impl Report { }, )?; - Ok(Report::select_by_id(conn.last_insert_rowid(), &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + Report::select_by_id(conn.last_insert_rowid(), conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn select_updated_since( @@ -266,7 +266,7 @@ const fn mapper() -> fn(&Row) -> rusqlite::Result { area_id: row.get(1)?, area_url_alias: row.get(2)?, date: Date::parse(&date, &format_description!("[year]-[month]-[day]")).unwrap(), - tags: tags, + tags, created_at: row.get(5)?, updated_at: row.get(6)?, deleted_at: row.get(7)?, diff --git a/src/report/v2.rs b/src/report/v2.rs index 9802023..87bc1af 100644 --- a/src/report/v2.rs +++ b/src/report/v2.rs @@ -42,33 +42,32 @@ pub struct GetItem { pub deleted_at: String, } -impl Into for Report { - fn into(self) -> GetItem { - let area_id = if self.area_url_alias == "earth" { +impl From for GetItem { + fn from(val: Report) -> Self { + let area_id = if val.area_url_alias == "earth" { "".into() } else { - self.area_url_alias + val.area_url_alias }; GetItem { - id: self.id, + id: val.id, area_id, - date: self.date.to_string(), - tags: self.tags, - created_at: self.created_at, - updated_at: self.updated_at, - deleted_at: self + date: val.date.to_string(), + tags: val.tags, + created_at: val.created_at, + updated_at: val.updated_at, + deleted_at: val .deleted_at .map(|it| it.format(&Rfc3339).unwrap()) - .unwrap_or_default() - .into(), + .unwrap_or_default(), } } } -impl Into> for Report { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: Report) -> Self { + Json(val.into()) } } diff --git a/src/report/v3.rs b/src/report/v3.rs index 55ddd66..b8034e1 100644 --- a/src/report/v3.rs +++ b/src/report/v3.rs @@ -39,37 +39,37 @@ pub struct GetItem { pub deleted_at: Option, } -impl Into for Report { - fn into(self) -> GetItem { - let area_id = if self.deleted_at.is_none() { - Some(self.area_id) +impl From for GetItem { + fn from(val: Report) -> Self { + let area_id = if val.deleted_at.is_none() { + Some(val.area_id) } else { None }; - let date = if self.deleted_at.is_none() { - Some(self.date) + let date = if val.deleted_at.is_none() { + Some(val.date) } else { None }; - let tags = if self.deleted_at.is_none() && !self.tags.is_empty() { - Some(self.tags) + let tags = if val.deleted_at.is_none() && !val.tags.is_empty() { + Some(val.tags) } else { None }; GetItem { - id: self.id, + id: val.id, area_id, date: date.map(|it| it.to_string()), tags, - updated_at: self.updated_at, - deleted_at: self.deleted_at, + updated_at: val.updated_at, + deleted_at: val.deleted_at, } } } -impl Into> for Report { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: Report) -> Self { + Json(val.into()) } } diff --git a/src/sync.rs b/src/sync.rs index e55338c..4812563 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,7 +1,7 @@ use crate::element::{self, Element}; use crate::event::{self, Event}; -use crate::osm::osm::{self, OsmElement}; use crate::osm::overpass::OverpassElement; +use crate::osm::{self, api::OsmElement}; use crate::user::User; use crate::{discord, Error, Result}; use rusqlite::Connection; @@ -98,23 +98,21 @@ fn mark_element_as_deleted( } async fn confirm_deleted(osm_type: &str, osm_id: i64) -> Result { - let osm_element = match osm::get_element(osm_type, osm_id).await? { + let osm_element = match osm::api::get_element(osm_type, osm_id).await? { Some(v) => v, None => Err(Error::OsmApi(format!( "Failed to fetch element {}:{} from OSM", osm_type, osm_id, )))?, }; - if osm_element.visible.unwrap_or(true) { - if osm_element.tag("currency:XBT", "no") == "yes" { - let message = format!( - "Overpass lied about element {}:{} being deleted", - osm_type, osm_id, - ); - error!(message); - discord::send_message_to_channel(&message, discord::CHANNEL_OSM_CHANGES).await; - Err(Error::OverpassApi(message))? - } + if osm_element.visible.unwrap_or(true) && osm_element.tag("currency:XBT", "no") == "yes" { + let message = format!( + "Overpass lied about element {}:{} being deleted", + osm_type, osm_id, + ); + error!(message); + discord::send_message_to_channel(&message, discord::CHANNEL_OSM_CHANGES).await; + Err(Error::OverpassApi(message))? } Ok(osm_element) } @@ -236,7 +234,7 @@ async fn insert_user_if_not_exists(user_id: i64, conn: &Connection) -> Result<() info!(user_id, "User already exists"); return Ok(()); } - match osm::get_user(user_id).await? { + match osm::api::get_user(user_id).await? { Some(user) => User::insert(user_id, &user, conn)?, None => Err(Error::OsmApi(format!( "User with id = {user_id} doesn't exist on OSM" @@ -249,7 +247,7 @@ async fn insert_user_if_not_exists(user_id: i64, conn: &Connection) -> Result<() mod test { use crate::{ element::Element, - osm::{osm::OsmUser, overpass::OverpassElement}, + osm::{api::OsmUser, overpass::OverpassElement}, test::mock_state, user::User, Result, diff --git a/src/user/model.rs b/src/user/model.rs index 19f1819..27f10c6 100644 --- a/src/user/model.rs +++ b/src/user/model.rs @@ -1,4 +1,4 @@ -use crate::{osm::osm::OsmUser, Error, Result}; +use crate::{osm::api::OsmUser, Error, Result}; use rusqlite::{named_params, Connection, OptionalExtension, Row}; use serde_json::{Map, Value}; use std::collections::HashMap; @@ -19,6 +19,8 @@ pub struct User { pub deleted_at: Option, } +const TABLE_NAME: &str = "user"; + impl User { pub fn insert(id: i64, osm_data: &OsmUser, conn: &Connection) -> Result { let query = r#" @@ -40,8 +42,8 @@ impl User { }, )?; - Ok(User::select_by_id(conn.last_insert_rowid(), &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + User::select_by_id(conn.last_insert_rowid(), conn)? + .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn select_all(limit: Option, conn: &Connection) -> Result> { @@ -133,7 +135,7 @@ impl User { created_at, updated_at, deleted_at - FROM user + FROM {TABLE_NAME} WHERE json_extract(osm_data, '$.display_name') = :name "# ); @@ -154,25 +156,26 @@ impl User { tags: &HashMap, conn: &Connection, ) -> crate::Result { - let query = r#" - UPDATE user - SET tags = json_patch(tags, :tags) - WHERE rowid = :id - "#; + let query = format!( + r#" + UPDATE {TABLE_NAME} + SET tags = json_patch(tags, :tags) + WHERE rowid = :id + "# + ); #[cfg(not(test))] sleep(Duration::from_millis(10)); conn.execute( - query, + &query, named_params! { ":id": id, ":tags": &serde_json::to_string(tags)? }, )?; - Ok(User::select_by_id(id, &conn)? - .ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows))?) + User::select_by_id(id, conn)?.ok_or(Error::Rusqlite(rusqlite::Error::QueryReturnedNoRows)) } pub fn remove_tag(id: i64, name: &str, conn: &Connection) -> Result> { let query = format!( r#" - UPDATE user + UPDATE {TABLE_NAME} SET tags = json_remove(tags, :name) WHERE id = :id "# @@ -186,7 +189,7 @@ impl User { ":name": format!("$.{name}"), }, )?; - let res = User::select_by_id(id, &conn)?; + let res = User::select_by_id(id, conn)?; Ok(res) } @@ -255,7 +258,7 @@ const fn mapper() -> fn(&Row) -> rusqlite::Result { #[cfg(test)] mod test { - use crate::{osm::osm::OsmUser, test::mock_conn, user::User, Result}; + use crate::{osm::api::OsmUser, test::mock_conn, user::User, Result}; use std::collections::HashMap; use time::macros::datetime; diff --git a/src/user/v2.rs b/src/user/v2.rs index ec5437f..ffa4ad3 100644 --- a/src/user/v2.rs +++ b/src/user/v2.rs @@ -1,5 +1,5 @@ use crate::log::RequestExtension; -use crate::osm::osm::OsmUser; +use crate::osm::api::OsmUser; use crate::user::User; use crate::Error; use actix_web::get; @@ -39,26 +39,25 @@ pub struct GetItem { pub deleted_at: String, } -impl Into for User { - fn into(self) -> GetItem { +impl From for GetItem { + fn from(val: User) -> Self { GetItem { - id: self.id, - osm_json: self.osm_data, - tags: self.tags, - created_at: self.created_at, - updated_at: self.updated_at, - deleted_at: self + id: val.id, + osm_json: val.osm_data, + tags: val.tags, + created_at: val.created_at, + updated_at: val.updated_at, + deleted_at: val .deleted_at .map(|it| it.format(&Rfc3339).unwrap()) - .unwrap_or_default() - .into(), + .unwrap_or_default(), } } } -impl Into> for User { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: User) -> Self { + Json(val.into()) } } @@ -103,7 +102,7 @@ pub async fn get_by_id(id: Path, pool: Data) -> Result, #[cfg(test)] mod test { - use crate::osm::osm::OsmUser; + use crate::osm::api::OsmUser; use crate::test::mock_state; use crate::user::v2::GetItem; use crate::user::User; diff --git a/src/user/v3.rs b/src/user/v3.rs index 8f77946..5e0d7c6 100644 --- a/src/user/v3.rs +++ b/src/user/v3.rs @@ -1,5 +1,5 @@ use crate::log::RequestExtension; -use crate::osm::osm::OsmUser; +use crate::osm::api::OsmUser; use crate::user::User; use crate::Error; use actix_web::get; @@ -38,31 +38,31 @@ pub struct GetItem { pub deleted_at: Option, } -impl Into for User { - fn into(self) -> GetItem { - let osm_data = if self.deleted_at.is_none() { - Some(self.osm_data) +impl From for GetItem { + fn from(val: User) -> Self { + let osm_data = if val.deleted_at.is_none() { + Some(val.osm_data) } else { None }; - let tags = if self.deleted_at.is_none() { - Some(self.tags) + let tags = if val.deleted_at.is_none() { + Some(val.tags) } else { None }; GetItem { - id: self.id, + id: val.id, osm_data, tags, - updated_at: self.updated_at, - deleted_at: self.deleted_at, + updated_at: val.updated_at, + deleted_at: val.deleted_at, } } } -impl Into> for User { - fn into(self) -> Json { - Json(self.into()) +impl From for Json { + fn from(val: User) -> Self { + Json(val.into()) } } @@ -100,7 +100,7 @@ pub async fn get_by_id(id: Path, pool: Data) -> Result, #[cfg(test)] mod test { use crate::error::{self, SyncAPIErrorResponseBody}; - use crate::osm::osm::OsmUser; + use crate::osm::api::OsmUser; use crate::test::mock_state; use crate::user::User; use crate::Result;