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;
14
15#[cfg(feature = "ternary-string")]
16use alloc::string::{String, ToString};
17
18#[cfg(feature = "ternary-string")]
19use crate::Ternary;
20
21impl From<char> for Digit {
22    fn from(value: char) -> Self {
23        Self::from_char(value)
24    }
25}
26
27impl From<i8> for Digit {
28    fn from(value: i8) -> Self {
29        Self::from_i8(value)
30    }
31}
32
33impl From<Digit> for char {
34    fn from(value: Digit) -> Self {
35        value.to_char()
36    }
37}
38
39impl From<Digit> for i8 {
40    fn from(value: Digit) -> Self {
41        value.to_i8()
42    }
43}
44
45#[cfg(feature = "ternary-string")]
46impl From<&str> for Ternary {
47    fn from(value: &str) -> Self {
48        Self::parse(value)
49    }
50}
51
52#[cfg(feature = "ternary-string")]
53impl From<String> for Ternary {
54    fn from(value: String) -> Self {
55        Self::from(value.as_str())
56    }
57}
58
59#[cfg(feature = "ternary-string")]
60impl From<i64> for Ternary {
61    fn from(value: i64) -> Self {
62        Self::from_dec(value)
63    }
64}
65
66#[cfg(feature = "ternary-string")]
67impl From<Ternary> for String {
68    fn from(value: Ternary) -> Self {
69        value.to_string()
70    }
71}
72
73#[cfg(feature = "ternary-string")]
74impl From<Ternary> for i64 {
75    fn from(value: Ternary) -> Self {
76        value.to_dec()
77    }
78}