ucm-core 0.1.13

Core types and traits for the Unified Content Model
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Content types for UCM blocks.
//!
//! Each block contains typed content that can be text, tables, code,
//! math expressions, media, JSON, or binary data.

use crate::id::BlockId;
use serde::{Deserialize, Serialize};

/// The content payload of a block.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Content {
    /// Plain, markdown, or rich text
    Text(Text),

    /// Tabular data with optional schema
    Table(Table),

    /// Source code with language hint
    Code(Code),

    /// Mathematical expressions
    Math(Math),

    /// Media references (images, audio, video)
    Media(Media),

    /// Structured JSON data
    Json {
        value: serde_json::Value,
        #[serde(skip_serializing_if = "Option::is_none")]
        schema: Option<JsonSchema>,
    },

    /// Raw binary data
    Binary {
        mime_type: String,
        #[serde(with = "base64_serde")]
        data: Vec<u8>,
        #[serde(default)]
        encoding: BinaryEncoding,
    },

    /// Composite block (contains other blocks by reference)
    Composite {
        layout: CompositeLayout,
        children: Vec<BlockId>,
    },
}

impl Content {
    /// Get the type tag for hashing and identification
    pub fn type_tag(&self) -> &'static str {
        match self {
            Content::Text(_) => "text",
            Content::Table(_) => "table",
            Content::Code(_) => "code",
            Content::Math(_) => "math",
            Content::Media(_) => "media",
            Content::Json { .. } => "json",
            Content::Binary { .. } => "binary",
            Content::Composite { .. } => "composite",
        }
    }

    /// Create a simple text content
    pub fn text(text: impl Into<String>) -> Self {
        Content::Text(Text {
            text: text.into(),
            format: TextFormat::Plain,
        })
    }

    /// Create markdown text content
    pub fn markdown(text: impl Into<String>) -> Self {
        Content::Text(Text {
            text: text.into(),
            format: TextFormat::Markdown,
        })
    }

    /// Create code content
    pub fn code(language: impl Into<String>, source: impl Into<String>) -> Self {
        Content::Code(Code {
            language: language.into(),
            source: source.into(),
            highlights: Vec::new(),
        })
    }

    /// Create JSON content
    pub fn json(value: serde_json::Value) -> Self {
        Content::Json {
            value,
            schema: None,
        }
    }

    /// Create table content from rows of strings
    pub fn table(rows: Vec<Vec<String>>) -> Self {
        let columns = if rows.is_empty() {
            Vec::new()
        } else {
            rows[0]
                .iter()
                .enumerate()
                .map(|(i, _)| Column {
                    name: format!("col{}", i),
                    data_type: Some(DataType::Text),
                    nullable: true,
                })
                .collect()
        };

        let table_rows = rows
            .into_iter()
            .map(|r| Row {
                cells: r.into_iter().map(Cell::Text).collect(),
            })
            .collect();

        Content::Table(Table {
            columns,
            rows: table_rows,
            schema: None,
        })
    }

    /// Check if the content is empty
    pub fn is_empty(&self) -> bool {
        match self {
            Content::Text(t) => t.text.is_empty(),
            Content::Table(t) => t.rows.is_empty(),
            Content::Code(c) => c.source.is_empty(),
            Content::Math(m) => m.expression.is_empty(),
            Content::Media(_) => false,
            Content::Json { value, .. } => value.is_null(),
            Content::Binary { data, .. } => data.is_empty(),
            Content::Composite { children, .. } => children.is_empty(),
        }
    }

    /// Get approximate size in bytes
    pub fn size_bytes(&self) -> usize {
        match self {
            Content::Text(t) => t.text.len(),
            Content::Table(t) => {
                t.columns.iter().map(|c| c.name.len()).sum::<usize>()
                    + t.rows
                        .iter()
                        .flat_map(|r| &r.cells)
                        .map(|c| c.size_bytes())
                        .sum::<usize>()
            }
            Content::Code(c) => c.source.len(),
            Content::Math(m) => m.expression.len(),
            Content::Media(m) => match &m.source {
                MediaSource::Base64(s) => s.len(),
                MediaSource::Url(s) => s.len(),
                _ => 32,
            },
            Content::Json { value, .. } => value.to_string().len(),
            Content::Binary { data, .. } => data.len(),
            Content::Composite { children, .. } => children.len() * 12,
        }
    }
}

