diff --git a/crates/documents/src/context.rs b/crates/documents/src/context.rs deleted file mode 100644 index 99d2edb..0000000 --- a/crates/documents/src/context.rs +++ /dev/null @@ -1,104 +0,0 @@ -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() - ) - } -} diff --git a/crates/documents/src/contextualize.rs b/crates/documents/src/contextualize.rs new file mode 100644 index 0000000..1c16864 --- /dev/null +++ b/crates/documents/src/contextualize.rs @@ -0,0 +1,4 @@ +/// Produces a textual description from already available data. +pub trait Contextualize { + fn render_context(&self) -> String; +} diff --git a/crates/documents/src/document.rs b/crates/documents/src/document.rs index 35903f5..530f316 100644 --- a/crates/documents/src/document.rs +++ b/crates/documents/src/document.rs @@ -1,6 +1,8 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; +use super::contextualize::Contextualize; + use super::{ id::{IdError, validate_id}, rich_text::RichText, @@ -70,3 +72,26 @@ pub enum DocumentContent { /// Preserves documents written with the original arbitrary JSON model. Json(Value), } + +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() + } +} diff --git a/crates/documents/src/lib.rs b/crates/documents/src/lib.rs index 0ce51f4..d5d258c 100644 --- a/crates/documents/src/lib.rs +++ b/crates/documents/src/lib.rs @@ -1,6 +1,6 @@ //! Logical models for documents, workspaces, users, and organizations. -mod context; +mod contextualize; mod document; mod id; mod organization; @@ -9,7 +9,7 @@ mod tree; mod user; mod workspace; -pub use context::Contextualize; +pub use contextualize::Contextualize; pub use document::{Document, DocumentContent, DocumentId, DocumentKey}; pub use id::IdError; pub use organization::{Organization, OrganizationId, OrganizationMembership, OrganizationRole}; diff --git a/crates/documents/src/organization.rs b/crates/documents/src/organization.rs index 0407d15..848db9e 100644 --- a/crates/documents/src/organization.rs +++ b/crates/documents/src/organization.rs @@ -1,3 +1,4 @@ +use super::contextualize::Contextualize; use super::{ id::{IdError, validate_id}, user::UserId, @@ -40,3 +41,24 @@ pub struct OrganizationMembership { pub user_id: UserId, pub role: OrganizationRole, } + +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() + ) + } +} diff --git a/crates/documents/src/rich_text.rs b/crates/documents/src/rich_text.rs index b49312b..5117bf8 100644 --- a/crates/documents/src/rich_text.rs +++ b/crates/documents/src/rich_text.rs @@ -1,5 +1,7 @@ use serde::{Deserialize, Serialize}; +use super::contextualize::Contextualize; + #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] #[serde(tag = "kind", content = "data", rename_all = "snake_case")] pub enum Inline { @@ -16,3 +18,21 @@ impl From<&str> for RichText { Self(vec![Inline::Text(value.into())]) } } + +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 + } +} diff --git a/crates/documents/src/tree.rs b/crates/documents/src/tree.rs index 7a6b4d8..1c3d665 100644 --- a/crates/documents/src/tree.rs +++ b/crates/documents/src/tree.rs @@ -1,5 +1,7 @@ use std::collections::{HashMap, HashSet}; +use super::contextualize::Contextualize; + use thiserror::Error; use crate::{Document, DocumentId}; @@ -79,3 +81,24 @@ impl DocumentTree { .chain(self.descendants.iter().map(|(depth, node)| (*depth, node))) } } + +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 + } +} diff --git a/crates/documents/src/user.rs b/crates/documents/src/user.rs index 9e1be6e..47ef685 100644 --- a/crates/documents/src/user.rs +++ b/crates/documents/src/user.rs @@ -1,3 +1,4 @@ +use super::contextualize::Contextualize; use super::id::{IdError, validate_id}; #[derive(Clone, Debug, Eq, Hash, PartialEq)] @@ -22,3 +23,9 @@ pub struct User { pub id: UserId, pub display_name: String, } + +impl Contextualize for User { + fn render_context(&self) -> String { + format!("User: {}", self.display_name) + } +} diff --git a/crates/documents/src/workspace.rs b/crates/documents/src/workspace.rs index dc701fa..437553e 100644 --- a/crates/documents/src/workspace.rs +++ b/crates/documents/src/workspace.rs @@ -1,3 +1,4 @@ +use super::contextualize::Contextualize; use super::{ id::{IdError, validate_id}, organization::OrganizationId, @@ -27,3 +28,9 @@ pub struct Workspace { pub organization_id: OrganizationId, pub name: String, } + +impl Contextualize for Workspace { + fn render_context(&self) -> String { + format!("Workspace: {}", self.name) + } +}