Skip to content

Include the macro #973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pgml-apps/cargo-pgml-components/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-pgml-components"
version = "0.1.10"
version = "0.1.11"
edition = "2021"
authors = ["PostgresML <team@postgresml.org>"]
license = "MIT"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%- value %>
55 changes: 55 additions & 0 deletions pgml-apps/cargo-pgml-components/src/components/component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![allow(dead_code, unused_macros, unused_imports)]
//! A basic UI component. Any other component can accept this
//! as a parameter and render it.

use sailfish::TemplateOnce;

#[derive(Default, Clone, TemplateOnce)]
#[template(path = "components/component.html")]
pub struct Component {
pub value: String,
}

macro_rules! component {
($name:tt) => {
impl From<$name> for crate::components::Component {
fn from(thing: $name) -> crate::components::Component {
use sailfish::TemplateOnce;

crate::components::Component {
value: thing.render_once().unwrap(),
}
}
}
};

($name:tt, $lifetime:lifetime) => {
impl<$lifetime> From<$name<$lifetime>> for crate::components::Component {
fn from(thing: $name<$lifetime>) -> crate::components::Component {
use sailfish::TemplateOnce;

crate::components::Component {
value: thing.render_once().unwrap(),
}
}
}
};
}

pub(crate) use component;

// Render any string.
impl From<&str> for Component {
fn from(value: &str) -> Component {
Component {
value: value.to_owned(),
}
}
}

// Render any string.
impl From<String> for Component {
fn from(value: String) -> Component {
Component { value }
}
}
32 changes: 32 additions & 0 deletions pgml-apps/cargo-pgml-components/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::util::{compare_strings, info, unwrap_or_exit, write_to_file};
use std::fs::read_to_string;
use std::path::Path;

static COMPONENT_PATH: &str = "src/components/component.rs";
static COMPONENT_TEMPLATE: &str = "templates/components/component.html";

pub mod component;

pub fn install() {
let source = include_str!("component.rs");
let template = include_str!("component.html");

compare_and_install(Path::new(COMPONENT_PATH), source);
compare_and_install(Path::new(COMPONENT_TEMPLATE), template);
}

fn compare_and_install(path: &Path, source: &str) {
if !path.exists() {
debug!("{} doesn't exist", path.display());
info(&format!("written {}", path.display()));
unwrap_or_exit!(write_to_file(&path, &source));
} else {
let template_source = unwrap_or_exit!(read_to_string(path));

if !compare_strings(&template_source, source) {
debug!("{} is different", path.display());
unwrap_or_exit!(write_to_file(&path, &source));
info(&format!("written {}", path.display()));
}
}
}
2 changes: 1 addition & 1 deletion pgml-apps/cargo-pgml-components/src/frontend/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn update_modules() {
let modules = unwrap_or_exit!(templates::Mod { modules }.render_once());
let existing_modules = unwrap_or_exit!(read_to_string(COMPONENT_MOD));

if !unwrap_or_exit!(compare_strings(&modules, &existing_modules)) {
if !compare_strings(&modules, &existing_modules) {
debug!("mod.rs is different");
unwrap_or_exit!(write_to_file(&Path::new(COMPONENT_MOD), &modules));
info(&format!("written {}", COMPONENT_MOD));
Expand Down
10 changes: 9 additions & 1 deletion pgml-apps/cargo-pgml-components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ use std::path::Path;
#[macro_use]
extern crate log;

mod components;
mod frontend;
mod util;
use util::{info, unwrap_or_exit};

/// These paths are exepcted to exist in the project directory.
static PROJECT_PATHS: &[&str] = &["src", "static/js", "static/css"];
static PROJECT_PATHS: &[&str] = &[
"src",
"static/js",
"static/css",
"templates/components",
"src/components",
];

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, propagate_version = true, bin_name = "cargo", name = "cargo")]
Expand Down Expand Up @@ -98,6 +105,7 @@ fn validate_project(project_path: Option<String>) {
}

unwrap_or_exit!(set_current_dir(path));
components::install();
}

/// Bundle SASS and JavaScript into neat bundle files.
Expand Down
6 changes: 3 additions & 3 deletions pgml-apps/cargo-pgml-components/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ pub fn compare_files(path1: &Path, path2: &Path) -> std::io::Result<bool> {
let content1 = read_to_string(path1)?;
let content2 = read_to_string(path2)?;

compare_strings(&content1, &content2)
Ok(compare_strings(&content1, &content2))
}

pub fn compare_strings(string1: &str, string2: &str) -> std::io::Result<bool> {
pub fn compare_strings(string1: &str, string2: &str) -> bool {
// TODO: faster string comparison method needed.
Ok(string1 == string2)
string1.trim() == string2.trim()
}
1 change: 1 addition & 0 deletions pgml-dashboard/src/components/component.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code, unused_macros, unused_imports)]
//! A basic UI component. Any other component can accept this
//! as a parameter and render it.

Expand Down