torigen_mounter 1.0.32

Core functionality for Torigen, a tool for mounting and managing Tor hidden services.
Documentation
use super::structure::WasmExtension;
use crate::{core::extension::*, generated::*};
use serde_json::{from_str, to_string};

pub struct WasmExtensionWrapper<T: SourceProvider> {
    pub inner: T,
}

impl<T: SourceProvider> WasmExtensionWrapper<T> {
    pub fn new(inner: T) -> Self {
        Self { inner }
    }
}

impl<T: SourceProvider> WasmExtension for WasmExtensionWrapper<T> {
    fn get_source_info(&self) -> String {
        to_string(&self.inner.get_source_info()).unwrap_or_default()
    }

    fn get_metadata(&self) -> String {
        to_string(&self.inner.get_metadata()).unwrap_or_default()
    }

    fn get_homepage_request(&self) -> String {
        to_string(&self.inner.get_homepage_request()).unwrap_or_default()
    }

    fn process_homepage_res(&self, responses_json: &str) -> String {
        let responses: Vec<HttpResponse> = match from_str(responses_json) {
            Ok(r) => r,
            Err(e) => {
                return to_string(&Result::<Homepage, String>::Err(e.to_string()))
                    .unwrap_or_default();
            }
        };

        let result = self.inner.process_homepage_res(responses);
        to_string(&result).unwrap_or_default()
    }

    fn get_viewmore_request(&self, section_id: &str, page: u32) -> String {
        to_string(&self.inner.get_viewmore_request(section_id, page)).unwrap_or_default()
    }

    fn process_viewmore_res(&self, response_json: &str) -> String {
        let response: HttpResponse = match from_str(response_json) {
            Ok(r) => r,
            Err(e) => {
                return to_string(&Result::<PagedResults<SectionEntry>, String>::Err(
                    e.to_string(),
                ))
                .unwrap_or_default();
            }
        };

        let result = self.inner.process_viewmore_res(response);
        to_string(&result).unwrap_or_default()
    }

    fn get_search_request(&self, query_json: &str) -> String {
        let query: SearchRequest = match from_str(query_json) {
            Ok(q) => q,
            Err(e) => return to_string(&format!("Error: {}", e)).unwrap_or_default(),
        };

        to_string(&self.inner.get_search_request(&query)).unwrap_or_default()
    }

    fn process_search_res(&self, response_json: &str) -> String {
        let response: HttpResponse = match from_str(response_json) {
            Ok(r) => r,
            Err(e) => {
                return to_string(&Result::<PagedResults<MangaEntry>, String>::Err(
                    e.to_string(),
                ))
                .unwrap_or_default();
            }
        };

        let result = self.inner.process_search_res(response);
        to_string(&result).unwrap_or_default()
    }

    fn get_search_tags_request(&self) -> String {
        to_string(&self.inner.get_search_tags_request()).unwrap_or_default()
    }

    fn process_search_tags_res(&self, response_json: &str) -> String {
        let response: HttpResponse = match from_str(response_json) {
            Ok(r) => r,
            Err(e) => {
                return to_string(&Result::<Vec<Tag>, String>::Err(e.to_string()))
                    .unwrap_or_default();
            }
        };

        let result = self.inner.process_search_tags_res(response);
        to_string(&result).unwrap_or_default()
    }

    fn get_manga_request(&self, manga_id: &str) -> String {
        to_string(&self.inner.get_manga_request(manga_id)).unwrap_or_default()
    }

    fn process_manga_res(&self, response_json: &str) -> String {
        let response: HttpResponse = match from_str(response_json) {
            Ok(r) => r,
            Err(e) => {
                return to_string(&Result::<Manga, String>::Err(e.to_string())).unwrap_or_default();
            }
        };

        let result = self.inner.process_manga_res(response);
        to_string(&result).unwrap_or_default()
    }

    fn get_chapters_request(&self, manga_id: &str) -> String {
        to_string(&self.inner.get_chapters_request(manga_id)).unwrap_or_default()
    }

    fn process_chapters_res(&self, response_json: &str) -> String {
        let response: HttpResponse = match from_str(response_json) {
            Ok(r) => r,
            Err(e) => {
                return to_string(&Result::<Vec<ChapterEntry>, String>::Err(e.to_string()))
                    .unwrap_or_default();
            }
        };

        let result = self.inner.process_chapters_res(response);
        to_string(&result).unwrap_or_default()
    }

    fn get_chapter_details_request(&self, manga_id: &str, chapter_id: &str) -> String {
        to_string(&self.inner.get_chapter_details_request(manga_id, chapter_id)).unwrap_or_default()
    }

    fn process_chapter_details_res(&self, responses_json: &str) -> String {
        let responses: Vec<HttpResponse> = match from_str(responses_json) {
            Ok(r) => r,
            Err(e) => {
                return to_string(&Result::<Chapter, String>::Err(e.to_string()))
                    .unwrap_or_default();
            }
        };

        let result = self.inner.process_chapter_details_res(responses);
        to_string(&result).unwrap_or_default()
    }
}

pub trait WasmExtensionProvider {
    fn new() -> Self;
}