lark-channel 0.5.0

Lark/Feishu Channel SDK for Rust
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use std::collections::BTreeMap;

use serde::{Deserialize, Deserializer, Serialize, de::Error as _};
use url::Url;

use crate::{Error, Result};

const DEFAULT_POST_LOCALE: &str = "zh_cn";
const ENGLISH_POST_LOCALE: &str = "en_us";

/// Lark/Feishu rich-text `post` message content.
///
/// The serialized shape is a locale map such as
/// `{ "zh_cn": { "title": "", "content": [[...]] } }`.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(transparent)]
pub struct PostContent {
    locales: BTreeMap<String, PostDocument>,
}

impl<'de> Deserialize<'de> for PostContent {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let content = Self {
            locales: BTreeMap::deserialize(deserializer)?,
        };
        content.validate().map_err(D::Error::custom)?;
        Ok(content)
    }
}

impl PostContent {
    /// Starts a rich-text builder using the default `zh_cn` locale.
    pub fn builder() -> PostContentBuilder {
        PostContentBuilder::new()
    }

    /// Wraps Markdown in the native Lark/Feishu `md` post element.
    pub fn markdown(markdown: impl Into<String>) -> Self {
        Self::from_default_document(PostDocument::new().markdown(markdown))
    }

    /// Wraps plain text in a structured Lark/Feishu `text` post element.
    pub fn text(text: impl Into<String>) -> Self {
        Self::from_default_document(PostDocument::new().paragraph([PostElement::text(text)]))
    }

    /// Creates validated content for the default `zh_cn` locale.
    pub fn new(document: PostDocument) -> Result<Self> {
        Self::for_locale(DEFAULT_POST_LOCALE, document)
    }

    /// Creates content for the documented `zh_cn` or `en_us` locale.
    pub fn for_locale(locale: impl Into<String>, document: PostDocument) -> Result<Self> {
        let locale = locale.into();
        validate_locale_and_document(&locale, &document).map(|locale| {
            let mut locales = BTreeMap::new();
            locales.insert(locale, document);
            Self { locales }
        })
    }

    /// Adds or replaces a documented `zh_cn` or `en_us` localized document.
    pub fn insert_locale(
        &mut self,
        locale: impl Into<String>,
        document: PostDocument,
    ) -> Result<Option<PostDocument>> {
        let locale = locale.into();
        let locale = validate_locale_and_document(&locale, &document)?;
        Ok(self.locales.insert(locale, document))
    }

    /// Returns all localized documents in deterministic key order.
    pub fn locales(&self) -> &BTreeMap<String, PostDocument> {
        &self.locales
    }

    /// Returns one localized document when present.
    pub fn document(&self, locale: &str) -> Option<&PostDocument> {
        self.locales.get(locale)
    }

    pub(crate) fn validate(&self) -> Result<()> {
        if self.locales.is_empty() {
            return Err(Error::Validation(
                "post content must contain at least one locale".to_owned(),
            ));
        }
        for (locale, document) in &self.locales {
            validate_locale_and_document(locale, document)?;
        }
        Ok(())
    }

    fn from_default_document(document: PostDocument) -> Self {
        let mut locales = BTreeMap::new();
        locales.insert(DEFAULT_POST_LOCALE.to_owned(), document);
        Self { locales }
    }
}

/// One localized rich-text document inside a `post` message.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct PostDocument {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    title: String,
    content: Vec<Vec<PostElement>>,
}

impl PostDocument {
    /// Creates an empty document.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the optional rich-text title.
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = title.into();
        self
    }

    /// Appends a paragraph when it contains at least one element.
    pub fn paragraph(mut self, elements: impl IntoIterator<Item = PostElement>) -> Self {
        let elements = elements.into_iter().collect::<Vec<_>>();
        if !elements.is_empty() {
            self.content.push(elements);
        }
        self
    }

    /// Appends a native Markdown paragraph.
    ///
    /// Lark/Feishu requires an `md` element to occupy its own paragraph.
    pub fn markdown(self, markdown: impl Into<String>) -> Self {
        self.paragraph([PostElement::markdown(markdown)])
    }

    /// Returns the optional title as a string slice.
    pub fn title(&self) -> &str {
        &self.title
    }

    /// Returns the post paragraphs.
    pub fn paragraphs(&self) -> &[Vec<PostElement>] {
        &self.content
    }

    fn is_empty(&self) -> bool {
        self.content.is_empty()
    }
}

/// Builder for a single-locale rich-text `post` message.
#[derive(Debug, Clone)]
pub struct PostContentBuilder {
    locale: String,
    document: PostDocument,
}

impl Default for PostContentBuilder {
    fn default() -> Self {
        Self {
            locale: DEFAULT_POST_LOCALE.to_owned(),
            document: PostDocument::new(),
        }
    }
}

