1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! # Unreact
//!
//! [Unreact](https://github.com/darccyy/unreact) is a simple static site generation framework
//!
//! ## Quick Start
//!
//! See also: [examples](https://github.com/darccyy/unreact/tree/main/examples)
//! and [unreact template](https://github.com/darccyy/unreact-template)
//!
//! Create an site with a single index page
//!
//! ```
//! use unreact::prelude::*;
//!
//! fn main() -> Result<(), Error> {
//!    // Create the app
//!    // Using default config, not in dev mode, and an example url
//!    let mut app = Unreact::new(Config::default(), false, "https://example.com")?;
//!    // Create an index route
//!    // This uses the template 'page.hbs' in 'templates/'
//!    // A json object with a value for 'foo' is passed into the template
//!    app.index("page", object! { foo: "World!" });
//!    // Compile it!
//!    app.compile()
//! }
//! ```
//!
//! Your workspace should look something like this:
//!
//! ```txt
//! unreact-app/
//!   ├─ Cargo.toml
//!   ├─ src/
//!   │  └─ main.rs
//!   │
//!   ├─ templates/
//!   │  └─ page.hbs
//!   │
//!   ├─ styles/
//!   └─ public/
//! ```
//!
//! This is the contents of `templates/page.hbs`:
//!
//! ```hbs
//! <h1> Hello {{foo}} </h1>
//! ```
//!
//! This will render `build/index.html`:
//!
//! ```html
//! <h1> Hello World! </h1>
//! ```
//!
//! A larger project could look something like this:
//!
//! ```txt
//! unreact-app/
//!   ├─ Cargo.toml
//!   ├─ src/
//!   │  └─ main.rs
//!   │
//!   ├─ templates/
//!   │  ├─ boilerplate.hbs
//!   │  ├─ hello.hbs
//!   │  ├─ other/
//!   │  │  ├─ another/
//!   │  │  │  └─ something.hbs
//!   │  │  └─ article.hbs
//!   │  └─ page.hbs
//!   │
//!   ├─ styles/
//!   │  ├─ global.scss
//!   │  └─ scoped/
//!   │     └─ stylish.scss
//!   │
//!   └─ public/
//!      └─ favicon.ico
//! ```

/// Private macros module
#[macro_use]
mod macros;
/// `Unreact` struct implementations
mod app;
/// `Config` struct
mod config;
/// Convert and render filetypes, .hbs and .scss
mod convert;
/// Unreact `Error` type
mod error;
/// Handle file system logic
mod files;

/// Dev server and websockets
#[cfg(feature = "dev")]
mod server;

use handlebars::Handlebars;
use std::collections::HashMap;

pub use crate::{
    config::Config,
    error::{Error, IoError},
};
pub use serde_json::Value;

/// Represents json-like object
/// A map of string keys to json values
///
/// A type alias for `serde_json::Map<String, serde_json::Value>`
///
/// See also: [`object`]
pub type Object = serde_json::Map<String, Value>;

/// Map a filepath to file contents
type FileMap = HashMap<String, String>;
/// Map a path to a `Page` enum
type RouteMap = HashMap<String, Page>;

/// Build directory for *dev mode*
///
/// Overrides any build directory given
const DEV_BUILD_DIR: &str = ".devbuild";

/// A page that will be rendered
///
/// ## Variants
///
/// - `Raw`: Raw string
/// - `Template`: Render a template, with data
#[derive(Debug)]
enum Page {
    /// Raw string
    Raw(String),
    /// Render a template, with data
    Template { template: String, data: Object },
}

/// Unreact app
///
/// Create a new app with `Unreact::new()`
///
/// # Examples
///
/// ```rust,no_run
/// use unreact::prelude::*;
///
/// const URL: &str = "https://example.com";
///
/// fn main() -> Result<(), Error> {
///    let mut app = Unreact::new(Config::default(), false, URL)?;
///    
///    app
///        .index("page", object! {})?
///        .route("hi", "hello", object! {
///            world: "World!"
///        })?;
///
///    app.run()
/// }
#[derive(Debug)]
pub struct Unreact<'a> {
    /// Configuration for app
    config: Config,
    /// Map paths to pages
    routes: RouteMap,
    /// Global variables for templates
    globals: Object,
    /// Whether *dev mode* is active
    is_dev: bool,
    /// [`Handlebars`](handlebars) registry
    registry: Handlebars<'a>,
}

/// Check if `--dev` or `-d` argument was passed on `cargo run`
///
/// # Examples
///
/// This will run in production mode
///
/// ```ps1
/// cargo run
/// ```
///
/// This will run in development mode
///
/// ```ps1
/// cargo run -- --dev
/// cargo run -- -d
/// ```
pub fn is_dev() -> bool {
    let args = std::env::args().collect::<Vec<_>>();
    args.contains(&"--dev".to_string()) || args.contains(&"-d".to_string())
}

/// Alias for u16
type Port = u16;

/// Local port to host dev server (on localhost)
const DEFAULT_PORT: Port = 3000;
/// Local port to host websocket hub (on localhost)
const DEFAULT_PORT_WS: Port = 3001;

/// Prelude for `Unreact`
///
/// ## Contains
///
/// - [`Unreact`] struct
/// - [`Config`] struct
/// - [`object`] macro
/// - [`is_dev`](fn.is_dev.html) function
/// - [`Error`] enum
pub mod prelude {
    pub use crate::{is_dev, object, Config, Error, Unreact};
}

/// Get package name from `Cargo.toml` file in workspace
///
/// Returns `None` if any errors are found, or no package name is found
fn get_package_name() -> Option<String> {
    // Read Cargo.toml or return
    let file = std::fs::read_to_string("./Cargo.toml").ok()?;

    // Current category is 'package'
    let mut is_package = false;
    // Loop lines
    for line in file.lines() {
        let line = line.trim();

        // Change category
        if line.starts_with('[') && line.ends_with(']') {
            is_package = line == "[package]";
        }
        // Continue if not package category
        if !is_package {
            continue;
        }

        // Check key is 'name'
        let mut split = line.split('=');
        if split.next().map(|x| x.trim()) != Some("name") {
            continue;
        }
        let rest: Vec<_> = split.collect();

        // Get rest of line
        let name = rest.join("=");
        let name = name.trim();

        // Remove first and last characters, break if not quotes
        let mut chars = name.chars();
        if chars.next() != Some('"') {
            break;
        }
        if chars.next_back() != Some('"') {
            break;
        }

        // Return value
        return Some(chars.as_str().to_string());
    }

    // No name found
    None
}