10 releases
0.2.1 | Jan 29, 2025 |
---|---|
0.2.0 | Jan 29, 2025 |
0.1.9 | Jan 28, 2025 |
#338 in Internationalization (i18n)
539 downloads per month
105KB
2K
SLoC
jscontact

- https://docs.rs/jscontact - documentation
- https://github.com/Its-Just-Nans/jscontact - repository
- https://www.rfc-editor.org/rfc/rfc9553 - RFC 9553
Tests
rm -rf tests/rfc9553
python tests/get_figures.py
cargo build
cargo test -- --test-threads=1
cargo test --no-default-features -- --test-threads=1
# the --test-threads=1 is used to have a deterministic (ordered) output
License
Licensed under the MIT license LICENSE except for the tests
directory.
The tests
directory contains the following files:
- the RFC 9553 figures
- some imported tests files from other repositories
lib.rs
:
JSContact
This crates implements types for the JSContact format as defined in RFC 9553.
To start using this crate, run the following command in your project directory:
cargo add jscontact
Simple deserialization example:
use jscontact::Card;
use serde_json;
let json_value = serde_json::json!({
"@type": "Card",
"version": "1.0",
"uid": "1234"
});
let card_deserialized: Card = Card::try_from(json_value).unwrap(); // deserialize from serde::Value
let card_string: String = String::try_from(card_deserialized.clone()).unwrap(); // serialize to String
let card: Card = card_string.parse().unwrap(); // deserialize with parse()
assert_eq!(card_deserialized, card);
Simple creation example:
use jscontact::{Card, CardKind, CardVersion, Name, NameComponent, NameComponentKind};
use serde_json;
let mut card = Card::new(CardVersion::OneDotZero, "my:uri");
card.kind = Some(CardKind::Individual);
let json = serde_json::to_string(&card).unwrap(); // serialize with serde
Get localized Card:
use jscontact::{Card, CardVersion, Name};
use std::collections::HashMap;
use serde_json::Value;
// create a card
let mut card = Card::new(CardVersion::OneDotZero, "my:uri");
let mut name = Name::default();
name.full = Some("John".to_string());
card.name = Some(name);
// add localization
let mut translations: HashMap<String, Value> = HashMap::new();
let mut name_en = Name::default();
name_en.full = Some("Johny".to_string());
translations.insert(
"name".to_string(),
serde_json::to_value(name_en).expect("Failed to serialize name"),
);
card.add_localization("en", translations);
// use localized card
let langs = card.get_available_languages();
assert_eq!(langs, vec!["en"]);
let localized = card.get_localized(&langs[0]).unwrap();
assert_eq!(localized.name.unwrap().full.unwrap(), "Johny");
Dependencies
~0.9–1.8MB
~38K SLoC