Crate lewp

Source
Expand description

Version Downloads MIT or Apache-2.0 License

Say goodbye to the web template hell. Generate your HTML5 website technically optimized and always valid. In your Rust source.

This crate is currently evolving. API changes can happen anytime until v1.0.0

§Provided solutions

When using lewp, you get the following benefits during web development:

  • No more template hell in your code base
  • No more whitespace bugs in your website
  • Technically optimized, always valid, minified, HTML5 code
  • Module based development, truly isolated
  • Build the DOM completely in Rust

§Hello world! example

use {
    lewp::{
        config::{ModuleConfig, PageConfig},
        html::{
            api::{h1, text},
            Nodes,
        },
        Module, Modules, RuntimeInformation,
        Page,
        Charset,
        LanguageTag,
        LewpError,
    },
    std::rc::Rc,
};

// This is one of your modules the webpage is build with.
struct HelloWorld {
    config: ModuleConfig,
    head_tags: Nodes,
    data: String,
}

impl HelloWorld {
    pub fn new() -> Self {
        Self {
            config: ModuleConfig::new(),
            head_tags: vec![],
            data: String::from("hello-world"),
        }
    }
}

// The [Module] trait is required for [lewp] to know it is a module. :-)
impl Module for HelloWorld {
    fn head_tags(&self) -> &Nodes {
        &self.head_tags
    }

    fn id(&self) -> &str {
        "hello-world"
    }

    fn config(&self) -> &ModuleConfig {
        &self.config
    }

    fn run(
        &mut self,
        _runtime_info: Rc<RuntimeInformation>,
    ) -> Result<(), LewpError> {
        Ok(())
    }

    fn view(&self) -> Nodes {
        vec![h1(vec![text(&self.data)])]
    }
}

// This struct defines the actual page. The containing members are
// required because they define the base on which [lewp] is working on.
struct HelloWorldPage {
    modules: Modules,
    config: PageConfig,
}

impl HelloWorldPage {
    /// Creates a new page.
    pub fn new(config: PageConfig) -> Self {
        Self {
            modules: vec![],
            config,
        }
    }
}

impl Page for HelloWorldPage {
    fn modules(&self) -> &Modules {
        &self.modules
    }
    fn modules_mut(&mut self) -> &mut Modules {
        &mut self.modules
    }

    fn title(&self) -> &str {
        "Hello World from lewp!"
    }

    fn description(&self) -> &str {
        "My first page using lewp!"
    }

    fn language(&self) -> LanguageTag {
        LanguageTag::parse("de-DE").unwrap()
    }

    fn charset(&self) -> Charset {
        Charset::Utf8
    }

    fn config(&self) -> &PageConfig {
        &self.config
    }

    fn run(&mut self) {
        self.add_module(HelloWorld::new().into_module_ptr());
    }
}

fn main() {
    let mut page = HelloWorldPage::new(PageConfig::new());
    println!("{}", page.build());
}

Modules§

config
Configuration data structures used by lewp.
css
CSS modification functions especially required by lewp.
fh
Defines the file hierarchy of lewp.
html
Re-export of the lewp_html crate.

Structs§

LewpError
Contains the error definitions that occur in lewp.
RuntimeInformation
Contains runtime information of a module.

Enums§

Charset
A Mime charset.
LanguageTag
Language tag with borrowed data.
LewpErrorKind
List of error kinds that occur within lewp.

Traits§

Module
Defines a web page module.
Page
Main trait of a page.
SubModule
Enables management of submodules.

Type Aliases§

ModulePtr
Wrapper type for a Module instance.
Modules
A collection of modules.