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 6, 2024
1 parent 08dcefa commit db3fc1b
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 109 deletions.
23 changes: 11 additions & 12 deletions src/area/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,24 @@ pub struct GetItem {
pub deleted_at: String,
}

impl Into<GetItem> for Area {
fn into(self) -> GetItem {
impl From<Area> for GetItem {
fn from(val: Area) -> Self {
GetItem {
id: self.tags["url_alias"].as_str().unwrap().into(),
tags: self.tags,
created_at: self.created_at,
updated_at: self.updated_at,
deleted_at: self
id: val.tags["url_alias"].as_str().unwrap().into(),
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 Area {
fn into(self) -> Json<GetItem> {
Json(self.into())
impl From<Area> for Json<GetItem> {
fn from(val: Area) -> Self {
Json(val.into())
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/area/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ pub struct GetItem {
pub deleted_at: Option<OffsetDateTime>,
}

impl Into<GetItem> for Area {
fn into(self) -> GetItem {
let tags = if self.deleted_at.is_none() && !self.tags.is_empty() {
Some(self.tags)
impl From<Area> for GetItem {
fn from(val: Area) -> Self {
let tags = if val.deleted_at.is_none() && !val.tags.is_empty() {
Some(val.tags)
} else {
None
};
GetItem {
id: self.id,
id: val.id,
tags,
updated_at: self.updated_at,
deleted_at: self.deleted_at,
updated_at: val.updated_at,
deleted_at: val.deleted_at,
}
}
}

impl Into<Json<GetItem>> for Area {
fn into(self) -> Json<GetItem> {
Json(self.into())
impl From<Area> for Json<GetItem> {
fn from(val: Area) -> Self {
Json(val.into())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/area_element/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub fn generate_areas_mapping(
conn: &Connection,
) -> Result<Res> {
let mut has_changes = false;
let element_areas = element::service::find_areas(&element, &areas)?;
let element_areas = element::service::find_areas(element, areas)?;
let old_mappings = AreaElement::select_by_element_id(element.id, conn)?;
let mut old_area_ids: Vec<i64> = old_mappings.into_iter().map(|it| it.area_id).collect();
let mut new_area_ids: Vec<i64> = element_areas.into_iter().map(|it| it.id).collect();
old_area_ids.sort();
new_area_ids.sort();
if new_area_ids != old_area_ids {
for old_area_id in &old_area_ids {
if !new_area_ids.contains(&old_area_id) {
if !new_area_ids.contains(old_area_id) {
AreaElement::set_deleted_at(*old_area_id, Some(OffsetDateTime::now_utc()), conn)?;
}
}
Expand Down
32 changes: 15 additions & 17 deletions src/area_element/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ pub struct GetItem {
pub deleted_at: Option<OffsetDateTime>,
}

impl Into<GetItem> for AreaElement {
fn into(self) -> GetItem {
let area_id = if self.deleted_at.is_none() {
Some(self.area_id)
impl From<AreaElement> for GetItem {
fn from(val: AreaElement) -> Self {
let area_id = if val.deleted_at.is_none() {
Some(val.area_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
};
GetItem {
id: self.id,
id: val.id,
area_id,
element_id,
updated_at: self.updated_at,
deleted_at: self.deleted_at,
updated_at: val.updated_at,
deleted_at: val.deleted_at,
}
}
}

impl Into<Json<GetItem>> for AreaElement {
fn into(self) -> Json<GetItem> {
Json(self.into())
impl From<AreaElement> for Json<GetItem> {
fn from(val: AreaElement) -> Self {
Json(val.into())
}
}

Expand Down Expand Up @@ -81,13 +81,11 @@ pub async fn get(

#[get("{id}")]
pub async fn get_by_id(id: Path<i64>, pool: Data<Pool>) -> Result<Json<GetItem>> {
let id_clone = id.clone();
let not_found = Error::NotFound(format!("Area-element mapping with id {id} doesn't exist"));
pool.get()
.await?
.interact(move |conn| AreaElement::select_by_id(id_clone, conn))
.interact(move |conn| AreaElement::select_by_id(*id, conn))
.await??
.ok_or(Error::NotFound(format!(
"Area-element mapping with id {id} doesn't exist"
)))
.ok_or(not_found)
.map(|it| it.into())
}
1 change: 1 addition & 0 deletions src/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct Ban {
end_at: OffsetDateTime,
}

#[allow(clippy::await_holding_refcell_ref)]
pub async fn check_if_banned(
req: ServiceRequest,
next: Next<impl MessageBody>,
Expand Down
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn init_pragmas(conn: &Connection) {
// conn.pragma_update(None, "cache_size", 25000).unwrap();
}

fn execute_migrations(migrations: &Vec<Migration>, db: &mut Connection) -> Result<()> {
fn execute_migrations(migrations: &[Migration], db: &mut Connection) -> Result<()> {
let mut schema_ver: i16 =
db.query_row("SELECT user_version FROM pragma_user_version", [], |row| {
row.get(0)
Expand Down
36 changes: 17 additions & 19 deletions src/element_comment/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,37 @@ pub struct GetItem {
pub deleted_at: Option<OffsetDateTime>,
}

impl Into<GetItem> for ElementComment {
fn into(self) -> GetItem {
let element_id = if self.deleted_at.is_none() {
Some(self.element_id)
impl From<ElementComment> for GetItem {
fn from(val: ElementComment) -> Self {
let element_id = if val.deleted_at.is_none() {
Some(val.element_id)
} 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
};
let comment = if self.deleted_at.is_none() {
Some(self.comment)
let comment = if val.deleted_at.is_none() {
Some(val.comment)
} else {
None
};
GetItem {
id: self.id,
id: val.id,
element_id,
comment,
created_at,
updated_at: self.updated_at,
deleted_at: self.deleted_at,
updated_at: val.updated_at,
deleted_at: val.deleted_at,
}
}
}

impl Into<Json<GetItem>> for ElementComment {
fn into(self) -> Json<GetItem> {
Json(self.into())
impl From<ElementComment> for Json<GetItem> {
fn from(val: ElementComment) -> Self {
Json(val.into())
}
}

Expand Down Expand Up @@ -96,13 +96,11 @@ pub async fn get(

#[get("{id}")]
pub async fn get_by_id(id: Path<i64>, pool: Data<Pool>) -> Result<Json<GetItem>, Error> {
let id_clone = id.clone();
let not_found_err = Error::NotFound(format!("Element comment with id {id} doesn't exist"));
pool.get()
.await?
.interact(move |conn| ElementComment::select_by_id(id_clone, conn))
.interact(move |conn| ElementComment::select_by_id(*id, conn))
.await??
.ok_or(Error::NotFound(format!(
"Element comment with id {id} doesn't exist"
)))
.ok_or(not_found_err)
.map(|it| it.into())
}
8 changes: 3 additions & 5 deletions src/feed/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ fn event_to_atom_entry(event: (Event, Element)) -> String {
let event_id = event.0.id;
let event_created_at = event.0.created_at.format(&Rfc3339).unwrap();
let element_id = event.1.overpass_data.btcmap_id();
let title = format!("{}", xml_escape(event.1.name()));
let summary = format!("Check BTC Map for more details");
let title = xml_escape(event.1.name());
let summary = "Check BTC Map for more details";
format!(
r#"
<entry>
Expand All @@ -134,7 +134,6 @@ fn event_to_atom_entry(event: (Event, Element)) -> String {
</entry>
"#
)
.into()
}

#[get("/new-comments")]
Expand Down Expand Up @@ -235,7 +234,7 @@ fn comment_to_atom_entry(comment: (ElementComment, Element)) -> String {
let comment_id = comment.0.id;
let comment_created_at = comment.0.created_at.format(&Rfc3339).unwrap();
let element_id = comment.1.overpass_data.btcmap_id();
let title = format!("{}", xml_escape(comment.0.comment.clone()));
let title = xml_escape(comment.0.comment.clone());
let summary = xml_escape(comment.0.comment);
format!(
r#"
Expand All @@ -249,7 +248,6 @@ fn comment_to_atom_entry(comment: (ElementComment, Element)) -> String {
</entry>
"#
)
.into()
}

fn xml_escape(str: String) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/log/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ fn log_summary(req: &HttpRequest, endpoint_id: &str, entities: i64, time_ns: i64
};
let today = OffsetDateTime::now_utc().date().to_string();
CONN.with(|conn| {
match summary::select(&today, &addr, endpoint_id, &conn)? {
match summary::select(&today, addr, endpoint_id, conn)? {
Some(entry) => summary::update(
entry.id,
entry.reqests + 1,
entry.entities + entities,
entry.time_ns + time_ns,
&conn,
conn,
),
None => summary::insert(&today, &addr, endpoint_id, 1, entities, time_ns, &conn),
None => summary::insert(&today, addr, endpoint_id, 1, entities, time_ns, conn),
}?;
Ok(())
})
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/boost_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn _boost(admin_id: i64, id_or_osm_id: &str, days: i64, conn: &Connection) -> Re
element.id,
"boost:expires",
&Value::String(boost_expires.format(&Iso8601::DEFAULT)?),
&conn,
conn,
)?;
Boost::insert(admin_id, element.id, days, conn)?;
Ok(element)
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/generate_element_categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn generate_element_categories(
let old_category = element.tag("category").as_str().unwrap_or_default();
let new_category = element.overpass_data.generate_category();
if old_category != new_category {
Element::set_tag(element.id, "category", &new_category.clone().into(), &conn)?;
Element::set_tag(element.id, "category", &new_category.clone().into(), conn)?;
changes += 1;
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/rpc/generate_element_icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn generate_element_icons(
let old_icon = element.tag("icon:android").as_str().unwrap_or_default();
let new_icon = element.overpass_data.generate_android_icon();
if old_icon != new_icon {
Element::set_tag(element.id, "icon:android", &new_icon.clone().into(), &conn)?;
Element::set_tag(element.id, "icon:android", &new_icon.clone().into(), conn)?;
changes += 1;
}
}
Expand Down Expand Up @@ -94,39 +94,39 @@ impl OverpassElement {

let mut icon_id: &str = "question_mark";

if shop != "" {
if !shop.is_empty() {
icon_id = "storefront";
}

if office != "" {
if !office.is_empty() {
icon_id = "business"
}

if healthcare != "" {
if !healthcare.is_empty() {
icon_id = "medical_services"
}

if craft != "" {
if !craft.is_empty() {
icon_id = "construction"
}

if playground != "" {
if !playground.is_empty() {
icon_id = "attractions"
}

if industrial != "" {
if !industrial.is_empty() {
icon_id = "factory"
}

if attraction != "" {
if !attraction.is_empty() {
icon_id = "attractions"
}

if shelter_type != "" {
if !shelter_type.is_empty() {
icon_id = "roofing"
}

if aeroway != "" {
if !aeroway.is_empty() {
icon_id = "paragliding"
}

Expand Down
Loading

0 comments on commit db3fc1b

Please sign in to comment.