lemon_bugsnag_rs 0.1.2

A library for interacting with the BugSnag error-reporting and sessions APIs.
Documentation
use url::Url;

use super::{
    builder::builder_options::BuilderOptions,
    error::{Error, ErrorType},
};

/// Provides a default implementation for combining BuilderOptions with an
/// existing URL. With this implementation, if both the url field and
/// individual URL pieces, such as the host, port, etc, have been set, the
/// individual pieces are applied on top of the existing URL.
///
/// It is recommended that you use this cobble() function when implementing
/// additional backends unless you have specific requirements that make doing
/// so impossible. That way, all backends generate the same URL given the same
/// options.
///
/// See the `build()` function on [ClientBuilderReqwestErrorSync](crate::error::reqwest::builder::blocking::ClientBuilderReqwestErrorSync)
/// for an example of usage.
pub(crate) struct Cobbler {}
impl Cobbler {
    pub(crate) fn cobble(options: BuilderOptions) -> Result<Url, Error> {
        let url = if options.url.is_some() {
            let mut url = options.url.clone().unwrap();
            if options.scheme.is_some() {
                if url
                    .set_scheme(options.scheme.clone().unwrap().as_str())
                    .is_err()
                {
                    return Err(Error::new(
                        "Error setting URL scheme".to_string(),
                        ErrorType::Unknown,
                        None,
                    ));
                }
            }

            if options.host.is_some() {
                url.set_host(options.host.clone().as_deref())?;
            }

            if options.port.is_some() {
                if url.set_port(options.port.clone()).is_err() {
                    return Err(Error::new(
                        "Error setting URL port".to_string(),
                        ErrorType::Unknown,
                        None,
                    ));
                }
            }

            if options.endpoint.is_some() {
                url.set_path(options.endpoint.clone().as_deref().unwrap());
            }

            url
        } else {
            let mut cobbled: String = String::new();
            match &options.scheme {
                Some(scheme) => {
                    cobbled.push_str(&scheme);

                    if !scheme.ends_with("://") {
                        cobbled.push_str(&"://");
                    }
                }
                None => (),
            }

            match &options.host {
                Some(host) => cobbled.push_str(&host),
                None => (),
            }

            match options.port {
                Some(port) => cobbled.push_str(&format!(":{}", port)),
                None => (),
            }

            match &options.endpoint {
                Some(endpoint) => {
                    if !endpoint.starts_with("/") {
                        cobbled.push_str(&"/".to_string());
                    }

                    cobbled.push_str(&endpoint);
                }
                None => (),
            }

            Url::parse(&cobbled)?
        };

        Ok(url)
    }
}