impl PostContentBuilder {
    /// Creates a builder using the default `zh_cn` locale.
    pub fn new() -> Self {
        Self::default()
    }

    /// Selects the documented `zh_cn` or `en_us` locale key.
    pub fn locale(mut self, locale: impl Into<String>) -> Self {
        self.locale = locale.into();
        self
    }

    /// Sets the optional rich-text title.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.document = self.document.with_title(title);
        self
    }

    /// Appends a structured rich-text paragraph.
    pub fn paragraph(mut self, elements: impl IntoIterator<Item = PostElement>) -> Self {
        self.document = self.document.paragraph(elements);
        self
    }

    /// Appends a native Markdown paragraph.
    pub fn markdown(mut self, markdown: impl Into<String>) -> Self {
        self.document = self.document.markdown(markdown);
        self
    }

    /// Appends a structured plain-text paragraph.
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.document = self.document.paragraph([PostElement::text(text)]);
        self
    }

    /// Builds validated rich-text content.
    pub fn build(self) -> Result<PostContent> {
        PostContent::for_locale(self.locale, self.document)
    }
}

/// A supported element in a structured rich-text paragraph.
///
/// Use `MessageContent::Custom` for official post elements that are not yet
/// modeled by these Channel helpers.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(transparent)]
pub struct PostElement(PostElementKind);

impl<'de> Deserialize<'de> for PostElement {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let element = Self(PostElementKind::deserialize(deserializer)?);
        element.validate().map_err(D::Error::custom)?;
        Ok(element)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "tag", deny_unknown_fields)]
enum PostElementKind {
    #[serde(rename = "text")]
    Text {
        text: String,
        #[serde(
            default,
            skip_serializing_if = "Option::is_none",
            deserialize_with = "deserialize_optional_bool"
        )]
        un_escape: Option<bool>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        style: Vec<PostStyle>,
    },
    #[serde(rename = "a")]
    Link {
        text: String,
        href: String,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        style: Vec<PostStyle>,
    },
    #[serde(rename = "at")]
    Mention {
        user_id: String,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        style: Vec<PostStyle>,
    },
    #[serde(rename = "md")]
    Markdown { text: String },
}

impl PostElement {
    /// Creates a plain text element.
    pub fn text(text: impl Into<String>) -> Self {
        Self(PostElementKind::Text {
            text: text.into(),
            un_escape: None,
            style: Vec::new(),
        })
    }

    /// Creates a validated hyperlink element.
    pub fn link(text: impl Into<String>, href: impl Into<String>) -> Result<Self> {
        let href = href.into();
        Url::parse(&href)?;
        Ok(Self(PostElementKind::Link {
            text: text.into(),
            href,
            style: Vec::new(),
        }))
    }

    /// Creates an @ mention for an open_id, user_id, or union_id.
    pub fn mention(user_id: impl Into<String>) -> Result<Self> {
        let user_id = validate_mention_id(user_id.into())?;
        Ok(Self(PostElementKind::Mention {
            user_id,
            style: Vec::new(),
        }))
    }

    /// Creates an @all mention.
    pub fn mention_all() -> Self {
        Self(PostElementKind::Mention {
            user_id: "all".to_owned(),
            style: Vec::new(),
        })
    }

    /// Creates a native Markdown element.
    pub fn markdown(markdown: impl Into<String>) -> Self {
        Self(PostElementKind::Markdown {
            text: markdown.into(),
        })
    }

    /// Sets the official `un_escape` flag on a plain text element.
    pub fn with_unescape(mut self, unescape: bool) -> Result<Self> {
        match &mut self.0 {
            PostElementKind::Text {
                un_escape: value, ..
            } => *value = Some(unescape),
            _ => {
                return Err(Error::Validation(
                    "post un_escape is only supported for text elements".to_owned(),
                ));
            }
        }
        Ok(self)
    }

    /// Applies supported text styles to text, link, or mention elements.
    pub fn with_styles(mut self, styles: impl IntoIterator<Item = PostStyle>) -> Result<Self> {
        let styles = styles.into_iter().collect::<Vec<_>>();
        let target = match &mut self.0 {
            PostElementKind::Text { style, .. }
            | PostElementKind::Link { style, .. }
            | PostElementKind::Mention { style, .. } => style,
            PostElementKind::Markdown { .. } => {
                return Err(Error::Validation(
                    "post styles are only supported for text, link, and mention elements"
                        .to_owned(),
                ));
            }
        };
        if !styles.is_empty() {
            *target = styles;
        }
        Ok(self)
    }

    fn validate(&self) -> Result<&'static str> {
        match &self.0 {
            PostElementKind::Text { .. } => Ok("text"),
            PostElementKind::Link { href, .. } => {
                Url::parse(href)?;
                Ok("a")
            }
            PostElementKind::Mention { user_id, .. } => {
                validate_mention_id(user_id.clone())?;
                Ok("at")
            }
            PostElementKind::Markdown { .. } => Ok("md"),
        }
    }
}

