#country-code #enums #codes #sqlx #compatible #serde #copyable

country-code-enum

Copyable Serde and Sqlx compatible country codes

2 unstable releases

0.2.0 Oct 6, 2024
0.1.0 Oct 5, 2024

#670 in Encoding

Download history 286/week @ 2024-10-01 76/week @ 2024-10-08 40/week @ 2024-10-15 12/week @ 2024-10-22 20/week @ 2024-10-29 23/week @ 2024-11-05 9/week @ 2024-11-12 8/week @ 2024-11-19 1/week @ 2024-11-26 171/week @ 2024-12-03 189/week @ 2024-12-10 232/week @ 2024-12-17 30/week @ 2024-12-24 22/week @ 2024-12-31 18/week @ 2025-01-07

330 downloads per month
Used in 2 crates (via anypki)

MIT license

18KB
480 lines

country-code-enum

License Crates.io Docs.rs

Copyable Serde and Sqlx compatible country codes

use country_code_enum::CountryCode;

let argentina: CountryCode = "AR".parse().unwrap();
assert_eq!(argentina, CountryCode::AR);
assert_eq!(argentina.as_ref(), "AR");
assert_eq!(argentina.name(), "Argentina");

Serde

use country_code_enum::CountryCode;

#[cfg(feature = "serde")]
{
    let argentina: CountryCode = serde_json::from_str("\"AR\"").unwrap();
    assert_eq!(argentina, CountryCode::AR);
    assert_eq!(serde_json::to_string(&argentina).unwrap(), "\"AR\"");
}

Sqlx

use country_code_enum::CountryCode;

async fn sqlx_example(pool: sqlx::PgPool) {
    let argentina = sqlx::query_scalar!(r#"SELECT 'AR'::varchar as "val: CountryCode""#)
        .fetch_one(&pool)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(argentina, CountryCode::AR);

    let s = sqlx::query_scalar!("SELECT $1::varchar", argentina as _)
        .fetch_one(&pool)
        .await
        .unwrap()
        .unwrap();
    assert_eq!(s, "AR");
}

Contributing

  • please run .pre-commit.sh before sending a PR, it will check everything

License

This project is licensed under the MIT license.

Dependencies

~0–15MB
~131K SLoC