<commit_before>pub mod ast;
mod merge;
mod parse;
pub mod ty;
mod typemap_macro;
pub mod utils;

use std::{borrow::Cow, cell::RefCell, fmt, mem, ops, rc::Rc};

use log::{debug, log_enabled, trace, warn};
use petgraph::{
    graph::{EdgeIndex, NodeIndex},
    Graph,
};
use proc_macro2::{Span, TokenStream};
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use smol_str::SmolStr;
use syn::{parse_quote, Ident, Type};

use crate::{
    error::{invalid_src_id_span, DiagnosticError, Result, SourceIdSpan},
    source_registry::SourceId,
    typemap::{
        ast::{get_trait_bounds, normalize_type, DisplayToTokens, GenericTypeConv, TypeName},
        ty::{
            ForeignConversationRule, ForeignType, ForeignTypeS, ForeignTypesStorage, RustType,
            RustTypeS,
        },
    },
    types::ForeignClassInfo,
};

pub(crate) use typemap_macro::{
    CItem, CItems, ExpandedFType, TypeMapConvRuleInfo, TypeMapConvRuleInfoExpanderHelper,
};
pub(crate) static TO_VAR_TEMPLATE: &str = "{to_var}";
pub(crate) static FROM_VAR_TEMPLATE: &str = "{from_var}";
pub(crate) static TO_VAR_TYPE_TEMPLATE: &str = "{to_var_type}";
pub(in crate::typemap) static FUNCTION_RETURN_TYPE_TEMPLATE: &str = "{function_ret_type}";
const MAX_TRY_BUILD_PATH_STEPS: usize = 7;

/// contains code to convert from one type to another
#[derive(Debug, Clone)]
pub(crate) struct TypeConvCode {
    pub(in crate::typemap) span: SourceIdSpan,
    code: String,
    params: Vec<SmolStr>,
}

impl PartialEq for TypeConvCode {
    fn eq(&self, o: &Self) -> bool {
        self.code == o.code
    }
}

