#
mcaptcha/pages/panel/sitekey/
mod.rs

    1// Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
    2// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
    3//
    4// SPDX-License-Identifier: AGPL-3.0-or-later
    5
    6mod add;
    7mod delete;
    8mod edit;
    9pub mod list;
    10mod view;
    11
    12pub mod routes {
    13    pub struct Sitekey {
    14        pub list: &'static str,
    15        pub add_easy: &'static str,
    16        pub add_advance: &'static str,
    17        pub view: &'static str,
    18        pub edit_easy: &'static str,
    19        pub edit_advance: &'static str,
    20        pub delete: &'static str,
    21    }
    22
    23    impl Sitekey {
    24        pub const fn new() -> Self {
    25            Sitekey {
    26                list: "/sitekeys",
    27                add_advance: "/sitekeys/advance/add",
    28                add_easy: "/sitekeys/easy/add",
    29                view: "/sitekey/{key}",
    30                edit_advance: "/sitekey/{key}/advance/edit",
    31                edit_easy: "/sitekey/{key}/easy/edit",
    32                delete: "/sitekey/{key}/delete",
    33            }
    34        }
    35        pub const fn get_sitemap() -> [&'static str; 2] {
    36            const S: Sitekey = Sitekey::new();
    37            [S.list, S.add_advance]
    38        }
    39
    40        pub fn get_edit_easy(&self, key: &str) -> String {
    41            self.edit_easy.replace("{key}", key)
    42        }
    43
    44        pub fn get_edit_advance(&self, key: &str) -> String {
    45            self.edit_advance.replace("{key}", key)
    46        }
    47
    48        pub fn get_view(&self, key: &str) -> String {
    49            self.view.replace("{key}", key)
    50        }
    51
    52        pub fn get_delete(&self, key: &str) -> String {
    53            self.delete.replace("{key}", key)
    54        }
    55    }
    56}
    57
    58pub fn services(cfg: &mut actix_web::web::ServiceConfig) {
    59    cfg.service(add::advance);
    60    cfg.service(add::easy);
    61    cfg.service(list::list_sitekeys);
    62    cfg.service(view::view_sitekey);
    63    cfg.service(edit::advance);
    64    cfg.service(edit::easy);
    65    cfg.service(delete::delete_sitekey);
    66}
    67
    68#[cfg(test)]
    69mod tests {
    70    use super::routes::Sitekey;
    71
    72    #[test]
    73    fn get_sitekey_routes_work() {
    74        const ROUTES: Sitekey = Sitekey::new();
    75        const KEY: &str = "foo";
    76        let tests = [
    77            (ROUTES.get_edit_easy(KEY), "/sitekey/foo/easy/edit"),
    78            (ROUTES.get_edit_advance(KEY), "/sitekey/foo/advance/edit"),
    79            (ROUTES.get_view(KEY), "/sitekey/foo"),
    80            (ROUTES.get_delete(KEY), "/sitekey/foo/delete"),
    81        ];
    82
    83        for (r, l) in tests.iter() {
    84            assert_eq!(r, l);
    85        }
    86    }
    87}