pebbles 0.0.102

A unfinished webautomation framework for working with Firefox and the geckodriver.
Documentation
use crate::{
    service::Service,
    result::{PebblesError, PebblesErrorDetails}, error,
};

use serde_json::{json, Value};
use std::rc::Rc;

pub struct Navigator {
    service: Rc<Service>,
}

impl Navigator {
    pub fn new(service: Rc<Service>) -> Self {
        Navigator {
            service,
        }
    }
    pub fn get(&self, url: &str) -> Result<(), PebblesError> {
        self.service.post("/url", &json!({"url":url}))?;
        Ok(())
    }

    pub fn url(&self) -> Result<String, PebblesError> {
        let response = self.service.get("/url")?;
        let response_json = response.json::<Value>()?;
        match response_json["value"].as_str() {
            Some(url) => Ok(url.to_string()),
            None => Err(PebblesError::PhrasingError(PebblesErrorDetails::new(
                "Json value is not a string",
            ))),
        }
    }

    pub fn back(&self) -> Result<(), PebblesError> {
        self.service.post("/back", &json!({}))?;
        Ok(())
    }

    pub fn forward(&self) -> Result<(), PebblesError> {
        self.service.post("/forward", &json!({}))?;
        Ok(())
    }

    pub fn refresh(&self) -> Result<(), PebblesError> {
        self.service.post("/refresh", &json!({}))?;
        Ok(())
    }

    pub fn title(&self) -> Result<String, PebblesError> {
        let response = self.service.get("/title")?;
        let response_json = response.json::<Value>()?;
        match response_json["value"].as_str() {
            Some(url) => Ok(url.to_string()),
            None => Err(PebblesError::PhrasingError(PebblesErrorDetails::new(
                "Json value is not a string",
            ))),
        }
    }

    pub fn close(&self) -> Result<(), PebblesError> {
        self.service.delete("/window")?;
        Ok(())
    }

    pub fn maximize(&self) -> Result<(), PebblesError> {
        self.service.post("/window/maximize", &json!({}))?;
        Ok(())
    }

    pub fn minimize(&self) -> Result<(), PebblesError> {
        self.service.post("/window/minimize", &json!({}))?;
        Ok(())
    }

    pub fn handles(&self) -> Result<Vec<String>, PebblesError> {
        let response = self.service.get("/window/handles")?;
        let response_json = response.json::<Value>()?;
        let response_vec = match response_json["value"].as_array() {
            Some(value) => value,
            None => return Err(PebblesError::PhrasingError(PebblesErrorDetails::new(
                "Json value is not a string",
            ))),
        };
        let mut string_vec: Vec<String> = vec![];
        for value in response_vec {
            match value.as_str() {
                Some(string_val) => string_vec.push(string_val.to_string()),
                None => continue,
            };
        };
        Ok(string_vec)
    }

    pub fn switch_handle(&self, handle:&str) -> Result<(), PebblesError> {
        self.service.post("/window", &json!({"handle": handle}))?;
        Ok(())
    }

    pub fn switch_index(&self, index:i64) -> Result<(), PebblesError> {
        let handles = self.handles()?;
        let len = handles.len() as i64;
        let index_usize = if index < 0 {
            (len + index) as usize
        } else {
            index as usize
        };
        match handles.get(index_usize) {
            Some(handle) => {
                self.service.post("/window", &json!({"handle": handle}))?;
                Ok(())
            }
            None => Err(error!{
                PebblesError::NoWindowError, 
                format!("There is no window with the index: {}/
                There are currently {} window handles open.", 
                index, len)})
        }
    }
}