Expand description
Convert Unicode Chinese characters to Chinese telegraph codes (中文電碼).
Chinese telegraph codes — also known as Chinese commercial codes or standard telegraph codes — are four-digit numerical codes historically used to transmit Chinese text over telegraph, and still used today in some identity documents and immigration forms.
Both the Traditional Chinese (Taiwan) and Simplified Chinese code tables are included. Lookups use compile-time perfect hash maps, so there is no runtime table construction and no heap allocation.
§Usage
use chinese_telegraph::{to_telegraph, to_telegraph_str, Table};
// Look up a Traditional Chinese character
assert_eq!(to_telegraph("這", Table::TW), Some(6638));
// Look up a Simplified Chinese character
assert_eq!(to_telegraph("这", Table::CN), Some(6638));
// Search both tables (Traditional first, then Simplified)
assert_eq!(to_telegraph("一", Table::Both), Some(1));
// Format as the conventional 4-digit code — no heap allocation
assert_eq!(to_telegraph_str("一", Table::Both).unwrap(), "0001");The standard conversion traits are also supported via TelegraphCode.
Converting with try_into searches both tables (Table::Both:
Traditional first, then Simplified); use TelegraphCode::lookup to
search a specific table:
use chinese_telegraph::TelegraphCode;
// Searches the Traditional table first, then the Simplified table
let code: TelegraphCode = '一'.try_into()?;
let num: usize = code.into();
assert_eq!(num, 1);
assert_eq!(code.to_code_str(), "0001");The input must be exactly one character; longer strings return None.
To convert a sentence, iterate over its characters:
use chinese_telegraph::{Table, TelegraphCode};
let codes: Vec<_> = "電報"
.chars()
.filter_map(|c| TelegraphCode::lookup(c, Table::TW))
.map(|code| code.to_code_str())
.collect();
assert_eq!(codes, ["7193", "1032"]);§Feature flags
All features are enabled by default:
telegraph-code— theTelegraphCodetype with its standard conversion traits, and theNoTelegraphCodeerror.code-str— theCodeStrinline four-digit string type andto_telegraph_str.std— addsto_telegraph_stringfor formatting codes as 4-digitStrings, and (withtelegraph-code) implementsstd::error::ErrorforNoTelegraphCode.
The core lookup, to_telegraph, works without any features: the crate
is #![no_std] and allocation-free at its core, with heap-free
four-digit formatting available via code-str. For no_std
environments, disable std (or all default features) with
default-features = false.
Structs§
- CodeStr
code-str - The four-digit form of a telegraph code (e.g.
"0001"), stored inline without heap allocation. - NoTelegraph
Code telegraph-code - The error returned when a conversion to
TelegraphCodefails. - Telegraph
Code telegraph-code - A Chinese telegraph code, usable with the standard conversion traits.
Enums§
- Table
- Selects which code table(s) a lookup searches.
Functions§
- to_
telegraph - Converts a Chinese character to its telegraph code.
- to_
telegraph_ str code-str - Converts a Chinese character to its telegraph code, formatted as the conventional four-digit code with leading zeros.
- to_
telegraph_ string std - Converts a Chinese character to its telegraph code, formatted as the
conventional four-digit
Stringwith leading zeros.