tiny_web/
lib.rs

1/// Show help message
2pub mod help;
3/// Web server
4pub mod sys;
5
6use std::mem::transmute;
7
8use sys::{action::ActMap, app::App, init::Mode, log::Log};
9
10/// Entry point
11pub fn run(name: &str, version: &str, desc: &str, func: impl Fn() -> ActMap) {
12    let app = match execute(name, version, desc, &func, true) {
13        Some(app) => app,
14        None => return,
15    };
16    if let Mode::Go = app.init.mode {
17        if app.init.conf.is_default {
18            execute(name, version, desc, &func, false);
19        }
20    }
21}
22
23/// Run application
24fn execute(name: &str, version: &str, desc: &str, func: &impl Fn() -> ActMap, allow_no_config: bool) -> Option<App> {
25    let app = match App::new(name, version, desc, allow_no_config) {
26        Some(a) => a,
27        None => return None,
28    };
29    Log::info(200, None);
30    app.run(func);
31    Log::info(201, None);
32    Some(app)
33}
34
35/// fnv1a_64 hash function
36///
37/// # Parameters
38///
39/// * `text: &str` - Origin string.
40///
41/// # Return
42///
43/// i64 hash
44#[inline]
45pub fn fnv1a_64(bytes: &[u8]) -> i64 {
46    let mut hash = 0xcbf29ce484222325;
47    for c in bytes {
48        hash ^= u64::from(*c);
49        hash = hash.wrapping_mul(0x100000001b3);
50    }
51    unsafe { transmute(hash) }
52}
53
54/// Trait `StrOrI64` defines a method for converting different types to `i64`.
55pub trait StrOrI64 {
56    /// The `to_i64` method takes input data and returns an `i64`.
57    fn to_i64(&self) -> i64;
58    /// The `to_str` method takes input data and returns an `&str`.
59    fn to_str(&self) -> &str;
60}
61
62impl StrOrI64 for i64 {
63    /// Implementation of the `to_i64` method for the `i64` type.  
64    /// Simply returns the input `i64`.
65    fn to_i64(&self) -> i64 {
66        *self
67    }
68    /// The `to_str` method takes input data and returns an `&str`.
69    fn to_str(&self) -> &str {
70        ""
71    }
72}
73
74impl StrOrI64 for &str {
75    /// Implementation of the `to_i64` method for the `&str` type.  
76    /// Computes the hash of the `&str` and returns it as an `i64`.
77    fn to_i64(&self) -> i64 {
78        fnv1a_64(self.as_bytes())
79    }
80    /// The `to_str` method takes input data and returns an `&str`.
81    /// Simply returns the input `i64`.
82    fn to_str(&self) -> &str {
83        self
84    }
85}