/// Text styles supported by Lark/Feishu structured post elements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PostStyle {
    #[serde(rename = "bold")]
    Bold,
    #[serde(rename = "underline")]
    Underline,
    #[serde(rename = "lineThrough")]
    LineThrough,
    #[serde(rename = "italic")]
    Italic,
}

fn validate_locale_and_document(locale: &str, document: &PostDocument) -> Result<String> {
    if !matches!(locale, DEFAULT_POST_LOCALE | ENGLISH_POST_LOCALE) {
        return Err(Error::Validation(
            "post locale must be `zh_cn` or `en_us`".to_owned(),
        ));
    }
    if document.is_empty() {
        return Err(Error::Validation(
            "post content must contain at least one paragraph".to_owned(),
        ));
    }
    for paragraph in &document.content {
        if paragraph.is_empty() {
            return Err(Error::Validation(
                "post paragraphs must contain at least one element".to_owned(),
            ));
        }
        let mut contains_markdown = false;
        for element in paragraph {
            contains_markdown |= element.validate()? == "md";
        }
        if contains_markdown && paragraph.len() != 1 {
            return Err(Error::Validation(
                "post markdown elements must occupy their own paragraph".to_owned(),
            ));
        }
    }
    Ok(locale.to_owned())
}

fn validate_mention_id(user_id: String) -> Result<String> {
    if user_id.trim().is_empty() {
        return Err(Error::Validation(
            "post mention user id must not be empty".to_owned(),
        ));
    }
    if user_id.chars().any(char::is_whitespace) {
        return Err(Error::Validation(
            "post mention user id must not contain whitespace".to_owned(),
        ));
    }
    Ok(user_id)
}

