use crate::{ Document, DocumentContent, DocumentTree, Inline, Organization, OrganizationMembership, OrganizationRole, RichText, User, Workspace, }; /// Produces a textual description from already available data. pub trait Contextualize { fn render_context(&self) -> String; } impl Contextualize for RichText { fn render_context(&self) -> String { let mut text = String::new(); for item in &self.0 { match item { Inline::Text(value) => text.push_str(value), Inline::Link { label, url } => { text.push_str(label); text.push_str(" ("); text.push_str(url); text.push(')'); } } } text } } impl Contextualize for DocumentContent { fn render_context(&self) -> String { match self { Self::Page { title } => format!("# {title}"), Self::Heading(text) => format!("## {}", text.render_context()), Self::Paragraph(text) => text.render_context(), Self::ListItem(text) => format!("- {}", text.render_context()), Self::Todo { text, checked } => { let mark = if *checked { 'x' } else { ' ' }; format!("- [{mark}] {}", text.render_context()) } Self::Code { language, source } => format!("Code ({language}):\n{source}"), Self::Json(value) => value.to_string(), } } } impl Contextualize for Document { fn render_context(&self) -> String { self.content.render_context() } } impl Contextualize for DocumentTree { fn render_context(&self) -> String { let mut text = String::new(); let mut first_line = true; for (depth, node) in self.nodes() { let rendered = node.render_context(); for line in rendered.split('\n') { if !first_line { text.push('\n'); } first_line = false; for _ in 1..depth { text.push_str(" "); } text.push_str(line); } } text } } impl Contextualize for User { fn render_context(&self) -> String { format!("User: {}", self.display_name) } } impl Contextualize for Workspace { fn render_context(&self) -> String { format!("Workspace: {}", self.name) } } impl Contextualize for Organization { fn render_context(&self) -> String { format!("Organization: {}", self.name) } } impl Contextualize for OrganizationMembership { fn render_context(&self) -> String { let role = match self.role { OrganizationRole::Owner => "owner", OrganizationRole::Admin => "admin", OrganizationRole::Member => "member", }; format!( "User {} is a {role} of organization {}", self.user_id.as_str(), self.organization_id.as_str() ) } }