Skip to content

Commit

Permalink
Recudes the usage of String in assets
Browse files Browse the repository at this point in the history
  • Loading branch information
cuducos committed Jun 6, 2024
1 parent a3fa87f commit 8e65889
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
44 changes: 18 additions & 26 deletions src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
use std::collections::HashMap;

use anyhow::Result;
use lazy_static::lazy_static;
use serde::Serialize;

use crate::whisky::WHISKIES;

lazy_static! {
pub static ref ASSETS: HashMap<String, Vec<u8>> = {
let mut m = HashMap::new();
m.insert(
"style.css".to_string(),
include_str!("static/style.css").to_string().into_bytes(),
);
m.insert(
"app.js".to_string(),
include_str!("static/app.js").to_string().into_bytes(),
);
m.insert(
"robots.txt".to_string(),
include_str!("static/robots.txt").to_string().into_bytes(),
);
m.insert(
"favicon.ico".to_string(),
include_bytes!("static/favicon.ico").to_vec(),
);
m
};
pub static ref CSS: &'static [u8] = include_bytes!("static/style.css");
pub static ref JS: &'static [u8] = include_bytes!("static/app.js");
pub static ref BOTS: &'static [u8] = include_bytes!("static/robots.txt");
pub static ref FAVICON: &'static [u8] = include_bytes!("static/favicon.ico");
}

pub fn by_name(name: &str) -> Option<&[u8]> {
match name {
"style.css" => Some(CSS.as_ref()),
"app.js" => Some(JS.as_ref()),
"robots.txt" => Some(BOTS.as_ref()),
"favicon.ico" => Some(FAVICON.as_ref()),
_ => None,
}
}

#[derive(Serialize)]
pub struct AutocompleteData {
pub whiskies: Vec<String>,
pub struct AutocompleteData<'a> {
pub whiskies: Vec<&'a str>,
}

impl AutocompleteData {
impl<'a> AutocompleteData<'a> {
pub fn new() -> Self {
Self {
whiskies: WHISKIES.iter().map(|w| w.distillery.clone()).collect(),
whiskies: WHISKIES.iter().map(|w| w.distillery.as_str()).collect(),
}
}
pub fn as_json(&self) -> Result<String> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn asset(py: Python, name: &str) -> PyResult<(PyObject, String)> {
if name == "whiskyton.json" {
bytes = &whisky::AUTOCOMPLETE;
}
if let Some(b) = assets::ASSETS.get(name) {
if let Some(b) = assets::by_name(name) {
bytes = b
}
if bytes.is_empty() {
Expand Down

0 comments on commit 8e65889

Please sign in to comment.