fn try_from()

in azure-kusto-ingest/src/resource_manager/resource_uri.rs [43:91]


    fn try_from(uri: &str) -> Result<Self, Self::Error> {
        let parsed_uri = Url::parse(uri)?;

        match parsed_uri.scheme() {
            "https" => {}
            other_scheme => return Err(ResourceUriError::InvalidScheme(other_scheme.to_string())),
        };

        let host_string = match parsed_uri.host() {
            Some(url::Host::Domain(host_string)) => host_string,
            _ => return Err(ResourceUriError::InvalidHost),
        };

        let service_uri = String::from("https://") + host_string;

        // WIBNI: better parsing that this conforms to a storage resource URI,
        // perhaps then ResourceUri could take a type like ResourceUri<Queue> or ResourceUri<Container>
        let (account_name, _service_endpoint) = host_string
            .split_once('.')
            .ok_or(ResourceUriError::MissingAccountName)?;

        let object_name = match parsed_uri.path_segments() {
            Some(mut path_segments) => {
                let object_name = match path_segments.next() {
                    Some(object_name) if !object_name.is_empty() => object_name,
                    _ => return Err(ResourceUriError::MissingObjectName),
                };
                // Ensure there is only one path segment (i.e. the object name)
                if path_segments.next().is_some() {
                    return Err(ResourceUriError::MissingObjectName);
                };
                object_name
            }
            None => return Err(ResourceUriError::MissingObjectName),
        };

        let sas_token = parsed_uri
            .query()
            .ok_or(ResourceUriError::MissingSasToken)?;

        let sas_token = StorageCredentials::sas_token(sas_token)?;

        Ok(Self {
            service_uri,
            object_name: object_name.to_string(),
            account_name: account_name.to_string(),
            sas_token,
        })
    }