Keep context implementations with their document models

This commit is contained in:
2026-11-30 18:56:28 +08:00
parent 455a3580d6
commit 66b6910eb1
9 changed files with 110 additions and 106 deletions
-104
View File
@@ -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()
)
}
}
+4
View File
@@ -0,0 +1,4 @@
/// Produces a textual description from already available data.
pub trait Contextualize {
fn render_context(&self) -> String;
}
+25
View File
@@ -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()
}
}
+2 -2
View File
@@ -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};
+22
View File
@@ -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()
)
}
}
+20
View File
@@ -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
}
}
+23
View File
@@ -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
}
}
+7
View File
@@ -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)
}
}
+7
View File
@@ -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)
}
}