Skip to content

Commit 6937920

Browse files
committed
feat: base45
1 parent eba2f98 commit 6937920

File tree

7 files changed

+32
-0
lines changed

7 files changed

+32
-0
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ base-x = { version = "0.2.7", default-features = false }
1919
base256emoji = "1.0.2"
2020
data-encoding = { version = "2.3.1", default-features = false, features = ["alloc"] }
2121
data-encoding-macro = "0.1.9"
22+
base45 = "3.1.0"
2223

2324
[dev-dependencies]
2425
criterion = "0.7"

cli/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl fmt::Display for StrBase {
8484
Base::Base32Z => "base32z",
8585
Base::Base36Lower => "base36lower",
8686
Base::Base36Upper => "base36upper",
87+
Base::Base45 => "base45",
8788
Base::Base58Flickr => "base58flickr",
8889
Base::Base58Btc => "base58btc",
8990
Base::Base64 => "base64",
@@ -118,6 +119,7 @@ impl FromStr for StrBase {
118119
"base32z" => Ok(Base::Base32Z),
119120
"base36lower" => Ok(Base::Base36Lower),
120121
"base36upper" => Ok(Base::Base36Upper),
122+
"base45" => Ok(Base::Base45),
121123
"base58flickr" => Ok(Base::Base58Flickr),
122124
"base58btc" => Ok(Base::Base58Btc),
123125
"base64" => Ok(Base::Base64),

src/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ build_base_enum! {
8282
'k' => Base36Lower,
8383
/// Base36, [0-9A-Z] no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ).
8484
'K' => Base36Upper,
85+
/// Base45, rfc9285 (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:).
86+
'R' => Base45,
8587
/// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ).
8688
'Z' => Base58Flickr,
8789
/// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz).

src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ impl From<data_encoding::DecodeError> for Error {
4141
Self::InvalidBaseString
4242
}
4343
}
44+
45+
impl From<base45::DecodeError> for Error {
46+
fn from(_: base45::DecodeError) -> Self {
47+
Self::InvalidBaseString
48+
}
49+
}

src/impls.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,16 @@ impl BaseCodec for Base36Upper {
158158
Ok(base_x::decode(encoding::BASE36_UPPER, &uppercased)?)
159159
}
160160
}
161+
162+
/// Base45, rfc9285 (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:).
163+
pub(crate) struct Base45;
164+
165+
impl BaseCodec for Base45 {
166+
fn encode<I: AsRef<[u8]>>(input: I) -> String {
167+
base45::encode(input.as_ref())
168+
}
169+
170+
fn decode<I: AsRef<str>>(input: I) -> Result<Vec<u8>> {
171+
Ok(base45::decode(input.as_ref())?)
172+
}
173+
}

tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn test_basic() {
5858
(Base32Z, "hxf1zgedpcfzg1ebb"),
5959
(Base36Lower, "k2lcpzo5yikidynfl"),
6060
(Base36Upper, "K2LCPZO5YIKIDYNFL"),
61+
(Base45, "RRFF.OEB$D5/DZ24"),
6162
(Base58Flickr, "Z7Pznk19XTTzBtx"),
6263
(Base58Btc, "z7paNL19xttacUY"),
6364
(Base64, "meWVzIG1hbmkgIQ"),

0 commit comments

Comments
 (0)