pub fn country_code<'a>(database: &Database, country: Option<country::Id>, mut number: Number<'a>) -> Result<Number<'a>, error::Parse> {
	let idd = country
		.and_then(|c| database.by_id(c.as_ref()))
		.and_then(|m| m.international_prefix.as_ref());

	number = international_prefix(idd, number);

	match number.country {
		// The country source was found from the initial PLUS or it was extract
		// from the number already.
		country::Source::Plus | country::Source::Idd | country::Source::Number => {
			if number.national.len() <= consts::MIN_LENGTH_FOR_NSN {
				return Err(error::Parse::TooShortNsn.into());
			}

			// If the prefix was already extracted, check it is valid.
			if number.prefix.is_some() {
				let prefix = number.prefix.as_ref().unwrap().parse()?;

				if database.by_code(&prefix).is_none() {
					return Err(error::Parse::InvalidCountryCode.into());
				}
				else {
					return Ok(number)
				}
			}
			else {
				// Check the possible country code does not start with a 0 since those
				// are invalid.
				if number.national.starts_with('0') {
					return Err(error::Parse::InvalidCountryCode.into());
				}

				// Try to find the first available country code.
				for len in 1 .. consts::MAX_LENGTH_FOR_COUNTRY_CODE + 1 {
					let code = number.national[.. len].parse().unwrap();

					if database.by_code(&code).is_some() {
						number.national = trim(number.national, len);
						number.prefix   = Some(code.to_string().into());

						return Ok(number);
					}
				}
			}
		}

		country::Source::Default => {
			if let Some(country) = country {
				let meta = database.by_id(country.as_ref()).unwrap();
				let code = meta.country_code.to_string();

				if number.national.starts_with(&code) &&
				   (!meta.descriptors().general().is_match(&number.national) ||
				    !validator::length(meta, &number, Type::Unknown).is_possible())
				{
					number.country  = country::Source::Number;
					number.national = trim(number.national, code.len());
				}

				number.prefix = Some(code.into());

				return Ok(number);
			}
		}
	}

	Err(error::Parse::InvalidCountryCode.into())
}
/// Parse and insert the proper country code.
Parse and insert the proper country code.
Parse and insert the proper country code .
pub fn country_code<'a>(database: &Database, country: Option<country::Id>, mut number: Number<'a>) -> Result<Number<'a>, error::Parse> {
	let idd = country
		.and_then(|c| database.by_id(c.as_ref()))
		.and_then(|m| m.international_prefix.as_ref());
	number = international_prefix(idd, number);
	match number.country {
		// The country source was found from the initial PLUS or it was extract
		// from the number already.
		country::Source::Plus | country::Source::Idd | country::Source::Number => {
			if number.national.len() <= consts::MIN_LENGTH_FOR_NSN {
				return Err(error::Parse::TooShortNsn.into());
			}
			// If the prefix was already extracted, check it is valid.
			if number.prefix.is_some() {
				let prefix = number.prefix.as_ref().unwrap().parse()?;
				if database.by_code(&prefix).is_none() {
					return Err(error::Parse::InvalidCountryCode.into());
				}
				else {
					return Ok(number)
				}
			}
			else {
				// Check the possible country code does not start with a 0 since those
				// are invalid.
				if number.national.starts_with('0') {
					return Err(error::Parse::InvalidCountryCode.into());
				}
				// Try to find the first available country code.
				for len in 1 .. consts::MAX_LENGTH_FOR_COUNTRY_CODE + 1 {
					let code = number.national[.. len].parse().unwrap();
					if database.by_code(&code).is_some() {
						number.national = trim(number.national, len);
						number.prefix   = Some(code.to_string().into());
						return Ok(number);
					}
				}
			}
		}
		country::Source::Default => {
			if let Some(country) = country {
				let meta = database.by_id(country.as_ref()).unwrap();
				let code = meta.country_code.to_string();
				if number.national.starts_with(&code) &&
				   (!meta.descriptors().general().is_match(&number.national) ||
				    !validator::length(meta, &number, Type::Unknown).is_possible())
				{
					number.country  = country::Source::Number;
					number.national = trim(number.national, code.len());
				}
				number.prefix = Some(code.into());
				return Ok(number);
			}
		}
	}
	Err(error::Parse::InvalidCountryCode.into())
}
pub fn country_code < ' a > ( database : & Database , country : Option < country :: Id > , mut number : Number < ' a > ) -> Result < Number < ' a > , error :: Parse > { let idd = country . and_then ( | c | database . by_id ( c . as_ref ( ) ) ) . and_then ( | m | m . international_prefix . as_ref ( ) ) ; number = international_prefix ( idd , number ) ; match number . country { // The country source was found from the initial PLUS or it was extract // from the number already. country :: Source :: Plus | country :: Source :: Idd | country :: Source :: Number => { if number . national . len ( ) <= consts :: MIN_LENGTH_FOR_NSN { return Err ( error :: Parse :: TooShortNsn . into ( ) ) ; } // If the prefix was already extracted, check it is valid. if number . prefix . is_some ( ) { let prefix = number . prefix . as_ref ( ) . unwrap ( ) . parse ( ) ? ; if database . by_code ( & prefix ) . is_none ( ) { return Err ( error :: Parse :: InvalidCountryCode . into ( ) ) ; } else { return Ok ( number ) } } else { // Check the possible country code does not start with a 0 since those // are invalid. if number . national . starts_with ( '0' ) { return Err ( error :: Parse :: InvalidCountryCode . into ( ) ) ; } // Try to find the first available country code. for len in 1 .. consts :: MAX_LENGTH_FOR_COUNTRY_CODE + 1 { let code = number . national [ .. len ] . parse ( ) . unwrap ( ) ; if database . by_code ( & code ) . is_some ( ) { number . national = trim ( number . national , len ) ; number . prefix = Some ( code . to_string ( ) . into ( ) ) ; return Ok ( number ) ; } } } } country :: Source :: Default => { if let Some ( country ) = country { let meta = database . by_id ( country . as_ref ( ) ) . unwrap ( ) ; let code = meta . country_code . to_string ( ) ; if number . national . starts_with ( & code ) && ( ! meta . descriptors ( ) . general ( ) . is_match ( & number . national ) || ! validator :: length ( meta , & number , Type :: Unknown ) . is_possible ( ) ) { number . country = country :: Source :: Number ; number . national = trim ( number . national , code . len ( ) ) ; } number . prefix = Some ( code . into ( ) ) ; return Ok ( number ) ; } } } Err ( error :: Parse :: InvalidCountryCode . into ( ) ) }
Parse and insert the proper country code.
Parse and insert the proper country code .
database
&Database
country
Option<country::Id>
number
Number<'a>