Unreact

Struct Unreact 

Source
pub struct Unreact<'a> { /* private fields */ }
Expand description

Unreact app

Create a new app with Unreact::new()

§Examples

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()
}

Implementations§

Source§

impl<'a> Unreact<'a>

Source

pub fn route( &mut self, path: &str, template: &str, data: Object, ) -> Result<&mut Self, Error>

Create a route

NOTE: Route will only validate if template exists in production. In dev mode, this function will always pass, and error will occur during run function

§Parameters
  • path: The folder (relative to build directory) that file should be written in ({build}/{path}/index.html)
  • template: The name of the template to use
  • data: Data to pass into the template, as an Object
§Examples
app
    // Create a route to '/some_path' with the template 'page.hbs' and a message
    .route("some_path", "page", object! {message: "this is at '/some_path'"})?
    // Create a route without a template (raw string)
    .route_raw("hello", "this is my hello page".to_string())
    // Create a route without data
    .route("article", "other/article", object! {})?
    // Index page with a message
    .index("page", object! {message: "World"})?
    // 404 page with no data
    .not_found("404", object! {})?;
§Routing Methods
Source

pub fn route_raw(&mut self, path: &str, content: impl Into<String>) -> &mut Self

Create a route, with raw page content instead of a template

§Parameters
  • path: The folder (relative to build directory) that file should be written in ({build}/{path}/index.html)
  • content: The raw file contents to write to the file
§Examples
app
    // Create a route to '/some_path' with the template 'page.hbs' and a message
    .route("some_path", "page", object! {message: "this is at '/some_path'"})?
    // Create a route without a template (raw string)
    .route_raw("hello", "this is my hello page".to_string())
    // Create a route without data
    .route("article", "other/article", object! {})?
    // Index page with a message
    .index("page", object! {message: "World"})?
    // 404 page with no data
    .not_found("404", object! {})?;
§Routing Methods
Source

pub fn route_raw_html( &mut self, path: &str, content: impl Into<String>, ) -> &mut Self

Create a route, with raw page content instead of a template

Adds HTML boilerplate around content (Unlike route_raw)

§Parameters
  • path: The folder (relative to build directory) that file should be written in ({build}/{path}/index.html)
  • content: The raw file contents to write to the file
§Examples
app
    // Create a route to '/some_path' with the template 'page.hbs' and a message
    .route("some_path", "page", object! {message: "this is at '/some_path'"})?
    // Create a route without a template (raw string)
    .route_raw("hello", "this is my hello page".to_string())
    // Create a route without data
    .route("article", "other/article", object! {})?
    // Index page with a message
    .index("page", object! {message: "World"})?
    // 404 page with no data
    .not_found("404", object! {})?;
§Routing Methods
Source

pub fn index( &mut self, template: &str, data: Object, ) -> Result<&mut Self, Error>

Create the index route

Alias of app.route("", ...)

File is written to {build}/index.html

NOTE: Route will only validate if template exists in production. In dev mode, this function will always pass, and error will occur during run function

§Parameters
  • template: The name of the template to use
  • data: Data to pass into the template, as an Object
§Examples
app
    // Create a route to '/some_path' with the template 'page.hbs' and a message
    .route("some_path", "page", object! {message: "this is at '/some_path'"})?
    // Create a route without a template (raw string)
    .route_raw("hello", "this is my hello page".to_string())
    // Create a route without data
    .route("article", "other/article", object! {})?
    // Index page with a message
    .index("page", object! {message: "World"})?
    // 404 page with no data
    .not_found("404", object! {})?;
§Routing Methods
Source

pub fn not_found( &mut self, template: &str, data: Object, ) -> Result<&mut Self, Error>

Create the 404 route

Alias of app.route("404", ...). Used as the 404 page, for a path not found

File is written to {build}/404/index.html

NOTE: Route will only validate if template exists in production. In dev mode, this function will always pass, and error will occur during run function

§Parameters
  • template: The name of the template to use
  • data: Data to pass into the template, as an Object
§Examples
app
    // Create a route to '/some_path' with the template 'page.hbs' and a message
    .route("some_path", "page", object! {message: "this is at '/some_path'"})?
    // Create a route without a template (raw string)
    .route_raw("hello", "this is my hello page".to_string())
    // Create a route without data
    .route("article", "other/article", object! {})?
    // Index page with a message
    .index("page", object! {message: "World"})?
    // 404 page with no data
    .not_found("404", object! {})?;
§Routing Methods
Source§

impl<'a> Unreact<'a>

Source

pub fn new(config: Config, is_dev: bool, url: &str) -> Result<Self, Error>

Create a new empty Unreact app

§Parameters
  • config: Configuration for the app (See Config)
  • is_dev: Whether the app should build in dev mode (See is_dev)
  • url: The url that should be given to rendered templates. Overridden in dev mode. Trailing forward-slash is added if not present
§Examples
§Quick Start
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.run()
}
§Long Example
use unreact::prelude::*;

fn main() -> Result<(), Error> {
    // Custom config
    let config = Config {
        // Strict mode enabled
        strict: true,
        ..Config::default()
    };

    // Create app, with `is_dev` function
    let mut app = Unreact::new(config, is_dev(), "https://bruh.news/").expect("Could not create app");

    // Set a global variable named 'smiley'
    app.globalize(object! {
        smiley: "(^_^)"
    });

    // Some routes
    app.index("page", object! {message: "World"})?
        .not_found("404", object! {})?
        .route_raw("hello", "this is my hello page".to_string())
        .route("article", "other/article", object! {})?;
     
    // Run app
    app.run().expect("Could not compile app");

    println!("Compiled successfully");
    Ok(())
}
Source

pub fn globalize(&mut self, data: Object) -> &mut Self

Set global variables for templates

§Example
Unreact::new(Config::default(), false, "https://example.com")?
    // Index page
    .index("page", object! {})?
    // Globalize does not need to be ran before routes
    .globalize(object! {smiley: "(^_^)"})
    // Compiles with a smiley face replacing `{{GLOBAL.smiley}}`
    .run()
Source

pub fn handlebars(&mut self) -> &mut Handlebars<'a>

Get Handlebars registry as mutable reference

Source

pub fn url(&self) -> &String

Get URL of app (overridden in dev mode)

Source

pub fn run(&self) -> Result<(), Error>

Compile app to build directory

Compile app to build directory

NOTE: The "dev" feature is not enabled, so app not open dev server, even in dev mode

Add features = "dev" or features = "watch" to the unreact dependency in Cargo.toml to use the ‘dev server’

§Examples
§Quick Start
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.run()
}

Trait Implementations§

Source§

impl<'a> Debug for Unreact<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Unreact<'a>

§

impl<'a> !RefUnwindSafe for Unreact<'a>

§

impl<'a> Send for Unreact<'a>

§

impl<'a> Sync for Unreact<'a>

§

impl<'a> Unpin for Unreact<'a>

§

impl<'a> !UnwindSafe for Unreact<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V