Implement AsRef for table_ref

This commit is contained in:
Love 2025-01-08 15:39:40 +01:00
parent 96aabc8019
commit 3ec57b4414
2 changed files with 24 additions and 4 deletions

View File

@ -6,10 +6,10 @@ use super::table_display::helper_table_print;
use super::{TableGet, TableTrait};
pub struct Table {
header: Option<String>,
column_left: String,
column_right: String,
rows: Vec<(String, f64)>,
pub(super) header: Option<String>,
pub(super) column_left: String,
pub(super) column_right: String,
pub(super) rows: Vec<(String, f64)>,
pub color: bool,
pub width: usize,
}

View File

@ -5,6 +5,7 @@ use crate::models::ExchangeRateResult;
use super::table_display::helper_table_print;
use super::table_getter::TableGet;
use super::table_trait::TableTrait;
use super::Table;
pub struct TableRef<'a> {
header: Option<&'a str>,
@ -90,3 +91,22 @@ impl<'a> Display for TableRef<'a> {
helper_table_print(f, self)
}
}
impl<'a> From<&'a Table> for TableRef<'a> {
fn from(table: &'a Table) -> Self {
let rows = table
.rows
.iter()
.map(|(left, right)| (left.as_str(), *right))
.collect();
TableRef {
header: table.header.as_deref(),
column_left: table.column_left.as_str(),
column_right: table.column_right.as_str(),
rows,
color: table.color,
width: table.width,
}
}
}