/// Text content
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Text {
    pub text: String,
    #[serde(default)]
    pub format: TextFormat,
}

/// Text format
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TextFormat {
    #[default]
    Plain,
    Markdown,
    Rich,
}

/// Table content
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Table {
    pub columns: Vec<Column>,
    pub rows: Vec<Row>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub schema: Option<TableSchema>,
}

impl Table {
    pub fn new(columns: Vec<Column>) -> Self {
        Self {
            columns,
            rows: Vec::new(),
            schema: None,
        }
    }

    pub fn with_rows(mut self, rows: Vec<Row>) -> Self {
        self.rows = rows;
        self
    }

    pub fn add_row(&mut self, row: Row) {
        self.rows.push(row);
    }

    pub fn column_count(&self) -> usize {
        self.columns.len()
    }

    pub fn row_count(&self) -> usize {
        self.rows.len()
    }
}

/// Table column definition
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Column {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_type: Option<DataType>,
    #[serde(default = "default_true")]
    pub nullable: bool,
}

fn default_true() -> bool {
    true
}

impl Column {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            data_type: None,
            nullable: true,
        }
    }

    pub fn with_type(mut self, data_type: DataType) -> Self {
        self.data_type = Some(data_type);
        self
    }

    pub fn not_null(mut self) -> Self {
        self.nullable = false;
        self
    }
}

/// Data type for table columns
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DataType {
    Text,
    Integer,
    Float,
    Boolean,
    Date,
    DateTime,
    Json,
}

/// Table row
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Row {
    pub cells: Vec<Cell>,
}

impl Row {
    pub fn new(cells: Vec<Cell>) -> Self {
        Self { cells }
    }

    pub fn from_strings(values: Vec<&str>) -> Self {
        Self {
            cells: values
                .into_iter()
                .map(|s| Cell::Text(s.to_string()))
                .collect(),
        }
    }
}

/// Table cell value
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Cell {
    Null,
    Text(String),
    Number(f64),
    Boolean(bool),
    Date(String),     // ISO 8601 date
    DateTime(String), // ISO 8601 datetime
    Json(serde_json::Value),
}

impl Cell {
    pub fn size_bytes(&self) -> usize {
        match self {
            Cell::Null => 0,
            Cell::Text(s) => s.len(),
            Cell::Number(_) => 8,
            Cell::Boolean(_) => 1,
            Cell::Date(s) => s.len(),
            Cell::DateTime(s) => s.len(),
            Cell::Json(v) => v.to_string().len(),
        }
    }

    pub fn is_null(&self) -> bool {
        matches!(self, Cell::Null)
    }
}

impl From<&str> for Cell {
    fn from(s: &str) -> Self {
        Cell::Text(s.to_string())
    }
}

impl From<String> for Cell {
    fn from(s: String) -> Self {
        Cell::Text(s)
    }
}

impl From<i64> for Cell {
    fn from(n: i64) -> Self {
        Cell::Number(n as f64)
    }
}

impl From<f64> for Cell {
    fn from(n: f64) -> Self {
        Cell::Number(n)
    }
}

impl From<bool> for Cell {
    fn from(b: bool) -> Self {
        Cell::Boolean(b)
    }
}

/// Table schema
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TableSchema {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub primary_key: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub constraints: Vec<Constraint>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub indices: Vec<IndexDef>,
}

/// Table constraint
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Constraint {
    Unique {
        columns: Vec<String>,
    },
    Check {
        expression: String,
    },
    ForeignKey {
        columns: Vec<String>,
        references: String,
    },
}

/// Index definition
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IndexDef {
    pub name: String,
    pub columns: Vec<String>,
    #[serde(default)]
    pub unique: bool,
}

/// Code content
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Code {
    pub language: String,
    pub source: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub highlights: Vec<LineRange>,
}

impl Code {
    pub fn new(language: impl Into<String>, source: impl Into<String>) -> Self {
        Self {
            language: language.into(),
            source: source.into(),
            highlights: Vec::new(),
        }
    }

    pub fn line_count(&self) -> usize {
        self.source.lines().count()
    }

    pub fn get_lines(&self, start: usize, end: usize) -> Option<String> {
        let lines: Vec<&str> = self.source.lines().collect();
        if start > 0 && end <= lines.len() && start <= end {
            Some(lines[start - 1..end].join("\n"))
        } else {
            None
        }
    }
}

