Skip to content

Commit

Permalink
fixed minor fuckups
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Jul 2, 2024
1 parent ee602c1 commit e0572b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
12 changes: 7 additions & 5 deletions server/main-api/src/limited/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ impl<K: Eq + Hash, V> From<HashMap<K, V>> for LimitedHashMap<K, V> {
}

const LIMIT: usize = 3;
impl<K: fmt::Debug + Eq + Hash +Clone+Ord, V: fmt::Debug+Clone> fmt::Debug for LimitedHashMap<K, V> {
impl<K: fmt::Debug + Eq + Hash + Clone + Ord, V: fmt::Debug + Clone> fmt::Debug
for LimitedHashMap<K, V>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut collection = self.0.clone().into_iter().collect::<Vec<(K,V)>>();
collection.sort_unstable_by(|(k1,_),(k2,_)|k1.cmp(k2));
let mut collection = self.0.clone().into_iter().collect::<Vec<(K, V)>>();
collection.sort_unstable_by(|(k1, _), (k2, _)| k1.cmp(k2));
if self.0.len() <= LIMIT {
f.debug_map().entries(collection).finish()
} else {
Expand All @@ -28,8 +30,8 @@ impl<K: fmt::Debug + Eq + Hash +Clone+Ord, V: fmt::Debug+Clone> fmt::Debug for L
collection
.into_iter()
.take(LIMIT)
.map(|(k,v)|(OrMore::Value(k),OrMore::Value(v)))
.chain([(OrMore::More,OrMore::More)])
.map(|(k, v)| (OrMore::Value(k), OrMore::Value(v)))
.chain([(OrMore::More, OrMore::More)]),
)
.finish()
}
Expand Down
16 changes: 10 additions & 6 deletions server/main-api/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tokio::time::Instant;
use tracing::{debug, error, warn};
use unicode_truncate::UnicodeTruncateStr;

use crate::limited::vec::LimitedVec;
use crate::maps::overlay_map::OverlayMapTask;
use crate::maps::overlay_text::{OverlayText, CANTARELL_BOLD, CANTARELL_REGULAR};
use crate::models::Location;
Expand Down Expand Up @@ -61,7 +62,10 @@ async fn get_localised_data(
}

#[tracing::instrument]
async fn construct_image_from_data(data: Location, format: PreviewFormat) -> Option<Vec<u8>> {
async fn construct_image_from_data(
data: Location,
format: PreviewFormat,
) -> Option<LimitedVec<u8>> {
let start_time = Instant::now();
let mut img = match format {
PreviewFormat::OpenGraph => image::RgbaImage::new(1200, 630),
Expand Down Expand Up @@ -91,10 +95,10 @@ fn draw_pin(img: &mut ImageBuffer<Rgba<u8>, Vec<u8>>) {
);
}

fn wrap_image_in_response(img: &image::RgbaImage) -> Vec<u8> {
fn wrap_image_in_response(img: &image::RgbaImage) -> LimitedVec<u8> {
let mut w = Cursor::new(Vec::new());
img.write_to(&mut w, image::ImageFormat::Png).unwrap();
w.into_inner()
LimitedVec(w.into_inner())
}
const WHITE_PIXEL: Rgba<u8> = Rgba([255, 255, 255, 255]);
fn draw_bottom(data: &Location, img: &mut image::RgbaImage) {
Expand Down Expand Up @@ -125,13 +129,13 @@ fn draw_bottom(data: &Location, img: &mut image::RgbaImage) {
.draw_onto(img);
}

fn load_default_image() -> Vec<u8> {
fn load_default_image() -> LimitedVec<u8> {
warn!("Loading default preview image, as map rendering failed. Check the connection to the tileserver");
let img = image::load_from_memory(include_bytes!("static/logo-card.png")).unwrap();
// encode the image as PNG
let mut w = Cursor::new(Vec::new());
img.write_to(&mut w, image::ImageFormat::Png).unwrap();
w.into_inner()
LimitedVec(w.into_inner())
}

#[tracing::instrument(skip(pool))]
Expand Down Expand Up @@ -216,5 +220,5 @@ pub async fn maps_handler(
"Preview Generation for {id} took {elapsed:?}",
elapsed = start_time.elapsed()
);
HttpResponse::Ok().content_type("image/png").body(img)
HttpResponse::Ok().content_type("image/png").body(img.0)
}

0 comments on commit e0572b3

Please sign in to comment.