Keep context implementations with their document models
This commit is contained in:
@@ -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()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
/// Produces a textual description from already available data.
|
||||||
|
pub trait Contextualize {
|
||||||
|
fn render_context(&self) -> String;
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
|
use super::contextualize::Contextualize;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
id::{IdError, validate_id},
|
id::{IdError, validate_id},
|
||||||
rich_text::RichText,
|
rich_text::RichText,
|
||||||
@@ -70,3 +72,26 @@ pub enum DocumentContent {
|
|||||||
/// Preserves documents written with the original arbitrary JSON model.
|
/// Preserves documents written with the original arbitrary JSON model.
|
||||||
Json(Value),
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
//! Logical models for documents, workspaces, users, and organizations.
|
//! Logical models for documents, workspaces, users, and organizations.
|
||||||
|
|
||||||
mod context;
|
mod contextualize;
|
||||||
mod document;
|
mod document;
|
||||||
mod id;
|
mod id;
|
||||||
mod organization;
|
mod organization;
|
||||||
@@ -9,7 +9,7 @@ mod tree;
|
|||||||
mod user;
|
mod user;
|
||||||
mod workspace;
|
mod workspace;
|
||||||
|
|
||||||
pub use context::Contextualize;
|
pub use contextualize::Contextualize;
|
||||||
pub use document::{Document, DocumentContent, DocumentId, DocumentKey};
|
pub use document::{Document, DocumentContent, DocumentId, DocumentKey};
|
||||||
pub use id::IdError;
|
pub use id::IdError;
|
||||||
pub use organization::{Organization, OrganizationId, OrganizationMembership, OrganizationRole};
|
pub use organization::{Organization, OrganizationId, OrganizationMembership, OrganizationRole};
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use super::contextualize::Contextualize;
|
||||||
use super::{
|
use super::{
|
||||||
id::{IdError, validate_id},
|
id::{IdError, validate_id},
|
||||||
user::UserId,
|
user::UserId,
|
||||||
@@ -40,3 +41,24 @@ pub struct OrganizationMembership {
|
|||||||
pub user_id: UserId,
|
pub user_id: UserId,
|
||||||
pub role: OrganizationRole,
|
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()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::contextualize::Contextualize;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
|
||||||
#[serde(tag = "kind", content = "data", rename_all = "snake_case")]
|
#[serde(tag = "kind", content = "data", rename_all = "snake_case")]
|
||||||
pub enum Inline {
|
pub enum Inline {
|
||||||
@@ -16,3 +18,21 @@ impl From<&str> for RichText {
|
|||||||
Self(vec![Inline::Text(value.into())])
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
use super::contextualize::Contextualize;
|
||||||
|
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::{Document, DocumentId};
|
use crate::{Document, DocumentId};
|
||||||
@@ -79,3 +81,24 @@ impl DocumentTree {
|
|||||||
.chain(self.descendants.iter().map(|(depth, node)| (*depth, node)))
|
.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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use super::contextualize::Contextualize;
|
||||||
use super::id::{IdError, validate_id};
|
use super::id::{IdError, validate_id};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||||
@@ -22,3 +23,9 @@ pub struct User {
|
|||||||
pub id: UserId,
|
pub id: UserId,
|
||||||
pub display_name: String,
|
pub display_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Contextualize for User {
|
||||||
|
fn render_context(&self) -> String {
|
||||||
|
format!("User: {}", self.display_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use super::contextualize::Contextualize;
|
||||||
use super::{
|
use super::{
|
||||||
id::{IdError, validate_id},
|
id::{IdError, validate_id},
|
||||||
organization::OrganizationId,
|
organization::OrganizationId,
|
||||||
@@ -27,3 +28,9 @@ pub struct Workspace {
|
|||||||
pub organization_id: OrganizationId,
|
pub organization_id: OrganizationId,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Contextualize for Workspace {
|
||||||
|
fn render_context(&self) -> String {
|
||||||
|
format!("Workspace: {}", self.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user