/// Line range for code highlights
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct LineRange {
    pub start: usize,
    pub end: usize,
}

impl LineRange {
    pub fn new(start: usize, end: usize) -> Self {
        Self { start, end }
    }

    pub fn single(line: usize) -> Self {
        Self {
            start: line,
            end: line,
        }
    }
}

/// Math content
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Math {
    pub format: MathFormat,
    pub expression: String,
    #[serde(default)]
    pub display_mode: bool,
}

impl Math {
    pub fn latex(expression: impl Into<String>) -> Self {
        Self {
            format: MathFormat::LaTeX,
            expression: expression.into(),
            display_mode: false,
        }
    }

    pub fn display(mut self) -> Self {
        self.display_mode = true;
        self
    }
}

/// Math format
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum MathFormat {
    #[default]
    LaTeX,
    MathML,
    AsciiMath,
}

/// Media content
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Media {
    pub media_type: MediaType,
    pub source: MediaSource,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alt_text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dimensions: Option<Dimensions>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_hash: Option<[u8; 32]>,
}

impl Media {
    pub fn image(source: MediaSource) -> Self {
        Self {
            media_type: MediaType::Image,
            source,
            alt_text: None,
            dimensions: None,
            content_hash: None,
        }
    }

    pub fn with_alt(mut self, alt: impl Into<String>) -> Self {
        self.alt_text = Some(alt.into());
        self
    }

    pub fn with_dimensions(mut self, width: u32, height: u32) -> Self {
        self.dimensions = Some(Dimensions { width, height });
        self
    }
}

/// Media type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MediaType {
    Image,
    Audio,
    Video,
    Document,
}

/// Media source
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", rename_all = "lowercase")]
pub enum MediaSource {
    Url(String),
    Base64(String),
    Reference(BlockId),
    External(ExternalRef),
}

impl MediaSource {
    pub fn url(url: impl Into<String>) -> Self {
        MediaSource::Url(url.into())
    }
}

/// External storage reference
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExternalRef {
    pub provider: String,
    pub bucket: String,
    pub key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
}

/// Dimensions for media
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Dimensions {
    pub width: u32,
    pub height: u32,
}

/// JSON schema reference
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsonSchema {
    Uri(String),
    Inline(serde_json::Value),
}

/// Binary encoding
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BinaryEncoding {
    #[default]
    Raw,
    Base64,
    Hex,
}

/// Composite layout
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CompositeLayout {
    #[default]
    Vertical,
    Horizontal,
    Grid(usize),
    Tabs,
}

// Base64 serde helper
mod base64_serde {
    use base64::{engine::general_purpose::STANDARD, Engine as _};
    use serde::{self, Deserialize, Deserializer, Serializer};

    pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&STANDARD.encode(bytes))
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        STANDARD.decode(&s).map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_content_type_tag() {
        assert_eq!(Content::text("hello").type_tag(), "text");
        assert_eq!(Content::code("rust", "fn main() {}").type_tag(), "code");
        assert_eq!(Content::json(serde_json::json!({})).type_tag(), "json");
    }

    #[test]
    fn test_text_content() {
        let content = Content::text("Hello, world!");
        match content {
            Content::Text(t) => {
                assert_eq!(t.text, "Hello, world!");
                assert_eq!(t.format, TextFormat::Plain);
            }
            _ => panic!("Expected Text content"),
        }
    }

    #[test]
    fn test_table_content() {
        let mut table = Table::new(vec![
            Column::new("name").with_type(DataType::Text),
            Column::new("age").with_type(DataType::Integer),
        ]);
        table.add_row(Row::new(vec![Cell::from("Alice"), Cell::from(30i64)]));

        assert_eq!(table.column_count(), 2);
        assert_eq!(table.row_count(), 1);
    }

    #[test]
    fn test_code_lines() {
        let code = Code::new("rust", "line1\nline2\nline3\nline4");
        assert_eq!(code.line_count(), 4);
        assert_eq!(code.get_lines(2, 3), Some("line2\nline3".to_string()));
    }

    #[test]
    fn test_content_serialization() {
        let content = Content::text("Hello");
        let json = serde_json::to_string(&content).unwrap();
        let parsed: Content = serde_json::from_str(&json).unwrap();
        assert_eq!(content, parsed);
    }
}