MCPcopy Create free account
hub / github.com/ddd/gpb / new

Method new

src/format/format.rs:136–289  ·  view source on GitHub ↗
(
        country_format: &CountryFormat,
        prefix: Option<String>,
        suffix_filter: Option<String>,
        infix_filter: Option<String>,
        digit_override: Option<usize>,
    )

Source from the content-addressed store, hash-verified

134// Now modify the new() method to accept an infix parameter
135impl PhoneNumberGenerator {
136 pub fn new(
137 country_format: &CountryFormat,
138 prefix: Option<String>,
139 suffix_filter: Option<String>,
140 infix_filter: Option<String>,
141 digit_override: Option<usize>,
142 ) -> Result<Self, Error> {
143 let country_code = country_format.code.clone();
144
145 // Get the format-defined digits (how many digits in a complete number for this country)
146 let format_digits = if let Some(d) = digit_override {
147 d
148 } else {
149 match get_digits_for_country(country_format) {
150 Ok(digit_lengths) => {
151 if digit_lengths.is_empty() {
152 return Err(anyhow!("No digit length specified for country code: {}. Check format.json", country_code));
153 }
154 // Use the first/minimum digit length
155 *digit_lengths.iter().min().unwrap()
156 },
157 Err(e) => return Err(e)
158 }
159 };
160
161 // Calculate the standard area code length for this country
162 let standard_area_code_len = if !country_format.area_codes.is_empty() {
163 // Most countries have consistent area code lengths, so we can use the first one
164 // as a reference
165 country_format.area_codes[0].len()
166 } else {
167 0 // No area codes
168 };
169
170 // Filter available area codes and calculate remaining parts based on user-provided prefix
171 let (selected_area_codes, remaining_area_code_parts, digits_to_generate) =
172 if let Some(p) = &prefix {
173 // Using a user-defined prefix:
174 // 1. If prefix is shorter than or equal to expected area code length,
175 // use it to filter area codes
176 // 2. If prefix is longer than area codes, split it to extract area code
177 // and starting digits
178
179 // Check if any area codes are defined for this country
180 if country_format.area_codes.is_empty() {
181 // No area codes specified, use empty string as the only "area code"
182 // and generate numbers with the full prefix
183 (
184 vec!["".to_string()],
185 vec!["".to_string()],
186 format_digits.saturating_sub(p.len())
187 )
188 } else {
189 if p.len() <= standard_area_code_len {
190 // Prefix is shorter than or equal to typical area code length
191 // Filter area codes that start with this prefix
192 let mut matching_codes = Vec::new();
193 let mut remaining_parts = Vec::new();

Callers

nothing calls this directly

Calls 2

get_digits_for_countryFunction · 0.85
cloneMethod · 0.80

Tested by

no test coverage detected