From 574d14714d56d26536929ca73e945fa10352ca14 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sun, 16 Jan 2022 14:14:58 +0530 Subject: [PATCH] fix: render only specified gists, if specification is provided --- src/data.rs | 65 ++++++++++++++++++++++++++++++-------- src/proxy.rs | 3 +- templates/gist_insert.html | 8 ++--- 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/data.rs b/src/data.rs index 6be4bd3..516b8b4 100644 --- a/src/data.rs +++ b/src/data.rs @@ -50,10 +50,6 @@ pub type PostResp = get_post::GetPostPost; pub type AppData = web::Data; impl PostResp { - pub fn get_gist_id<'a>(&self, url: &'a str) -> &'a str { - url.split('/').last().unwrap() - } - pub fn get_subtitle(&self) -> &str { self.preview_content.as_ref().unwrap().subtitle.as_str() } @@ -65,7 +61,7 @@ pub struct GistContent { pub html_url: String, } -#[derive(Deserialize, Serialize)] +#[derive(Deserialize, Clone, Serialize)] pub struct GistFile { pub file_name: String, pub content: String, @@ -191,9 +187,25 @@ impl Data { } } - pub async fn get_gist(&self, id: String) -> (String, GistContent) { - match self.gists.get(&id) { - Ok(Some(v)) => (id, bincode::deserialize(&v[..]).unwrap()), + pub fn get_gist_id(url: &str) -> &str { + url.split('/').last().unwrap() + } + + pub async fn get_gist(&self, gist_url: String) -> (String, GistContent) { + let id = Self::get_gist_id(&gist_url).to_owned(); + let file_name = if gist_url.contains('?') { + let parsed = url::Url::parse(&gist_url).unwrap(); + if let Some((_, file_name)) = parsed.query_pairs().find(|(k, _)| k == "file") { + Some(file_name.into_owned()) + } else { + None + } + } else { + None + }; + + let gist = match self.gists.get(&id) { + Ok(Some(v)) => bincode::deserialize(&v[..]).unwrap(), _ => { const URL: &str = "https://api.github.com/gists/"; @@ -211,9 +223,9 @@ impl Data { .unwrap(); let files = resp.get("files").unwrap(); let v = files.as_object().unwrap(); - let mut files = Vec::with_capacity(v.len()); - v.iter().for_each(|(name, file_obj)| { - let file = GistFile { + + fn to_gist_file(name: &str, file_obj: &serde_json::Value) -> GistFile { + GistFile { file_name: name.to_string(), content: file_obj .get("content") @@ -233,9 +245,15 @@ impl Data { .as_str() .unwrap() .to_owned(), - }; + } + } + + let mut files = Vec::with_capacity(v.len()); + v.iter().for_each(|(name, file_obj)| { + let file = to_gist_file(name, file_obj); files.push(file); }); + let gist = GistContent { files, html_url: resp.get("html_url").unwrap().as_str().unwrap().to_owned(), @@ -244,8 +262,27 @@ impl Data { self.gists .insert(&id, bincode::serialize(&gist).unwrap()) .unwrap(); - (id, gist) + gist } - } + }; + + let gist = if let Some(file_name) = file_name { + let mut files: Vec = Vec::with_capacity(1); + let file = gist + .files + .iter() + .find(|f| f.file_name == file_name) + .unwrap() + .to_owned(); + files.push(file); + GistContent { + files, + html_url: gist_url, + } + } else { + gist + }; + + (id, gist) } } diff --git a/src/proxy.rs b/src/proxy.rs index 45a2d3c..0475efa 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -185,8 +185,7 @@ async fn page(path: web::Path<(String, String)>, data: AppData) -> impl Responde .unwrap() .href; if src.contains("gist.github.com") { - let gist_id = post_data.get_gist_id(src); - let fut = data.get_gist(gist_id.to_owned()); + let fut = data.get_gist(src.to_owned()); futs.push(fut); } } diff --git a/templates/gist_insert.html b/templates/gist_insert.html index c2b29c5..b897e91 100644 --- a/templates/gist_insert.html +++ b/templates/gist_insert.html @@ -1,9 +1,9 @@ -<. let gist_id = data.get_gist_id(src); .> +<. let gist_id = crate::data::Data::get_gist_id(src); .> <. let (_, gist)= gists.as_ref().unwrap().iter().find(|(id, _)| id == gist_id).as_ref().unwrap(); .>
-<. for file in &gist.files {.> - <.= file.get_html_content() .> -<.}.> + <. for file in &gist.files {.> + <.= file.get_html_content() .> + <.}.> See gist on GitHub