fn deserialize_optional_bool<'de, D>(deserializer: D) -> std::result::Result<Option<bool>, D::Error>
where
    D: Deserializer<'de>,
{
    bool::deserialize(deserializer).map(Some)
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    #[test]
    fn markdown_uses_native_md_post_shape() {
        let post = PostContent::markdown("# Hello\n\n[docs](https://open.feishu.cn)");

        assert_eq!(
            serde_json::to_value(post).expect("post JSON"),
            json!({
                "zh_cn": {
                    "content": [[{
                        "tag": "md",
                        "text": "# Hello\n\n[docs](https://open.feishu.cn)"
                    }]]
                }
            })
        );
    }

    #[test]
    fn builder_creates_structured_mentions_links_and_styles() {
        let post = PostContent::builder()
            .locale("en_us")
            .title("Status")
            .paragraph([
                PostElement::mention("ou_alice").expect("mention"),
                PostElement::text(" read "),
                PostElement::link("the docs", "https://open.feishu.cn")
                    .expect("link")
                    .with_styles([PostStyle::Bold, PostStyle::Underline])
                    .expect("styles"),
                PostElement::mention_all(),
            ])
            .build()
            .expect("post content");

        assert_eq!(
            serde_json::to_value(post).expect("post JSON"),
            json!({
                "en_us": {
                    "title": "Status",
                    "content": [[
                        { "tag": "at", "user_id": "ou_alice" },
                        { "tag": "text", "text": " read " },
                        {
                            "tag": "a",
                            "text": "the docs",
                            "href": "https://open.feishu.cn",
                            "style": ["bold", "underline"]
                        },
                        { "tag": "at", "user_id": "all" }
                    ]]
                }
            })
        );
    }

    #[test]
    fn rejects_invalid_link_mention_and_empty_document() {
        assert!(matches!(
            PostElement::link("docs", "not a URL"),
            Err(Error::Url(_))
        ));
        assert!(matches!(
            PostElement::mention("  "),
            Err(Error::Validation(message)) if message == "post mention user id must not be empty"
        ));
        assert!(matches!(
            PostContent::builder().build(),
            Err(Error::Validation(message))
                if message == "post content must contain at least one paragraph"
        ));
        assert!(matches!(
            PostContent::new(PostDocument::new()),
            Err(Error::Validation(message))
                if message == "post content must contain at least one paragraph"
        ));
        for locale in ["", "ja_jp", "ZH_CN", "fr-fr", " zh_cn "] {
            assert!(matches!(
                PostContent::builder().locale(locale).text("hello").build(),
                Err(Error::Validation(message))
                    if message == "post locale must be `zh_cn` or `en_us`"
            ));
        }
    }

    #[test]
    fn rejects_markdown_mixed_with_structured_elements() {
        assert!(matches!(
            PostContent::new(PostDocument::new().paragraph([
                PostElement::markdown("**status**"),
                PostElement::text(" ready"),
            ])),
            Err(Error::Validation(message))
                if message == "post markdown elements must occupy their own paragraph"
        ));
    }

    #[test]
    fn deserialization_enforces_post_content_invariants() {
        let invalid_values = [
            json!({}),
            json!({ "": { "content": [[{ "tag": "text", "text": "hello" }]] } }),
            json!({ "ja_jp": { "content": [[{ "tag": "text", "text": "hello" }]] } }),
            json!({ "zh_cn": { "content": [] } }),
            json!({ "zh_cn": { "content": [[]] } }),
            json!({
                "zh_cn": {
                    "content": [[
                        { "tag": "md", "text": "**status**" },
                        { "tag": "text", "text": " ready" }
                    ]]
                }
            }),
            json!({
                "zh_cn": {
                    "content": [[{ "tag": "unsupported", "text": "hello" }]]
                }
            }),
            json!({ "zh_cn": { "content": [[{ "tag": "text" }]] } }),
            json!({
                "zh_cn": {
                    "content": [[{ "tag": "text", "text": "hello", "un_escape": "yes" }]]
                }
            }),
            json!({
                "zh_cn": {
                    "content": [[{ "tag": "text", "text": "hello", "un_escape": null }]]
                }
            }),
            json!({
                "zh_cn": {
                    "content": [[{ "tag": "a", "text": "docs", "href": "https://open.feishu.cn", "un_escape": true }]]
                }
            }),
            json!({
                "zh_cn": {
                    "content": [[{ "tag": "a", "text": "docs", "href": "not a URL" }]]
                }
            }),
            json!({
                "zh_cn": {
                    "content": [[{ "tag": "at", "user_id": "invalid id" }]]
                }
            }),
            json!({
                "zh_cn": {
                    "content": [[{ "tag": "md", "text": "**status**", "style": ["bold"] }]]
                }
            }),
        ];

        for value in invalid_values {
            assert!(serde_json::from_value::<PostContent>(value).is_err());
        }
    }

    #[test]
    fn valid_post_content_round_trips_through_json() {
        let post = PostContent::builder()
            .title("Status")
            .markdown("**ready**")
            .paragraph([
                PostElement::mention("ou_alice").expect("mention"),
                PostElement::text(" read ")
                    .with_unescape(true)
                    .expect("unescape"),
                PostElement::link("the docs", "https://open.feishu.cn")
                    .expect("link")
                    .with_styles([PostStyle::Italic])
                    .expect("styles"),
            ])
            .build()
            .expect("post content");

        let value = serde_json::to_value(&post).expect("serialize post content");
        assert_eq!(
            serde_json::from_value::<PostContent>(value).expect("deserialize post content"),
            post
        );
    }

    #[test]
    fn markdown_elements_reject_structured_styles() {
        assert!(matches!(
            PostElement::markdown("**bold**").with_styles([PostStyle::Bold]),
            Err(Error::Validation(message))
                if message.contains("only supported for text, link, and mention")
        ));
    }

    #[test]
    fn unescape_is_only_available_as_a_boolean_on_text_elements() {
        assert_eq!(
            serde_json::to_value(
                PostElement::text("hello&nbsp;world")
                    .with_unescape(true)
                    .expect("text unescape")
            )
            .expect("serialize text element"),
            json!({
                "tag": "text",
                "text": "hello&nbsp;world",
                "un_escape": true
            })
        );
        assert!(matches!(
            PostElement::markdown("hello").with_unescape(true),
            Err(Error::Validation(message))
                if message == "post un_escape is only supported for text elements"
        ));
    }

    #[test]
    fn standalone_element_deserialization_rejects_invalid_and_extra_fields() {
        for value in [
            json!({ "tag": "text", "text": "hello", "un_escape": "yes" }),
            json!({ "tag": "text", "text": "hello", "un_escape": null }),
            json!({ "tag": "a", "text": "docs", "href": "not a URL" }),
            json!({ "tag": "at", "user_id": "invalid id" }),
            json!({ "tag": "md", "text": "hello", "href": 123 }),
            json!({ "tag": "text", "text": "hello", "unknown": true }),
        ] {
            assert!(serde_json::from_value::<PostElement>(value).is_err());
        }
    }

    #[test]
    fn inserts_multiple_locales() {
        let mut post = PostContent::text("你好");
        post.insert_locale(
            "en_us",
            PostDocument::new().paragraph([PostElement::text("hello")]),
        )
        .expect("English locale");

        assert_eq!(post.locales().len(), 2);
        assert_eq!(
            post.document("en_us").expect("document").paragraphs().len(),
            1
        );

        assert!(matches!(
            post.insert_locale(
                "ja_jp",
                PostDocument::new().paragraph([PostElement::text("こんにちは")]),
            ),
            Err(Error::Validation(message))
                if message == "post locale must be `zh_cn` or `en_us`"
        ));
        assert_eq!(post.locales().len(), 2);
    }
}