<commit_before>use clap;
use info::{DiscoveryServiceInfo, EnvironmentInfo};
use serde_json::Value;

pub fn read_only_environment(info: &DiscoveryServiceInfo) -> EnvironmentInfo {
    let read_only: Vec<EnvironmentInfo> =
        info.environments
            .clone()
            .into_iter()
            .filter({
            |env| env.environment["read_only"].as_bool().unwrap_or(false)
        })
            .collect();

    assert!(
        read_only.len() <= 1,
        format!("Multiple read only environments found {:?}", read_only)
    );

    read_only.first()
             .expect("No read only environment found")
             .clone()
}

pub fn writable_environment(info: &DiscoveryServiceInfo) -> EnvironmentInfo {
    let writables: Vec<EnvironmentInfo> =
        info.environments
            .clone()
            .into_iter()
            .filter({
            |env| !env.environment["read_only"].as_bool().unwrap_or(true)
        })
            .collect();

    assert!(
        writables.len() <= 1,
        format!("Multiple writable environments found {:?}", writables)
    );

    writables.first()
             .expect("No writable environment found")
             .clone()
}

pub fn oldest_collection(env: &EnvironmentInfo) -> Value {
    env.collections
       .first()
       .expect("No collections found")
       .clone()
}

pub fn newest_collection(env: &EnvironmentInfo) -> Value {
    env.collections
       .last()
       .expect("No collections found")
       .clone()
}

pub fn collection_with_name(env: &EnvironmentInfo, name: &str) -> Value {
    let f: Vec<Value> = env.collections
                           .clone()
                           .into_iter()
                           .filter({
        |i| i["name"].as_str() == Some(name)
    })
                           .collect();
    assert_eq!(f.len(), 1, "No collection matched {}", name);
    f.first()
     .expect("Internal error: count=1, but no last!?")
     .clone()
}

pub fn collection_with_id(env: &EnvironmentInfo, id: &str) -> Value {
    let f: Vec<Value> = env.collections
                           .clone()
                           .into_iter()
                           .filter({
        |i| i["collection_id"].as_str() == Some(id)
    })
                           .collect();
    assert_eq!(f.len(), 1, "No collection matched {}", id);
    f.last()
     .expect("Internal error: count=1, but no last!?")
     .clone()
}

pub fn select_collection(
    env_info: &EnvironmentInfo,
    matches: &clap::ArgMatches,
) -> Value {
    if matches.is_present("named") {
        collection_with_name(env_info, matches.value_of("named").unwrap())
    } else if matches.is_present("id") {
        collection_with_id(env_info, matches.value_of("id").unwrap())
    } else if matches.is_present("oldest") {
        oldest_collection(env_info)
    } else {
        newest_collection(env_info)
    }
}

pub fn configuration_with_name(
    env: &EnvironmentInfo,
    configuration_name: &str,
) -> Value {
    env.configurations
       .clone()
       .into_iter()
       .filter({
        |c| c["name"].as_str() == Some(configuration_name)
    })
       .last()
       .expect("No configuration found")
       .clone()
}

pub fn configuration_with_id(
    env: &EnvironmentInfo,
    configuration_id: &str,
) -> Value {
    env.configurations
       .clone()
       .into_iter()
       .filter({
        |c| c["configuration_id"].as_str() == Some(configuration_id)
    })
       .last()
       .expect("No configuration found")
       .clone()
}
<commit_msg>Bug fix: avoid panic when no config<commit_after>use clap;
use info::{DiscoveryServiceInfo, EnvironmentInfo};
use serde_json::Value;

pub fn read_only_environment(info: &DiscoveryServiceInfo) -> EnvironmentInfo {
    let read_only: Vec<EnvironmentInfo> =
        info.environments
            .clone()
            .into_iter()
            .filter({
            |env| env.environment["read_only"].as_bool().unwrap_or(false)
        })
            .collect();

    assert!(
        read_only.len() <= 1,
        format!("Multiple read only environments found {:?}", read_only)
    );

    read_only.first()
             .expect("No read only environment found")
             .clone()
}

pub fn writable_environment(info: &DiscoveryServiceInfo) -> EnvironmentInfo {
    let writables: Vec<EnvironmentInfo> =
        info.environments
            .clone()
            .into_iter()
            .filter({
            |env| !env.environment["read_only"].as_bool().unwrap_or(true)
        })
            .collect();

    assert!(
        writables.len() <= 1,
        format!("Multiple writable environments found {:?}", writables)
    );

    writables.first()
             .expect("No writable environment found")
             .clone()
}

pub fn oldest_collection(env: &EnvironmentInfo) -> Value {
    env.collections
       .first()
       .expect("No collections found")
       .clone()
}

pub fn newest_collection(env: &EnvironmentInfo) -> Value {
    env.collections
       .last()
       .expect("No collections found")
       .clone()
}

pub fn collection_with_name(env: &EnvironmentInfo, name: &str) -> Value {
    let f: Vec<Value> = env.collections
                           .clone()
                           .into_iter()
                           .filter({
        |i| i["name"].as_str() == Some(name)
    })
                           .collect();
    assert_eq!(f.len(), 1, "No collection matched {}", name);
    f.first()
     .expect("Internal error: count=1, but no last!?")
     .clone()
}

pub fn collection_with_id(env: &EnvironmentInfo, id: &str) -> Value {
    let f: Vec<Value> = env.collections
                           .clone()
                           .into_iter()
                           .filter({
        |i| i["collection_id"].as_str() == Some(id)
    })
                           .collect();
    assert_eq!(f.len(), 1, "No collection matched {}", id);
    f.last()
     .expect("Internal error: count=1, but no last!?")
     .clone()
}

pub fn select_collection(
    env_info: &EnvironmentInfo,
    matches: &clap::ArgMatches,
) -> Value {
    if matches.is_present("named") {
        collection_with_name(env_info, matches.value_of("named").unwrap())
    } else if matches.is_present("id") {
        collection_with_id(env_info, matches.value_of("id").unwrap())
    } else if matches.is_present("oldest") {
        oldest_collection(env_info)
    } else {
        newest_collection(env_info)
    }
}

pub fn configuration_with_name(
    env: &EnvironmentInfo,
    configuration_name: &str,
) -> Value {
    env.configurations
       .clone()
       .into_iter()
       .filter({
        |c| c["name"].as_str() == Some(configuration_name)
    })
       .last()
       .unwrap_or(Value::Null)
       .clone()
}

pub fn configuration_with_id(
    env: &EnvironmentInfo,
    configuration_id: &str,
) -> Value {
    env.configurations
       .clone()
       .into_iter()
       .filter({
        |c| c["configuration_id"].as_str() == Some(configuration_id)
    })
       .last()
       .unwrap_or(Value::Null)
       .clone()
}

{'fraction_non_alphanumeric': 0.14211414113016588, 'fraction_numerical': 0.0016868147315153219, 'mean_word_length': 2.0445014976465554, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1622152', 'n_tokens_mistral': 2109, 'n_tokens_neox': 2111, 'n_words': 434}