balanced_ternary/
conversions.rs

1//! This module provides conversions between `Digit` and `Ternary` types and common Rust types such as `char`, `&str`, and `String`.
2//!
3//! # Overview
4//! The module defines `impl From` traits for seamless conversions:
5//!
6//! - `Digit` <-> `char`: Converts digits to and from their character representation.
7//! - `Digit` <-> `i8`: Converts digits to and from their byte representation.
8//! - `Ternary` <-> `&str` / `String`: Allows parsing and generating ternary numbers from strings.
9//! - `Ternary` <-> `i64`: Converts ternary numbers from/to decimal numbers.
10//!
11//! The primary goal of these conversions is to simplify working with `Digit` and `Ternary` types by leveraging Rust's `From` and `Into` traits.
12
13use crate::{Digit, Ternary};
14use alloc::string::{String, ToString};
15
16impl From<char> for Digit {
17    fn from(value: char) -> Self {
18        Self::from_char(value)
19    }
20}
21
22impl From<i8> for Digit {
23    fn from(value: i8) -> Self {
24        Self::from_i8(value)
25    }
26}
27
28impl From<Digit> for char {
29    fn from(value: Digit) -> Self {
30        value.to_char()
31    }
32}
33
34impl From<Digit> for i8 {
35    fn from(value: Digit) -> Self {
36        value.to_i8()
37    }
38}
39
40impl From<&str> for Ternary {
41    fn from(value: &str) -> Self {
42        Self::parse(value)
43    }
44}
45
46impl From<String> for Ternary {
47    fn from(value: String) -> Self {
48        Self::from(value.as_str())
49    }
50}
51
52impl From<i64> for Ternary {
53    fn from(value: i64) -> Self {
54        Self::from_dec(value)
55    }
56}
57
58impl From<Ternary> for String {
59    fn from(value: Ternary) -> Self {
60        value.to_string()
61    }
62}
63
64impl From<Ternary> for i64 {
65    fn from(value: Ternary) -> Self {
66        value.to_dec()
67    }
68}