Skip to content

Commit

Permalink
Fix code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 8, 2024
1 parent db3fc1b commit 9ecde27
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 231 deletions.
6 changes: 3 additions & 3 deletions src/admin/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Admin>> {
Expand Down Expand Up @@ -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<Admin> {
Expand All @@ -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)?,
Expand Down
6 changes: 3 additions & 3 deletions src/admin/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub async fn check_rpc(password: &str, action: &str, pool: &Pool) -> Result<Admi
admin.name, action,
);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Err(Error::Unauthorized(format!(
"You are not allowed to perform this action"
)))?
Err(Error::Unauthorized(
"You are not allowed to perform this action".into(),
))?
}
Ok(admin)
}
Expand Down
6 changes: 3 additions & 3 deletions src/area/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl Area {
":tags": Value::from(tags),
},
)?;
let res = Area::select_by_id(id, &conn)?;
let res = Area::select_by_id(id, conn)?;
Ok(res)
}

Expand All @@ -207,7 +207,7 @@ impl Area {
":name": format!("$.{name}"),
},
)?;
let res = Area::select_by_id(id, &conn)?;
let res = Area::select_by_id(id, conn)?;
Ok(res)
}

Expand Down Expand Up @@ -339,7 +339,7 @@ impl Area {
GeoJson::Geometry(v) => geometries.push(v),
};

return geometries;
geometries
}
}

Expand Down
40 changes: 16 additions & 24 deletions src/area/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ pub fn insert(tags: Map<String, Value>, conn: &Connection) -> Result<Area> {
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)
Expand Down Expand Up @@ -108,71 +108,63 @@ pub fn get_trending_areas(
period_end: &OffsetDateTime,
conn: &Connection,
) -> Result<Vec<TrendingArea>> {
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<i64, Vec<&Event>> = 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<i64> = 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<i64, Vec<&ElementComment>> = 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<i64> = 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<i64> = 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<TrendingArea> = 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,
Expand Down
22 changes: 11 additions & 11 deletions src/element/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64>, conn: &Connection) -> Result<Vec<Element>> {
Expand Down Expand Up @@ -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(
Expand All @@ -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<Element> {
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand All @@ -361,7 +361,7 @@ const fn mapper() -> fn(&Row) -> rusqlite::Result<Element> {
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)?,
Expand Down
27 changes: 11 additions & 16 deletions src/element/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use time::OffsetDateTime;
use tracing::info;

pub fn find_in_area(area: &Area, conn: &Connection) -> Result<Vec<Element>> {
let all_elements: Vec<Element> = Element::select_all(None, &conn)?
.into_iter()
.filter(|it| it.deleted_at == None)
.collect();
let all_elements: Vec<Element> = Element::select_all_except_deleted(conn)?;
filter_by_area(&all_elements, area)
}

Expand Down Expand Up @@ -65,7 +62,7 @@ pub fn generate_areas_mapping_old(
conn: &Connection,
) -> Result<Vec<Element>> {
let mut res: Vec<Element> = vec![];
let all_areas: Vec<Area> = Area::select_all(&conn)?;
let all_areas: Vec<Area> = 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);
Expand Down Expand Up @@ -153,7 +150,7 @@ pub fn generate_issues(elements: Vec<&Element>, conn: &Connection) -> Result<Gen
let started_at = OffsetDateTime::now_utc();
let mut affected_elements = 0;
for element in elements {
let issues = crate::element::service::get_issues(&element);
let issues = crate::element::service::get_issues(element);
// No current issues, no saved issues, nothing to do here
if issues.is_empty() && !element.tags.contains_key("issues") {
continue;
Expand Down Expand Up @@ -196,10 +193,8 @@ fn get_issues(element: &Element) -> Vec<Issue> {
};
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
}
Expand All @@ -208,23 +203,23 @@ fn get_date_format_issues(element: &Element) -> Vec<Issue> {
let mut res: Vec<Issue> = 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,
description: "survey:date is not formatted properly".into(),
});
}
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,
description: "check_date is not formatted properly".into(),
});
}
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 {
Expand All @@ -239,23 +234,23 @@ fn get_date_format_issues(element: &Element) -> Vec<Issue> {
fn get_misspelled_tag_issues(element: &Element) -> Vec<Issue> {
let mut res: Vec<Issue> = 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,
description: "Spelling issue: payment:lighting".into(),
});
}
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,
description: "Spelling issue: payment:lightning_contacless".into(),
});
}
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,
Expand Down
25 changes: 12 additions & 13 deletions src/element/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,25 @@ pub struct GetItem {
pub deleted_at: String,
}

impl Into<GetItem> for Element {
fn into(self) -> GetItem {
impl From<Element> 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<Json<GetItem>> for Element {
fn into(self) -> Json<GetItem> {
Json(self.into())
impl From<Element> for Json<GetItem> {
fn from(val: Element) -> Json<GetItem> {
Json(val.into())
}
}

Expand Down
Loading

0 comments on commit 9ecde27

Please sign in to comment.