summaryrefslogtreecommitdiff
path: root/src/resources/style.rs
diff options
context:
space:
mode:
authormrw1593 <botahamec@outlook.com>2023-05-15 21:42:47 -0400
committermrw1593 <botahamec@outlook.com>2023-05-29 10:46:04 -0400
commite38854c7db0fe6f006304d7f638b6aa190fc2d87 (patch)
treeeb730768a5f9c68f20c3b20a180cb1ab327a2de5 /src/resources/style.rs
parent3f0cfb69f2ab1dda4425fe871a6cbf3b4bfb8dc3 (diff)
Started on frontend
Diffstat (limited to 'src/resources/style.rs')
-rw-r--r--src/resources/style.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/resources/style.rs b/src/resources/style.rs
new file mode 100644
index 0000000..2777a82
--- /dev/null
+++ b/src/resources/style.rs
@@ -0,0 +1,53 @@
+use std::path::{Path, PathBuf};
+
+use actix_web::{get, http::StatusCode, web, HttpResponse, ResponseError};
+use exun::{Expect, ResultErrorExt};
+use grass::OutputStyle;
+use raise::yeet;
+use serde::Serialize;
+use thiserror::Error;
+
+fn output_style() -> OutputStyle {
+ if cfg!(debug_assertions) {
+ OutputStyle::Expanded
+ } else {
+ OutputStyle::Compressed
+ }
+}
+
+fn options() -> grass::Options<'static> {
+ grass::Options::default()
+ .load_path("static/style")
+ .style(output_style())
+}
+
+#[derive(Debug, Clone, Error, Serialize)]
+pub enum LoadStyleError {
+ #[error("The requested stylesheet was not found")]
+ FileNotFound(Box<Path>),
+}
+
+impl ResponseError for LoadStyleError {
+ fn status_code(&self) -> StatusCode {
+ match self {
+ Self::FileNotFound(..) => StatusCode::NOT_FOUND,
+ }
+ }
+}
+
+pub fn load(stylesheet: &str) -> Result<String, Expect<LoadStyleError>> {
+ let options = options();
+ let path = PathBuf::from(format!("static/style/{}.scss", stylesheet));
+ if !path.exists() {
+ yeet!(LoadStyleError::FileNotFound(path.into()).into());
+ }
+ let css = grass::from_path(format!("static/style/{}.scss", stylesheet), &options).unexpect()?;
+ Ok(css)
+}
+
+#[get("/{stylesheet}.css")]
+pub async fn get_css(stylesheet: web::Path<Box<str>>) -> Result<HttpResponse, LoadStyleError> {
+ let css = load(&stylesheet).map_err(|e| e.unwrap())?;
+ let response = HttpResponse::Ok().content_type("text/css").body(css);
+ Ok(response)
+}