use crate::log::logln;
use crate::model::text::fmt::*;
use golem_client::model::{Account, Role};
use serde::{Deserialize, Serialize};
fn account_fields(account: &Account) -> Vec<(String, String)> {
let mut fields = FieldsBuilder::new();
fields
.fmt_field("Account ID", &account.id, format_main_id)
.fmt_field("E-mail", &account.email, format_id)
.field("Name", &account.name);
fields.build()
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AccountGetView(pub Account);
impl MessageWithFields for AccountGetView {
fn message(&self) -> String {
format!(
"Got metadata for account {}",
format_message_highlight(&self.0.id)
)
}
fn fields(&self) -> Vec<(String, String)> {
account_fields(&self.0)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountNewView(pub Account);
impl MessageWithFields for AccountNewView {
fn message(&self) -> String {
format!(
"Created new account {}",
format_message_highlight(&self.0.id)
)
}
fn fields(&self) -> Vec<(String, String)> {
account_fields(&self.0)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountUpdateView(pub Account);
impl MessageWithFields for AccountUpdateView {
fn message(&self) -> String {
format!("Updated account {}", format_message_highlight(&self.0.id))
}
fn fields(&self) -> Vec<(String, String)> {
account_fields(&self.0)
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct GrantGetView(pub Vec<Role>);
impl TextView for GrantGetView {
fn log(&self) {
if self.0.is_empty() {
logln("No roles granted")
} else {
logln("Granted roles:");
for role in &self.0 {
logln(format!(" - {role}"));
}
}
}
}