Skip to content

Commit c232826

Browse files
authored
Include the macro (#973)
1 parent 295be83 commit c232826

File tree

8 files changed

+103
-6
lines changed

8 files changed

+103
-6
lines changed

pgml-apps/cargo-pgml-components/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cargo-pgml-components"
3-
version = "0.1.10"
3+
version = "0.1.11"
44
edition = "2021"
55
authors = ["PostgresML <team@postgresml.org>"]
66
license = "MIT"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%- value %>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#![allow(dead_code, unused_macros, unused_imports)]
2+
//! A basic UI component. Any other component can accept this
3+
//! as a parameter and render it.
4+
5+
use sailfish::TemplateOnce;
6+
7+
#[derive(Default, Clone, TemplateOnce)]
8+
#[template(path = "components/component.html")]
9+
pub struct Component {
10+
pub value: String,
11+
}
12+
13+
macro_rules! component {
14+
($name:tt) => {
15+
impl From<$name> for crate::components::Component {
16+
fn from(thing: $name) -> crate::components::Component {
17+
use sailfish::TemplateOnce;
18+
19+
crate::components::Component {
20+
value: thing.render_once().unwrap(),
21+
}
22+
}
23+
}
24+
};
25+
26+
($name:tt, $lifetime:lifetime) => {
27+
impl<$lifetime> From<$name<$lifetime>> for crate::components::Component {
28+
fn from(thing: $name<$lifetime>) -> crate::components::Component {
29+
use sailfish::TemplateOnce;
30+
31+
crate::components::Component {
32+
value: thing.render_once().unwrap(),
33+
}
34+
}
35+
}
36+
};
37+
}
38+
39+
pub(crate) use component;
40+
41+
// Render any string.
42+
impl From<&str> for Component {
43+
fn from(value: &str) -> Component {
44+
Component {
45+
value: value.to_owned(),
46+
}
47+
}
48+
}
49+
50+
// Render any string.
51+
impl From<String> for Component {
52+
fn from(value: String) -> Component {
53+
Component { value }
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::util::{compare_strings, info, unwrap_or_exit, write_to_file};
2+
use std::fs::read_to_string;
3+
use std::path::Path;
4+
5+
static COMPONENT_PATH: &str = "src/components/component.rs";
6+
static COMPONENT_TEMPLATE: &str = "templates/components/component.html";
7+
8+
pub mod component;
9+
10+
pub fn install() {
11+
let source = include_str!("component.rs");
12+
let template = include_str!("component.html");
13+
14+
compare_and_install(Path::new(COMPONENT_PATH), source);
15+
compare_and_install(Path::new(COMPONENT_TEMPLATE), template);
16+
}
17+
18+
fn compare_and_install(path: &Path, source: &str) {
19+
if !path.exists() {
20+
debug!("{} doesn't exist", path.display());
21+
info(&format!("written {}", path.display()));
22+
unwrap_or_exit!(write_to_file(&path, &source));
23+
} else {
24+
let template_source = unwrap_or_exit!(read_to_string(path));
25+
26+
if !compare_strings(&template_source, source) {
27+
debug!("{} is different", path.display());
28+
unwrap_or_exit!(write_to_file(&path, &source));
29+
info(&format!("written {}", path.display()));
30+
}
31+
}
32+
}

pgml-apps/cargo-pgml-components/src/frontend/components.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn update_modules() {
133133
let modules = unwrap_or_exit!(templates::Mod { modules }.render_once());
134134
let existing_modules = unwrap_or_exit!(read_to_string(COMPONENT_MOD));
135135

136-
if !unwrap_or_exit!(compare_strings(&modules, &existing_modules)) {
136+
if !compare_strings(&modules, &existing_modules) {
137137
debug!("mod.rs is different");
138138
unwrap_or_exit!(write_to_file(&Path::new(COMPONENT_MOD), &modules));
139139
info(&format!("written {}", COMPONENT_MOD));

pgml-apps/cargo-pgml-components/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,19 @@ use std::path::Path;
88
#[macro_use]
99
extern crate log;
1010

11+
mod components;
1112
mod frontend;
1213
mod util;
1314
use util::{info, unwrap_or_exit};
1415

1516
/// These paths are exepcted to exist in the project directory.
16-
static PROJECT_PATHS: &[&str] = &["src", "static/js", "static/css"];
17+
static PROJECT_PATHS: &[&str] = &[
18+
"src",
19+
"static/js",
20+
"static/css",
21+
"templates/components",
22+
"src/components",
23+
];
1724

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

100107
unwrap_or_exit!(set_current_dir(path));
108+
components::install();
101109
}
102110

103111
/// Bundle SASS and JavaScript into neat bundle files.

pgml-apps/cargo-pgml-components/src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ pub fn compare_files(path1: &Path, path2: &Path) -> std::io::Result<bool> {
8484
let content1 = read_to_string(path1)?;
8585
let content2 = read_to_string(path2)?;
8686

87-
compare_strings(&content1, &content2)
87+
Ok(compare_strings(&content1, &content2))
8888
}
8989

90-
pub fn compare_strings(string1: &str, string2: &str) -> std::io::Result<bool> {
90+
pub fn compare_strings(string1: &str, string2: &str) -> bool {
9191
// TODO: faster string comparison method needed.
92-
Ok(string1 == string2)
92+
string1.trim() == string2.trim()
9393
}

pgml-dashboard/src/components/component.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(dead_code, unused_macros, unused_imports)]
12
//! A basic UI component. Any other component can accept this
23
//! as a parameter and render it.
34

0 commit comments

Comments
 (0)