iota_conversion/
lib.rs

1#![deny(unused_extern_crates)]
2#![warn(
3    missing_debug_implementations,
4    missing_docs,
5    rust_2018_idioms,
6    unreachable_pub
7)]
8
9//! Trinary and unit conversion traits and methods
10
11#[macro_use]
12extern crate failure;
13#[macro_use]
14extern crate lazy_static;
15
16/// Provides useful unit definitions for Iota
17pub mod iota_units;
18mod trinary;
19/// Converts between strings and tryte-encoded strings
20pub mod trytes_converter;
21/// Provides converters between various unit representations of Iota
22pub mod unit_converter;
23
24pub use trinary::*;
25
26type Result<T> = ::std::result::Result<T, failure::Error>;
27
28/// Converts a slice of trits into a numeric value
29pub fn value(trits: &[i8]) -> i8 {
30    trits.iter().rev().fold(0, |acc, trit| acc * 3 + *trit)
31}
32
33/// Converts a slice of trits into a numeric value in i64
34pub fn long_value(trits: &[i8]) -> i64 {
35    trits
36        .iter()
37        .rev()
38        .fold(0, |acc, trit| acc * 3 + i64::from(*trit))
39}