<commit_before>use std::result;

use library;
use nameutil::module_name;

pub type Result = result::Result<String, String>;

pub trait AsStr {
    fn as_str(&self) -> &str;
}

impl AsStr for Result {
    #[inline]
    fn as_str(&self) -> &str {
        self.as_ref().unwrap_or_else(|s| s)
    }
}

pub fn rust_type(library: &library::Library, type_id: library::TypeId) -> Result {
    use library::Type::*;
    use library::Fundamental::*;
    let type_ = library.type_(type_id);
    match type_ {
        &Fundamental(fund) => {
            let ok = |s: &str| Ok(s.into());
            let err = |s: &str| Err(s.into());
            match fund {
                None => err("()"),
                Boolean => ok("bool"),
                Int8 => ok("i8"),
                UInt8 => ok("u8"),
                Int16 => ok("i16"),
                UInt16 => ok("u16"),
                Int32 => ok("i32"),
                UInt32 => ok("u32"),
                Int64 => ok("i64"),
                UInt64 => ok("u64"),

                Int => ok("i32"),      //maybe dependent on target system
                UInt => ok("u32"),     //maybe dependent on target system

                Float => ok("f32"),
                Double => ok("f64"),

                Utf8 => ok("String"),

                Type => ok("Type"),
                Unsupported => err("Unsupported"),
                _ => err(&format!("Fundamental: {:?}", fund)),
            }
        },

        &Enumeration(ref enum_) => Ok(enum_.name.clone()),
        &Interface(ref interface) => Ok(interface.name.clone()),
        &Class(ref klass) => Ok(klass.name.clone()),
        _ => Err(format!("Unknown rust type: {:?}", type_.get_name())),
        //TODO: check usage library::Type::get_name() when no _ in this
    }
}

pub fn used_rust_type(library: &library::Library, type_id: library::TypeId) -> Result {
    use library::Type::*;
    use library::Fundamental::*;
    let rust_type = match library.type_(type_id) {
        &Fundamental(fund) => {
            match fund {
                Type => Ok("Type".into()),
                _ => Err("Don't need use".into()),
            }
        },
        &Enumeration(ref enum_) => Ok(enum_.name.clone()),
        &Interface(ref interface) => Ok(interface.name.clone()),
        &Class(ref klass) => Ok(klass.name.clone()),
        _ => Err("Don't need use".into()),
    };
    if type_id.ns_id == library::MAIN_NAMESPACE || type_id.ns_id == library::INTERNAL_NAMESPACE {
        rust_type
    } else {
        rust_type.map(|s| format!("{}::{}",
            module_name(&library.namespace(type_id.ns_id).name), s))
    }
}

pub fn parameter_rust_type(library: &library::Library, type_id:library::TypeId, direction: library::ParameterDirection) -> Result {
    use library::Type::*;
    let type_ = library.type_(type_id);<commit_msg>Fixed usage of glib::types::Type<commit_after>use std::result;

use library;
use nameutil::module_name;

pub type Result = result::Result<String, String>;

pub trait AsStr {
    fn as_str(&self) -> &str;
}

impl AsStr for Result {
    #[inline]
    fn as_str(&self) -> &str {
        self.as_ref().unwrap_or_else(|s| s)
    }
}

pub fn rust_type(library: &library::Library, type_id: library::TypeId) -> Result {
    use library::Type::*;
    use library::Fundamental::*;
    let type_ = library.type_(type_id);
    match type_ {
        &Fundamental(fund) => {
            let ok = |s: &str| Ok(s.into());
            let err = |s: &str| Err(s.into());
            match fund {
                None => err("()"),
                Boolean => ok("bool"),
                Int8 => ok("i8"),
                UInt8 => ok("u8"),
                Int16 => ok("i16"),
                UInt16 => ok("u16"),
                Int32 => ok("i32"),
                UInt32 => ok("u32"),
                Int64 => ok("i64"),
                UInt64 => ok("u64"),

                Int => ok("i32"),      //maybe dependent on target system
                UInt => ok("u32"),     //maybe dependent on target system

                Float => ok("f32"),
                Double => ok("f64"),

                Utf8 => ok("String"),

                Type => ok("types::Type"),
                Unsupported => err("Unsupported"),
                _ => err(&format!("Fundamental: {:?}", fund)),
            }
        },

        &Enumeration(ref enum_) => Ok(enum_.name.clone()),
        &Interface(ref interface) => Ok(interface.name.clone()),
        &Class(ref klass) => Ok(klass.name.clone()),
        _ => Err(format!("Unknown rust type: {:?}", type_.get_name())),
        //TODO: check usage library::Type::get_name() when no _ in this
    }
}

pub fn used_rust_type(library: &library::Library, type_id: library::TypeId) -> Result {
    use library::Type::*;
    let rust_type = match library.type_(type_id) {
        &Enumeration(ref enum_) => Ok(enum_.name.clone()),
        &Interface(ref interface) => Ok(interface.name.clone()),
        &Class(ref klass) => Ok(klass.name.clone()),
        _ => Err("Don't need use".into()),
    };
    if type_id.ns_id == library::MAIN_NAMESPACE || type_id.ns_id == library::INTERNAL_NAMESPACE {
        rust_type
    } else {
        rust_type.map(|s| format!("{}::{}",
            module_name(&library.namespace(type_id.ns_id).name), s))
    }
}

pub fn parameter_rust_type(library: &library::Library, type_id:library::TypeId, direction: library::ParameterDirection) -> Result {
    use library::Type::*;
    let type_ = library.type_(type_id);
{'fraction_non_alphanumeric': 0.17597816196542312, 'fraction_numerical': 0.013466787989080983, 'mean_word_length': 2.129840546697039, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, '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': '7288302', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1665, 'n_words': 356}