Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Once cell instead of lazy_static #66

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ bincode = "1.3"
either = "1.11"
fnv = "1"
itertools = ">=0.10, <= 0.12"
lazy_static = "1.4"
once_cell = "1"
nom = "7.1"
quick-xml = ">=0.28, <= 0.31"
regex = "1.7"
Expand Down
Empty file added clippy.toml
Empty file.
2 changes: 1 addition & 1 deletion src/carrier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl AsRef<str> for Carrier {
}

impl fmt::Display for Carrier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
653 changes: 329 additions & 324 deletions src/consts.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/country.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,4 @@ pub enum Id {
ZW,
}

pub use self::Id::*;
pub use Id::*;
2 changes: 1 addition & 1 deletion src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl AsRef<str> for Extension {
}

impl fmt::Display for Extension {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
8 changes: 4 additions & 4 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn format_with<'d, 'n>(
}

impl<'n, 'd, 'f> fmt::Display for Formatter<'n, 'd, 'f> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let db = self.database.unwrap_or(&DATABASE);

// If the country code is invalid, return an error.
Expand Down Expand Up @@ -247,9 +247,9 @@ fn replace(
.get(1)
.unwrap()
.as_str();
let format = transform.replace(*consts::NP, meta.national_prefix().unwrap_or(""));
let format = format.replace(*consts::FG, &format!("${}", first));
let format = format.replace(*consts::CC, carrier.unwrap_or(""));
let format = transform.replace(consts::NP, meta.national_prefix().unwrap_or(""));
let format = format.replace(consts::FG, &format!("${}", first));
let format = format.replace(consts::CC, carrier.unwrap_or(""));

consts::FIRST_GROUP.replace(formatter.format(), &*format)
} else {
Expand Down
32 changes: 18 additions & 14 deletions src/metadata/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::error;
use crate::metadata::loader;
use crate::Metadata;
use bincode;
use bincode::Options;
use fnv::FnvHashMap;
use once_cell::sync::Lazy;
use regex_cache::{CachedRegex, CachedRegexBuilder, RegexCache};
use std::borrow::Borrow;
use std::fs::File;
use std::hash::Hash;
use std::io::{BufReader, Cursor};
use std::path::Path;
use std::sync::{Arc, Mutex};

use bincode::Options;
use fnv::FnvHashMap;
use regex_cache::{CachedRegex, CachedRegexBuilder, RegexCache};

use crate::error;
use crate::metadata::loader;
use crate::Metadata;

const DATABASE: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/database.bin"));

lazy_static::lazy_static! {
/// The Google provided metadata database, used as default.
pub static ref DEFAULT: Database =
Database::from(bincode::options()
.with_varint_encoding().deserialize(DATABASE).unwrap()).unwrap();
}
/// The Google provided metadata database, used as default.
pub static DEFAULT: Lazy<Database> = Lazy::new(|| {
Database::from(
bincode::options()
.with_varint_encoding()
.deserialize(DATABASE)
.unwrap(),
)
.unwrap()
});

/// Representation of a database of metadata for phone number.
#[derive(Clone, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/national_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl From<NationalNumber> for u64 {
}

impl fmt::Display for NationalNumber {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for _ in 0..self.zeros() {
write!(f, "0")?;
}
Expand Down
20 changes: 9 additions & 11 deletions src/parser/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::consts;
use crate::country;
use crate::error;
use crate::metadata::{Database, Metadata};
use crate::phone_number::Type;
use crate::validator;
use fnv::FnvHashMap;
use nom::{
character::complete::*,
combinator::*,
error::{make_error, ErrorKind},
multi::*,
AsChar, IResult,
};
use std::borrow::Cow;

use fnv::FnvHashMap;
use regex_cache::CachedRegex;

use crate::consts;
use crate::country;
use crate::error;
use crate::metadata::{Database, Metadata};
use crate::phone_number::Type;
use crate::validator;
use std::borrow::Cow;

macro_rules! parse {
($input:ident => ) => ();
Expand Down Expand Up @@ -369,7 +367,7 @@ pub fn normalize<'a>(mut number: Number<'a>, mappings: &FnvHashMap<char, char>)
number
}

pub fn trim(value: Cow<str>, start: usize) -> Cow<str> {
pub fn trim(value: Cow<'_, str>, start: usize) -> Cow<'_, str> {
match value {
Cow::Borrowed(value) => Cow::Borrowed(&value[start..]),

Expand Down
3 changes: 1 addition & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::metadata::{Database, DATABASE};
use crate::national_number::NationalNumber;
use crate::phone_number::{PhoneNumber, Type};
use crate::validator::{self, Validation};

use nom::{branch::alt, IResult};

#[macro_use]
Expand All @@ -44,7 +43,7 @@ pub fn parse_with<S: AsRef<str>>(
country: Option<country::Id>,
string: S,
) -> Result<PhoneNumber, error::Parse> {
fn phone_number(i: &str) -> IResult<&str, helper::Number> {
fn phone_number(i: &str) -> IResult<&str, helper::Number<'_>> {
parse! { i => alt((rfc3966::phone_number, natural::phone_number)) }
}

Expand Down
7 changes: 3 additions & 4 deletions src/parser/natural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use nom::error::ErrorKind;
use nom::IResult;

use crate::consts;
use crate::parser::helper::*;
use nom::error::ErrorKind;
use nom::IResult;

pub fn phone_number(i: &str) -> IResult<&str, Number> {
pub fn phone_number(i: &str) -> IResult<&str, Number<'_>> {
let (_, i) = extract(i)?;
let extension = consts::EXTN_PATTERN.captures(i);

Expand Down
7 changes: 3 additions & 4 deletions src/parser/rfc3966.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::parser::helper::*;
use fnv::FnvHashMap;
use nom::{
bytes::complete::*,
Expand All @@ -22,9 +23,7 @@ use nom::{
AsChar, IResult,
};

use crate::parser::helper::*;

pub fn phone_number(i: &str) -> IResult<&str, Number> {
pub fn phone_number(i: &str) -> IResult<&str, Number<'_>> {
parse! { i =>
opt(tag_no_case("Tel:"));
let prefix = opt(prefix);
Expand Down Expand Up @@ -168,6 +167,6 @@ mod test {
#[test]
fn advisory_1() {
// Just make sure this does not panic.
let _ = rfc3966::phone_number(".;phone-context=");
drop(rfc3966::phone_number(".;phone-context="));
}
}
6 changes: 3 additions & 3 deletions src/phone_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ impl FromStr for PhoneNumber {
}

impl fmt::Display for PhoneNumber {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.format())
}
}

impl PhoneNumber {
/// Get information about the country for the phone number.
pub fn country(&self) -> Country {
pub fn country(&self) -> Country<'_> {
Country(self)
}

Expand Down Expand Up @@ -251,7 +251,7 @@ impl<'a> Deref for Country<'a> {

#[cfg(test)]
mod test {
use crate::country::{self, *};
use crate::country::{self, Id::*};
use crate::metadata::DATABASE;
use crate::Type;
use crate::{parser, Mode, PhoneNumber};
Expand Down
5 changes: 2 additions & 3 deletions src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use either::*;

use crate::consts;
use crate::country;
use crate::metadata::{Database, Metadata, DATABASE};
use crate::parser;
use crate::parser::helper::Number as ParseNumber;
use crate::phone_number::{PhoneNumber, Type};
use either::*;

/// Possible outcomes when testing if a `PhoneNumber` is possible.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
Expand Down Expand Up @@ -106,7 +105,7 @@ pub fn is_valid_with(database: &Database, number: &PhoneNumber) -> bool {
.unwrap_or(false)
}

pub fn length(meta: &Metadata, number: &ParseNumber, kind: Type) -> Validation {
pub fn length(meta: &Metadata, number: &ParseNumber<'_>, kind: Type) -> Validation {
let desc = if let Some(desc) = meta.descriptors().get(kind) {
desc
} else {
Expand Down
Loading