#[derive(Debug)]
pub(crate) enum TypeConvCodeSubstParam<'a> {
    Name(&'a str),
    Tmp(&'a str),
}

impl TypeConvCode {
    pub(crate) fn invalid() -> Self {
        TypeConvCode {
            span: invalid_src_id_span(),
            code: "invalid code".into(),
            params: vec![],
        }
    }

    pub(crate) fn with_params<S: Into<String>>(
        code: S,
        span: SourceIdSpan,
        params: Vec<SmolStr>,
    ) -> TypeConvCode {
        let code: String = code.into();
        TypeConvCode { code, span, params }
    }

    /// # Panics
    pub(crate) fn new<S: Into<String>>(code: S, span: SourceIdSpan) -> TypeConvCode {
        let code: String = code.into();

        let mut params = Vec::with_capacity(1);

        if code.contains(TO_VAR_TEMPLATE) {
            params.push(TO_VAR_TEMPLATE.into());
        }
        if code.contains(FROM_VAR_TEMPLATE) {
            params.push(FROM_VAR_TEMPLATE.into());
        }

        if params.is_empty() {
            panic!(
                "Code: '{}' should contains {} or {}",
                code, TO_VAR_TEMPLATE, FROM_VAR_TEMPLATE
            );
        }

        TypeConvCode { code, span, params }
    }
    /// # Panics
    pub(crate) fn new2<S: Into<String>>(code: S, span: SourceIdSpan) -> TypeConvCode {
        let code: String = code.into();
        let mut params = Vec::with_capacity(2);
        if code.contains(TO_VAR_TEMPLATE) {
            params.push(TO_VAR_TEMPLATE.into());
        }
        if code.contains(FROM_VAR_TEMPLATE) {
            params.push(FROM_VAR_TEMPLATE.into());
        }

        if params.len() < 2 {
            panic!(
                "Code: '{}' should contains {} and {}",
                code, TO_VAR_TEMPLATE, FROM_VAR_TEMPLATE
            );
        }

        TypeConvCode { code, span, params }
    }

    pub(crate) fn as_str(&self) -> &str {
        self.code.as_str()
    }
    pub(crate) fn src_id(&self) -> SourceId {
        self.span.0
    }
    pub(crate) fn span(&self) -> Span {
        self.span.1
    }
    pub(crate) fn full_span(&self) -> SourceIdSpan {<commit_msg>cleanups: fix assert! instead of panic!<commit_after>pub mod ast;
mod merge;
mod parse;
pub mod ty;
mod typemap_macro;
pub mod utils;

use std::{borrow::Cow, cell::RefCell, fmt, mem, ops, rc::Rc};

use log::{debug, log_enabled, trace, warn};
use petgraph::{
    graph::{EdgeIndex, NodeIndex},
    Graph,
};
use proc_macro2::{Span, TokenStream};
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use smol_str::SmolStr;
use syn::{parse_quote, Ident, Type};

use crate::{
    error::{invalid_src_id_span, DiagnosticError, Result, SourceIdSpan},
    source_registry::SourceId,
    typemap::{
        ast::{get_trait_bounds, normalize_type, DisplayToTokens, GenericTypeConv, TypeName},
        ty::{
            ForeignConversationRule, ForeignType, ForeignTypeS, ForeignTypesStorage, RustType,
            RustTypeS,
        },
    },
    types::ForeignClassInfo,
};

pub(crate) use typemap_macro::{
    CItem, CItems, ExpandedFType, TypeMapConvRuleInfo, TypeMapConvRuleInfoExpanderHelper,
};
pub(crate) static TO_VAR_TEMPLATE: &str = "{to_var}";
pub(crate) static FROM_VAR_TEMPLATE: &str = "{from_var}";
pub(crate) static TO_VAR_TYPE_TEMPLATE: &str = "{to_var_type}";
pub(in crate::typemap) static FUNCTION_RETURN_TYPE_TEMPLATE: &str = "{function_ret_type}";
const MAX_TRY_BUILD_PATH_STEPS: usize = 7;

/// contains code to convert from one type to another
#[derive(Debug, Clone)]
pub(crate) struct TypeConvCode {
    pub(in crate::typemap) span: SourceIdSpan,
    code: String,
    params: Vec<SmolStr>,
}

impl PartialEq for TypeConvCode {
    fn eq(&self, o: &Self) -> bool {
        self.code == o.code
    }
}

#[derive(Debug)]
pub(crate) enum TypeConvCodeSubstParam<'a> {
    Name(&'a str),
    Tmp(&'a str),
}

impl TypeConvCode {
    pub(crate) fn invalid() -> Self {
        TypeConvCode {
            span: invalid_src_id_span(),
            code: "invalid code".into(),
            params: vec![],
        }
    }

    pub(crate) fn with_params<S: Into<String>>(
        code: S,
        span: SourceIdSpan,
        params: Vec<SmolStr>,
    ) -> TypeConvCode {
        let code: String = code.into();
        TypeConvCode { code, span, params }
    }

    /// # Panics
    pub(crate) fn new<S: Into<String>>(code: S, span: SourceIdSpan) -> TypeConvCode {
        let code: String = code.into();

        let mut params = Vec::with_capacity(1);

        if code.contains(TO_VAR_TEMPLATE) {
            params.push(TO_VAR_TEMPLATE.into());
        }
        if code.contains(FROM_VAR_TEMPLATE) {
            params.push(FROM_VAR_TEMPLATE.into());
        }

        if params.is_empty() {
            panic!(
                "Code: '{}' should contains {} or {}",
                code, TO_VAR_TEMPLATE, FROM_VAR_TEMPLATE
            );
        }

        TypeConvCode { code, span, params }
    }
    /// # Panics
    pub(crate) fn new2<S: Into<String>>(code: S, span: SourceIdSpan) -> TypeConvCode {
        let code: String = code.into();
        let mut params = Vec::with_capacity(2);
        if code.contains(TO_VAR_TEMPLATE) {
            params.push(TO_VAR_TEMPLATE.into());
        }
        if code.contains(FROM_VAR_TEMPLATE) {
            params.push(FROM_VAR_TEMPLATE.into());
        }

        assert!(
            params.len() >= 2,
            "Code: '{}' should contains {} and {}",
            code,
            TO_VAR_TEMPLATE,
            FROM_VAR_TEMPLATE
        );

        TypeConvCode { code, span, params }
    }

    pub(crate) fn as_str(&self) -> &str {
        self.code.as_str()
    }
    pub(crate) fn src_id(&self) -> SourceId {
        self.span.0
    }
    pub(crate) fn span(&self) -> Span {
        self.span.1
    }
    pub(crate) fn full_span(&self) -> SourceIdSpan {
{'fraction_non_alphanumeric': 0.13587395636951252, 'fraction_numerical': 0.0021545919741448962, 'mean_word_length': 2.6282364435759646, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 40, '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': '6505622', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2531, 'n_words': 540}