Skip to content

Commit

Permalink
fix char and index maps, enable tests (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trolldemorted committed Mar 9, 2024
1 parent 73933f8 commit 0dd81f6
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 29 deletions.
11 changes: 11 additions & 0 deletions src/types/strings/bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use super::*;

use crate::error::strings::InvalidBmpString;
use alloc::{boxed::Box, string::String, vec::Vec};
use once_cell::race::OnceBox;

/// A Basic Multilingual Plane (BMP) string, which is a subtype of [`UniversalString`]
/// containing only the BMP set of characters.
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct BmpString(Vec<u16>);
static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();

impl BmpString {
/// Converts the string into a set of big endian bytes.
Expand Down Expand Up @@ -38,6 +41,14 @@ impl StaticPermittedAlphabet for BmpString {
fn chars(&self) -> Box<dyn Iterator<Item = u32> + '_> {
Box::from(self.0.iter().map(|ch| *ch as u32))
}

fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
INDEX_MAP.get_or_init(Self::build_index_map)
}

fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
CHARACTER_MAP.get_or_init(Self::build_character_map)
}
}

impl TryFrom<String> for BmpString {
Expand Down
47 changes: 20 additions & 27 deletions src/types/strings/constrained.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use alloc::collections::BTreeMap;
use crate::error::strings::PermittedAlphabetError;
use alloc::{boxed::Box, vec::Vec};
use bitvec::prelude::*;
use once_cell::race::OnceBox;

use crate::types;

Expand All @@ -13,6 +12,8 @@ pub(crate) trait StaticPermittedAlphabet: Sized + Default {

fn push_char(&mut self, ch: u32);
fn chars(&self) -> Box<dyn Iterator<Item = u32> + '_>;
fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32>;
fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32>;
fn char_range_to_bit_range(mut range: core::ops::Range<usize>) -> core::ops::Range<usize> {
let width = Self::CHARACTER_WIDTH as usize;
range.start *= width;
Expand Down Expand Up @@ -96,34 +97,26 @@ pub(crate) trait StaticPermittedAlphabet: Sized + Default {
self.chars().count()
}

fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
static MAP: OnceBox<BTreeMap<u32, u32>> = OnceBox::new();

MAP.get_or_init(|| {
Box::new(
Self::CHARACTER_SET
.iter()
.copied()
.enumerate()
.map(|(i, e)| (e, i as u32))
.collect(),
)
})
fn build_index_map() -> Box<alloc::collections::BTreeMap<u32, u32>> {
Box::new(
Self::CHARACTER_SET
.iter()
.copied()
.enumerate()
.map(|(i, e)| (e, i as u32))
.collect(),
)
}

fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
static MAP: OnceBox<BTreeMap<u32, u32>> = OnceBox::new();

MAP.get_or_init(|| {
Box::new(
Self::CHARACTER_SET
.iter()
.copied()
.enumerate()
.map(|(i, e)| (i as u32, e))
.collect(),
)
})
fn build_character_map() -> Box<alloc::collections::BTreeMap<u32, u32>> {
Box::new(
Self::CHARACTER_SET
.iter()
.copied()
.enumerate()
.map(|(i, e)| (i as u32, e))
.collect(),
)
}

fn try_from_permitted_alphabet(
Expand Down
11 changes: 11 additions & 0 deletions src/types/strings/ia5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use super::*;

use crate::error::strings::InvalidIso646Character;
use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec};
use once_cell::race::OnceBox;

/// An string which only contains ASCII characters.
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Ia5String(Vec<u8>);
static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();

impl Ia5String {
pub fn from_iso646_bytes(bytes: &[u8]) -> Result<Self, InvalidIso646Character> {
Expand Down Expand Up @@ -133,4 +136,12 @@ impl super::StaticPermittedAlphabet for Ia5String {
);
self.0.push(ch as u8);
}

fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
INDEX_MAP.get_or_init(Self::build_index_map)
}

fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
CHARACTER_MAP.get_or_init(Self::build_character_map)
}
}
11 changes: 11 additions & 0 deletions src/types/strings/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ use super::*;

use crate::error::strings::{InvalidNumericString, PermittedAlphabetError};
use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec};
use once_cell::race::OnceBox;

/// A string which can only contain numbers or `SPACE` characters.
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct NumericString(Vec<u8>);
static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();

impl NumericString {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, InvalidNumericString> {
Expand Down Expand Up @@ -101,4 +104,12 @@ impl StaticPermittedAlphabet for NumericString {
);
self.0.push(ch as u8);
}

fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
INDEX_MAP.get_or_init(Self::build_index_map)
}

fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
CHARACTER_MAP.get_or_init(Self::build_character_map)
}
}
11 changes: 11 additions & 0 deletions src/types/strings/printable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use super::*;

use crate::error::strings::InvalidPrintableString;
use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec};
use once_cell::race::OnceBox;

/// A string, which contains the characters defined in X.680 41.4 Section, Table 10.
///
/// You must use `try_from` or `from_*` to construct a `PrintableString`.
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::module_name_repetitions)]
pub struct PrintableString(Vec<u8>);
static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();

impl StaticPermittedAlphabet for PrintableString {
/// `PrintableString` contains only "printable" characters.
Expand All @@ -32,6 +35,14 @@ impl StaticPermittedAlphabet for PrintableString {
fn chars(&self) -> Box<dyn Iterator<Item = u32> + '_> {
Box::from(self.0.iter().map(|byte| *byte as u32))
}

fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
INDEX_MAP.get_or_init(Self::build_index_map)
}

fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
CHARACTER_MAP.get_or_init(Self::build_character_map)
}
}

impl PrintableString {
Expand Down
11 changes: 11 additions & 0 deletions src/types/strings/visible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::*;

use crate::error::strings::InvalidIso646Character;
use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec};
use once_cell::race::OnceBox;

/// A string which contains a subset of the ISO 646 character set.
/// Type **should be** constructed by using `try_from` or `from` methods.
Expand All @@ -14,6 +15,8 @@ use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec};
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::module_name_repetitions)]
pub struct VisibleString(Vec<u8>);
static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();

impl VisibleString {
/// Create a new `VisibleString` from ISO 646 bytes (also known as US-ASCII/IA5/IRA5).
Expand Down Expand Up @@ -64,6 +67,14 @@ impl StaticPermittedAlphabet for VisibleString {
);
self.0.push(ch as u8);
}

fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
INDEX_MAP.get_or_init(Self::build_index_map)
}

fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
CHARACTER_MAP.get_or_init(Self::build_character_map)
}
}

impl core::fmt::Display for VisibleString {
Expand Down
1 change: 0 additions & 1 deletion src/uper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ mod tests {
}

#[test]
#[ignore]
fn numeric_string() {
round_trip!(
uper,
Expand Down
2 changes: 1 addition & 1 deletion tests/personnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ test! {
0x0E, 0x2E, 0x02, 0x02, 0x80
];

#[ignore] ax_uper(uper): Ax = <_>::default() => &[0x9e, 0x00, 0x06, 0x00, 0x04, 0x0a, 0x46, 0x90];
ax_uper(uper): Ax = <_>::default() => &[0x9e, 0x00, 0x06, 0x00, 0x04, 0x0a, 0x46, 0x90];

constrained_aper(aper): PersonnelRecordWithConstraints = <_>::default() => &[
0x86, 0x4a, 0x6f, 0x68, 0x6e, 0x50, 0x10, 0x53, 0x6d, 0x69, 0x74, 0x68,
Expand Down

0 comments on commit 0dd81f6

Please sign in to comment.