1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use cases::snakecase::to_snake_case;

/// Converts a `&str` to a `foreign_key`
///
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "foo_bar";
///     let expected_string: String = "foo_bar_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "Foo bar";
///     let expected_string: String = "foo_bar_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "Foo Bar";
///     let expected_string: String = "foo_bar_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "Foo::Bar";
///     let expected_string: String = "bar_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "Test::Foo::Bar";
///     let expected_string: String = "bar_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "FooBar";
///     let expected_string: String = "foo_bar_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "fooBar";
///     let expected_string: String = "foo_bar_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::to_foreign_key;
///     let mock_string: &str = "fooBar3";
///     let expected_string: String = "foo_bar_3_id".to_owned();
///     let asserted_string: String = to_foreign_key(mock_string);
///     assert!(asserted_string == expected_string);
///
/// ```
pub fn to_foreign_key(non_foreign_key_string: &str) -> String {
    if non_foreign_key_string.contains("::") {
        let split_string: Vec<&str> = non_foreign_key_string.split("::").collect();
        safe_convert(split_string[split_string.len() - 1])
    } else {
        safe_convert(non_foreign_key_string)
    }
}
fn safe_convert(safe_string: &str) -> String {
    let snake_cased: String = to_snake_case(safe_string);
    if snake_cased.ends_with("_id") {
        snake_cased
    } else {
        format!("{}{}", snake_cased, "_id")
    }
}

/// Determines if a `&str` is a `foreign_key`
///
/// ```
///     use inflector::suffix::foreignkey::is_foreign_key;
///     let mock_string: &str = "Foo bar string that is really really long";
///     let asserted_bool: bool = is_foreign_key(mock_string);
///     assert!(asserted_bool == false);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::is_foreign_key;
///     let mock_string: &str = "foo-bar-string-that-is-really-really-long";
///     let asserted_bool: bool = is_foreign_key(mock_string);
///     assert!(asserted_bool == false);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::is_foreign_key;
///     let mock_string: &str = "FooBarIsAReallyReallyLongString";
///     let asserted_bool: bool = is_foreign_key(mock_string);
///     assert!(asserted_bool == false);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::is_foreign_key;
///     let mock_string: &str = "Foo Bar Is A Really Really Long String";
///     let asserted_bool: bool = is_foreign_key(mock_string);
///     assert!(asserted_bool == false);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::is_foreign_key;
///     let mock_string: &str = "fooBarIsAReallyReallyLongString";
///     let asserted_bool: bool = is_foreign_key(mock_string);
///     assert!(asserted_bool == false);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::is_foreign_key;
///     let mock_string: &str = "foo_bar_string_that_is_really_really_long";
///     let asserted_bool: bool = is_foreign_key(mock_string);
///     assert!(asserted_bool == false);
///
/// ```
/// ```
///     use inflector::suffix::foreignkey::is_foreign_key;
///     let mock_string: &str = "foo_bar_string_that_is_really_really_long_id";
///     let asserted_bool: bool = is_foreign_key(mock_string);
///     assert!(asserted_bool == true);
///
/// ```
pub fn is_foreign_key(test_string: &str) -> bool {
    to_foreign_key(test_string.clone()) == test_string
}