diff --git a/Cargo.lock b/Cargo.lock index 7fbfc73..7fe0501 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -485,6 +485,11 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "quote" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "quote" version = "0.6.12" @@ -599,7 +604,7 @@ dependencies = [ [[package]] name = "realm" -version = "0.1.7" +version = "0.1.8" dependencies = [ "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -611,6 +616,7 @@ dependencies = [ "itertools 0.7.11 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-embed 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -622,6 +628,35 @@ name = "redox_syscall" version = "0.1.54" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rust-embed" +version = "5.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rust-embed-impl 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-embed-utils 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rust-embed-impl" +version = "5.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-embed-utils 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rust-embed-utils" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rustc-demangle" version = "0.1.14" @@ -640,6 +675,14 @@ name = "ryu" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "same-file" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "scopeguard" version = "0.3.3" @@ -703,6 +746,16 @@ name = "string" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "syn" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "syn" version = "0.15.33" @@ -713,6 +766,14 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "synom" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "synstructure" version = "0.10.1" @@ -886,6 +947,11 @@ dependencies = [ "smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-xid" +version = "0.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -901,6 +967,16 @@ dependencies = [ "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "walkdir" +version = "2.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "want" version = "0.2.0" @@ -935,6 +1011,14 @@ name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" @@ -1008,6 +1092,7 @@ dependencies = [ "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" "checksum pretty_assertions 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a029430f0d744bc3d15dd474d591bed2402b645d024583082b9f63bb936dac6" "checksum proc-macro2 0.4.29 (registry+https://github.com/rust-lang/crates.io-index)" = "64c827cea7a7ab30ce4593e5e04d7a11617ad6ece2fa230605a78b00ff965316" +"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" "checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" @@ -1021,9 +1106,13 @@ dependencies = [ "checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252" +"checksum rust-embed 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8defb88737955626d62a4c7d90e2acf552a79b2f5bd7526cd46697f2d98eb45c" +"checksum rust-embed-impl 5.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d88346d83619c94beddba4f2ffee53c4cd006afd44c303182c7bcc8b6abe5ae" +"checksum rust-embed-utils 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97655158074ccb2d2cfb1ccb4c956ef0f4054e43a2c1e71146d4991e6961e105" "checksum rustc-demangle 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "ccc78bfd5acd7bf3e89cffcf899e5cb1a52d6fafa8dec2739ad70c9577a57288" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b96a9549dc8d48f2c283938303c4b5a77aa29bfbc5b54b084fb1630408899a8f" +"checksum same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "585e8ddcedc187886a30fa705c47985c3fa88d06624095856b36ca0b82ff4421" "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" @@ -1034,7 +1123,9 @@ dependencies = [ "checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b639411d0b9c738748b5397d5ceba08e648f4f1992231aa859af1a017f31f60b" +"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)" = "ec52cd796e5f01d0067225a5392e70084acc4c0013fa71d55166d38a8b307836" +"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tokio 0.1.18 (registry+https://github.com/rust-lang/crates.io-index)" = "65641e515a437b308ab131a82ce3042ff9795bef5d6c5a9be4eb24195c417fd9" @@ -1051,12 +1142,15 @@ dependencies = [ "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426" +"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" +"checksum walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9658c94fa8b940eab2250bd5a457f9c48b748420d71293b165c8cdbe2f55f71e" "checksum want 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b6395efa4784b027708f7451087e647ec73cc74f5d9bc2e418404248d679a230" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/Cargo.toml b/Cargo.toml index bdef827..b6280bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "realm" -version = "0.1.7" +version = "0.1.8" authors = ["Amit Upadhyay "] description = "Rust / Elm base full stack web framework." license = "BSD-2-Clause" @@ -19,6 +19,7 @@ failure = "~0.1.1" url = "~1" htmlescape = "0.3.1" itertools = "~0.7.8" +rust-embed = "5.1.0" [dependencies.chrono] diff --git a/realm-cli/realm_cli/compile_elm.py b/realm-cli/realm_cli/compile_elm.py index be13deb..32b9605 100644 --- a/realm-cli/realm_cli/compile_elm.py +++ b/realm-cli/realm_cli/compile_elm.py @@ -6,28 +6,21 @@ home: str = expanduser("~") -elm_path_G: str = "forgit/dwelm/elm_latest/node_modules/elm/bin/elm" -elm_path_G = home + "/" + elm_path_G -elm_format_path_G: str = "forgit/dwelm/elm_latest/node_modules/elm-format/bin/elm-format" -elm_format_path_G = home + "/" + elm_format_path_G - -go_to_dir_G: str = home + "/forgit/dwelm/graftpress/" - - -def compile( +def compile_( source_path: str, destination_path: str, - elm_path: str = elm_path_G, - elm_format_path: str = elm_format_path_G, - elm_proj_dir: str = go_to_dir_G, + elm_path: str, + elm_format_path: str, + elm_proj_dir: str, ): if elm_proj_dir: os.chdir(elm_proj_dir) print("source dest ", os.getcwd(), source_path, destination_path) os.system(elm_format_path + " --yes " + source_path) - os.system(elm_path + " make " + source_path + " --output " + destination_path) + os.system(elm_path + " make " + source_path + " --output " + + destination_path) def has_main(file: str) -> bool: @@ -37,7 +30,7 @@ def has_main(file: str) -> bool: reg_st = r"\s+?main\s*?=\s*?(Browser[.])?((sandbox)|(element))\s*\{.*?\}" c = re.compile(reg_st, re.DOTALL) search_result: Optional[Match[str]] = c.search(content_st) - return search_result != None + return search_result is not None def populate_set(bucket: Set[str], source_dir: str, root_path: str): @@ -65,9 +58,9 @@ def check_conflicts(source_dirs: List[str]): def compile_all_elm( source_dir: str, destination_dir: str, - elm_path: str = elm_path_G, - elm_format_path: str = elm_format_path_G, - go_to_dir: str = go_to_dir_G, + elm_path: str, + elm_format_path: str, + go_to_dir: str, ): # handle error @@ -92,7 +85,7 @@ def compile_all_elm( if file_extension == ".elm" and has_main(source_path): dest_path = destination_dir + "/" + filename + ".js" - compile(source_path, dest_path, elm_path, elm_format_path, go_to_dir) + compile_(source_path, dest_path, elm_path, elm_format_path, go_to_dir) if __name__ == "__main__": diff --git a/realm-cli/realm_cli/rust_json.py b/realm-cli/realm_cli/rust_json.py index e502e80..e4386dc 100644 --- a/realm-cli/realm_cli/rust_json.py +++ b/realm-cli/realm_cli/rust_json.py @@ -1,4 +1,5 @@ import os + import re from typing import List, Tuple, Optional, Match from string import Template @@ -14,7 +15,7 @@ """ FORWARD_TEMPLATE = """pub fn magic(%s) -> realm::Result {%s - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() {%s } } @@ -85,7 +86,8 @@ def get_routes(test_dir: Optional[str] = None): path = f"/{routePath}/{routeName}/" module = routePath.replace("/", "::") + "::" + routeName print("module", module) - routes.append((path, module, parse(root + "/" + fileName)[1:])) + parse_res = parse(root + "/" + fileName) + routes.append((path, module, parse_res[0][1:], parse_res[1] )) routes.sort(reverse=True) return routes @@ -95,14 +97,14 @@ def generate_forward(directories, routes, test_dir=None): # filter routes and directories based on whitelist forward = "" - + print(routes) if "context" in REALM_CONFIG: ireq_type = REALM_CONFIG["context"] else: print("'context' key is absent in realm.json") ireq_type = None - for (url, mod, args) in routes: + for (url, mod, args, is_layout_present) in routes: if url == "/" and mod != "index": url += mod + "/" @@ -165,10 +167,12 @@ def generate_forward(directories, routes, test_dir=None): def generate_reverse( - routes: List[Tuple[str, str, List[Tuple[str, str]]]], test_dir: Optional[str] = None + routes: (List[Tuple[str, str, List[Tuple[str, str]]]], bool), test_dir: Optional[str] = None ) -> str: reverse: str = "" - for (url, mod, args) in routes: + for (url, mod, args, is_layout_present) in routes: + if not is_layout_present: + return "" if url == "/": function_name = mod if mod != "index": @@ -177,7 +181,7 @@ def generate_reverse( function_name = url[1:].replace("/", "_").replace("_index", "") if function_name.endswith("_"): function_name = function_name[:-1] - + print("args ", args) if len(args) == 0: reverse += """ pub fn %s() -> String { @@ -213,7 +217,7 @@ def generate_reverse( return reverse_content -def parse(mod_path: str) -> List[Tuple[str, str]]: +def parse(mod_path: str) -> (List[Tuple[str, str]], bool): """ Given a module name (file path, relative to ., eg src/acko/utils.rs), this function returns: @@ -222,13 +226,21 @@ def parse(mod_path: str) -> List[Tuple[str, str]]: """ with open(mod_path, "r") as f: + file_content = f.read() + if not re.search(r'.*?(pub fn layout)', file_content ): + return ([], False) + args_str_: Optional[Match[str]] = re.compile( r"(?<=pub fn layout\()[^{]*(?=\) ->)" - ).search(f.read().replace("\n", " ")) + ).search(file_content.replace("\n", " ")) args_str: str = "" if not args_str_ else args_str_[0].replace(" ", "") - return [ + + + return ([ (r.split(":")[0], r.split(":")[1]) for r in args_str.split(",") if r != "" - ] + ], True) + + return ([], False) def main() -> None: @@ -254,7 +266,7 @@ def test() -> None: print(i) gen_reverse_content = generate_reverse(r, test_dir=test_dir) reverse_content = open(test_dir + "/reverse.rs").read() - + print("gen reverse content is", gen_reverse_content) try: assert gen_reverse_content.strip() == reverse_content.strip() except: diff --git a/realm-cli/setup.py b/realm-cli/setup.py index 410acd9..d79d917 100644 --- a/realm-cli/setup.py +++ b/realm-cli/setup.py @@ -9,7 +9,7 @@ setup( name="graftpress_cli", - version="0.0.10", + version="0.0.11", entry_points={"console_scripts": ["realm-cli=graftpress_cli.main:main"]}, author="nilinswap", author_email="nilinswap@gmail.com", diff --git a/realm-cli/tests/00_m_basic/forward.rs b/realm-cli/tests/00_m_basic/forward.rs new file mode 100644 index 0000000..f97d87d --- /dev/null +++ b/realm-cli/tests/00_m_basic/forward.rs @@ -0,0 +1,7 @@ +pub fn magic(req: realm::Request) -> realm::Result { + let input = realm::request_config::RequestConfig::new(req)?; + match input.path.as_str() { + "/" => crate::routes::index::layout(&input.req), + _ => unimplemented!(), + } +} \ No newline at end of file diff --git a/realm-cli/tests/00_m_basic/realm.json b/realm-cli/tests/00_m_basic/realm.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/realm-cli/tests/00_m_basic/realm.json @@ -0,0 +1 @@ +{} diff --git a/realm-cli/tests/00_m_basic/reverse.rs b/realm-cli/tests/00_m_basic/reverse.rs new file mode 100644 index 0000000..3f2ff2d --- /dev/null +++ b/realm-cli/tests/00_m_basic/reverse.rs @@ -0,0 +1,5 @@ + + + + + diff --git a/realm-cli/tests/00_m_basic/routes/mod.rs b/realm-cli/tests/00_m_basic/routes/mod.rs new file mode 100644 index 0000000..33edc95 --- /dev/null +++ b/realm-cli/tests/00_m_basic/routes/mod.rs @@ -0,0 +1 @@ +pub mod index; diff --git a/realm-cli/tests/01_basic/forward.rs b/realm-cli/tests/01_basic/forward.rs index 2ea72e5..f97d87d 100644 --- a/realm-cli/tests/01_basic/forward.rs +++ b/realm-cli/tests/01_basic/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => crate::routes::index::layout(&input.req), _ => unimplemented!(), diff --git a/realm-cli/tests/02_single_arg/forward.rs b/realm-cli/tests/02_single_arg/forward.rs index 92bb349..9354eb8 100644 --- a/realm-cli/tests/02_single_arg/forward.rs +++ b/realm-cli/tests/02_single_arg/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => { let i = input.get("i", false)?; diff --git a/realm-cli/tests/03_two_args/forward.rs b/realm-cli/tests/03_two_args/forward.rs index 884ebd8..6e8d83c 100644 --- a/realm-cli/tests/03_two_args/forward.rs +++ b/realm-cli/tests/03_two_args/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => { let i = input.get("i", false)?; diff --git a/realm-cli/tests/04_order/forward.rs b/realm-cli/tests/04_order/forward.rs index 0200b6a..2145bcd 100644 --- a/realm-cli/tests/04_order/forward.rs +++ b/realm-cli/tests/04_order/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/ab/c/" => crate::routes::ab_c::layout(&input.req), "/ab/" => crate::routes::ab::layout(&input.req), diff --git a/realm-cli/tests/04_order/routes/ab/c.rs b/realm-cli/tests/04_order/routes/ab/c.rs index e69de29..3f0f0fe 100644 --- a/realm-cli/tests/04_order/routes/ab/c.rs +++ b/realm-cli/tests/04_order/routes/ab/c.rs @@ -0,0 +1,7 @@ +use crate::pages::BasicPage; +use crate::widgets::TextWidget; +use realm::{Page, Widget, HTML}; + +pub fn layout(req: &realm::Request) -> realm::Result { + BasicPage::new(req, TextWidget::new(req).widget_spec()?).page(req, HTML::new().title("index")) +} diff --git a/realm-cli/tests/04_order/routes/ab/index.rs b/realm-cli/tests/04_order/routes/ab/index.rs index e69de29..3f0f0fe 100644 --- a/realm-cli/tests/04_order/routes/ab/index.rs +++ b/realm-cli/tests/04_order/routes/ab/index.rs @@ -0,0 +1,7 @@ +use crate::pages::BasicPage; +use crate::widgets::TextWidget; +use realm::{Page, Widget, HTML}; + +pub fn layout(req: &realm::Request) -> realm::Result { + BasicPage::new(req, TextWidget::new(req).widget_spec()?).page(req, HTML::new().title("index")) +} diff --git a/realm-cli/tests/05_maybe/forward.rs b/realm-cli/tests/05_maybe/forward.rs index 5914761..be68339 100644 --- a/realm-cli/tests/05_maybe/forward.rs +++ b/realm-cli/tests/05_maybe/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => { let m = input.get("m", true)?; diff --git a/realm-cli/tests/06_default_catchall/forward.rs b/realm-cli/tests/06_default_catchall/forward.rs index 2ea72e5..f97d87d 100644 --- a/realm-cli/tests/06_default_catchall/forward.rs +++ b/realm-cli/tests/06_default_catchall/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => crate::routes::index::layout(&input.req), _ => unimplemented!(), diff --git a/realm-cli/tests/07_default_context_catchall/forward.rs b/realm-cli/tests/07_default_context_catchall/forward.rs index 38c5a22..872bc71 100644 --- a/realm-cli/tests/07_default_context_catchall/forward.rs +++ b/realm-cli/tests/07_default_context_catchall/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => crate::routes::index::layout(&input.req), url_ => crate::cms::layout(&input.req, crate::cms::get_default_context(), url_), diff --git a/realm-cli/tests/08_custom_context_catchall/forward.rs b/realm-cli/tests/08_custom_context_catchall/forward.rs index 300d7c0..7ac90d1 100644 --- a/realm-cli/tests/08_custom_context_catchall/forward.rs +++ b/realm-cli/tests/08_custom_context_catchall/forward.rs @@ -1,5 +1,5 @@ pub fn magic(req: realm::Request) -> realm::Result { - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => crate::routes::index::layout(&input.req), url_ => crate::cms::layout(&input.req, crate::cms::get_context("cms"), url_), diff --git a/realm-cli/tests/09_middleware/forward.rs b/realm-cli/tests/09_middleware/forward.rs index 700d1d4..2eddb32 100644 --- a/realm-cli/tests/09_middleware/forward.rs +++ b/realm-cli/tests/09_middleware/forward.rs @@ -1,6 +1,6 @@ pub fn magic(ireq: crate::in_::In) -> realm::Result { let req = ireq.realm_request; - let mut input = realm::request_config::RequestConfig::new(req)?; + let input = realm::request_config::RequestConfig::new(req)?; match input.path.as_str() { "/" => crate::routes::index::layout(&input.req), _ => unimplemented!(), diff --git a/scripts/compile_elm.py b/scripts/compile_elm.py deleted file mode 100644 index e4977e0..0000000 --- a/scripts/compile_elm.py +++ /dev/null @@ -1,74 +0,0 @@ -import os -from os.path import expanduser -import re -import json - - -home = expanduser("~") -config = json.loads(open(home+'/forgit/dwelm/realm/examples/basic/realm.json').read()) - -def compile(source_path, destination_path): - - - - - elm_path = 'forgit/dwelm/elm_latest/node_modules/elm/bin/elm' - elm_path = home + '/' + elm_path - elm_format_path = 'forgit/dwelm/elm_latest/node_modules/elm-format/bin/elm-format' - elm_format_path = home + '/' + elm_format_path - - os.chdir(home + "/forgit/dwelm/realm/examples/dl_elm/") - print(os.getcwd(), source_path, destination_path) - os.system(elm_format_path + " --yes " + source_path) - os.system( - elm_path + " make " + source_path + " --output " + destination_path) - - - -def has_main( file ): - with open(file) as f: - content_st = f.read() - - - - reg_st = r"\s+?main\s*?=\s*?(Browser[.])?((sandbox)|(element))\s*\{.*?\}" - c = re.compile(reg_st, re.DOTALL) - search_result = c.search(content_st) - return (search_result != None) - -def compile_all_elm(source_dir, destination_dir): - - #handle error - for file in os.listdir(source_dir): - print(file) - source_path = source_dir+ '/'+file - dest_path = destination_dir + '/' + file - #if file is already present handle - - - if os.path.isdir(source_path): - - if not os.path.isdir(dest_path): - - os.mkdir(dest_path) - - compile_all_elm(source_path, dest_path) - - filename, file_extension = os.path.splitext(file) - if file_extension == '.elm' and has_main(source_path): - dest_path = destination_dir + '/'+ filename+".js" - compile(source_path, dest_path) - - - -source_dir = home + "/forgit/dwelm/realm/examples/dl_elm/src" - -static_dir = "../" -if 'static_dir' in config: - static_dir = config['static_dir'] -destination_dir = (+"/realm/"+open("latest.txt", "r").read()).strip() -print(destination_dir) - -print(compile_all_elm(source_dir, destination_dir)) - -# make everything exception friendly and neat \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index bdc2aaa..5682923 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,7 +1,15 @@ use std::collections::HashMap; use std::path::Path; + + + +#[derive(RustEmbed)] +#[folder = "."] +struct Asset; + #[derive(Deserialize, Debug, Default)] pub(crate) struct Config { + #[serde(default)] pub context: String, #[serde(default)] @@ -18,7 +26,6 @@ pub(crate) struct Config { pub site_title_postfix: String, #[serde(default)] pub static_dir: String, - #[serde(default)] pub latest_elm: String, #[serde(default)] @@ -27,6 +34,7 @@ pub(crate) struct Config { pub js_code: HashMap, #[serde(default)] pub loader_file: String, + } impl Config { pub fn new() -> Self { @@ -62,11 +70,25 @@ lazy_static! { let proj_dir = std::env::current_dir().expect("could not find current dir"); let conf_file = proj_dir.join("realm.json"); let mut config: Config = Config::new(); - match std::fs::File::open(conf_file) { - Ok(conf_file) => config = serde_json::from_reader(conf_file).expect("invalid json"), - Err(e) => println!("could not find realm.json. Trying Default Json."), +// match std::fs::File::open(conf_file) { +// Ok(conf_file) => config = serde_json::from_reader(conf_file).expect("invalid json"), +// Err(e) => println!("could not find realm.json. Trying Default Json."), +// }; + + println!("using rust-embed"); + let realmjson_content = Asset::get("realm.json"); + match realmjson_content{ + Some(c) => { + match std::str::from_utf8(c.as_ref()).ok(){ + Some(c_str) => {println!("realmjson_content {:?}", c_str);config = serde_json::from_str(c_str).expect("invalid json")}, + None => println!("could not decode u8 to str after reading realm.json") + } + } + None =>println!("could not find realm.json. Trying Default Json.") }; + + if config.loader_file == "" { config.loader_file = proj_dir .join("node_modules") diff --git a/src/lib.rs b/src/lib.rs index 52cddc1..d89c4fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,10 +5,13 @@ extern crate serde_json; #[macro_use] extern crate lazy_static; +#[macro_use] +extern crate rust_embed; extern crate chrono; extern crate itertools; extern crate url; + mod config; mod html; mod mode; diff --git a/src/request_config.rs b/src/request_config.rs index 8199625..162240e 100644 --- a/src/request_config.rs +++ b/src/request_config.rs @@ -2,7 +2,6 @@ use serde::de::DeserializeOwned; use std::{collections::HashMap, fmt::Debug, str::FromStr}; #[derive(Debug)] pub struct RequestConfig { - pub req: crate::Request, pub query: std::collections::HashMap, pub data: serde_json::Value, pub rest: String, @@ -26,7 +25,7 @@ fn first_rest(s: &str) -> (Option, String) { } impl RequestConfig { - pub fn new(req: crate::Request) -> std::result::Result { + pub fn new(req: &crate::Request) -> std::result::Result { let url = req.uri(); let path = crate::utils::get_slash_complete_path(url.path()); let site_url = "http://127.0.0.1:3000".to_string(); @@ -42,7 +41,6 @@ impl RequestConfig { query, data, path, - req, }; Ok(req_config) } diff --git a/src/serve.rs b/src/serve.rs index 2d2dfe4..b2527d8 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -39,7 +39,6 @@ macro_rules! realm { pub fn serve() { let addr = ([127, 0, 0, 1], 3000).into(); - let server = hyper::Server::bind(&addr) .serve(|| { hyper::service::service_fn(|req: hyper::Request| -> BoxFut {