@@ -33,13 +33,14 @@ enum ListItemType {
3333#[ derive( Clone , Debug , PartialEq ) ]
3434pub struct StyledTextParagraph {
3535 /// The raw paragraph text
36- pub text : std :: string:: String ,
36+ pub text : alloc :: string:: String ,
3737 /// Formatting styles and spans
38- pub formatting : std :: vec:: Vec < FormattedSpan > ,
38+ pub formatting : alloc :: vec:: Vec < FormattedSpan > ,
3939 /// Locations of clickable links within the paragraph
40- pub links : std :: vec:: Vec < ( std :: ops:: Range < usize > , std :: string:: String ) > ,
40+ pub links : alloc :: vec:: Vec < ( core :: ops:: Range < usize > , alloc :: string:: String ) > ,
4141}
4242
43+ #[ cfg( feature = "std" ) ]
4344#[ derive( Debug , thiserror:: Error ) ]
4445#[ allow( missing_docs) ]
4546#[ non_exhaustive]
@@ -55,27 +56,27 @@ pub enum StyledTextError<'a> {
5556 #[ error( "Unimplemented: {:?}" , . 0 ) ]
5657 UnimplementedEvent ( pulldown_cmark:: Event < ' a > ) ,
5758 #[ error( "Unimplemented: {}" , . 0 ) ]
58- UnimplementedHtmlEvent ( std :: string:: String ) ,
59+ UnimplementedHtmlEvent ( alloc :: string:: String ) ,
5960 #[ error( "Unimplemented html tag: {}" , . 0 ) ]
60- UnimplementedHtmlTag ( std :: string:: String ) ,
61+ UnimplementedHtmlTag ( alloc :: string:: String ) ,
6162 #[ error( "Unexpected {} attribute in html {}" , . 0 , . 1 ) ]
62- UnexpectedAttribute ( std :: string:: String , std :: string:: String ) ,
63+ UnexpectedAttribute ( alloc :: string:: String , alloc :: string:: String ) ,
6364 #[ error( "Missing color attribute in html {}" , . 0 ) ]
64- MissingColor ( std :: string:: String ) ,
65+ MissingColor ( alloc :: string:: String ) ,
6566 #[ error( "Closing html tag doesn't match the opening tag. Expected {}, got {}" , . 0 , . 1 ) ]
66- ClosingTagMismatch ( & ' a str , std :: string:: String ) ,
67+ ClosingTagMismatch ( & ' a str , alloc :: string:: String ) ,
6768}
6869
6970/// Internal styled text type
7071#[ derive( Debug , PartialEq , Clone , Default ) ]
7172pub struct StyledText {
7273 /// Paragraphs of styled text
73- pub paragraphs : std :: vec:: Vec < StyledTextParagraph > ,
74+ pub paragraphs : alloc :: vec:: Vec < StyledTextParagraph > ,
7475}
7576
7677impl StyledText {
7778 fn begin_paragraph ( & mut self , indentation : u32 , list_item_type : Option < ListItemType > ) {
78- let mut text = std :: string:: String :: with_capacity ( indentation as usize * 4 ) ;
79+ let mut text = alloc :: string:: String :: with_capacity ( indentation as usize * 4 ) ;
7980 for _ in 0 ..indentation {
8081 text. push_str ( " " ) ;
8182 }
@@ -89,7 +90,7 @@ impl StyledText {
8990 text. push_str ( "▪ " )
9091 }
9192 }
92- Some ( ListItemType :: Ordered ( num) ) => text. push_str ( & std :: format!( "{}. " , num) ) ,
93+ Some ( ListItemType :: Ordered ( num) ) => text. push_str ( & alloc :: format!( "{}. " , num) ) ,
9394 None => { }
9495 } ;
9596 self . paragraphs . push ( StyledTextParagraph {
@@ -100,13 +101,14 @@ impl StyledText {
100101 }
101102
102103 /// Parse a markdown string as styled text
104+ #[ cfg( feature = "std" ) ]
103105 pub fn parse ( string : & str ) -> Result < Self , StyledTextError < ' _ > > {
104106 let parser =
105107 pulldown_cmark:: Parser :: new_ext ( string, pulldown_cmark:: Options :: ENABLE_STRIKETHROUGH ) ;
106108
107109 let mut styled_text = StyledText :: default ( ) ;
108- let mut list_state_stack: std :: vec:: Vec < Option < u64 > > = std :: vec:: Vec :: new ( ) ;
109- let mut style_stack = std :: vec:: Vec :: new ( ) ;
110+ let mut list_state_stack: alloc :: vec:: Vec < Option < u64 > > = alloc :: vec:: Vec :: new ( ) ;
111+ let mut style_stack = alloc :: vec:: Vec :: new ( ) ;
110112 let mut current_url = None ;
111113
112114 for event in parser {
@@ -328,7 +330,7 @@ impl StyledText {
328330 Ok ( htmlparser:: Token :: ElementEnd { .. } ) => { }
329331 _ => {
330332 return Err ( StyledTextError :: UnimplementedHtmlEvent (
331- std :: format!( "{:?}" , token) ,
333+ alloc :: format!( "{:?}" , token) ,
332334 ) ) ;
333335 }
334336 }
@@ -364,8 +366,8 @@ fn markdown_parsing() {
364366 StyledText :: parse( "hello *world*" ) . unwrap( ) . paragraphs,
365367 [ StyledTextParagraph {
366368 text: "hello world" . into( ) ,
367- formatting: std :: vec![ FormattedSpan { range: 6 ..11 , style: Style :: Emphasis } ] ,
368- links: std :: vec![ ]
369+ formatting: alloc :: vec![ FormattedSpan { range: 6 ..11 , style: Style :: Emphasis } ] ,
370+ links: alloc :: vec![ ]
369371 } ]
370372 ) ;
371373
@@ -381,13 +383,13 @@ fn markdown_parsing() {
381383 [
382384 StyledTextParagraph {
383385 text: "• line 1" . into( ) ,
384- formatting: std :: vec![ ] ,
385- links: std :: vec![ ]
386+ formatting: alloc :: vec![ ] ,
387+ links: alloc :: vec![ ]
386388 } ,
387389 StyledTextParagraph {
388390 text: "• line 2" . into( ) ,
389- formatting: std :: vec![ ] ,
390- links: std :: vec![ ]
391+ formatting: alloc :: vec![ ] ,
392+ links: alloc :: vec![ ]
391393 }
392394 ]
393395 ) ;
@@ -405,18 +407,18 @@ fn markdown_parsing() {
405407 [
406408 StyledTextParagraph {
407409 text: "1. a" . into( ) ,
408- formatting: std :: vec![ ] ,
409- links: std :: vec![ ]
410+ formatting: alloc :: vec![ ] ,
411+ links: alloc :: vec![ ]
410412 } ,
411413 StyledTextParagraph {
412414 text: "2. b" . into( ) ,
413- formatting: std :: vec![ ] ,
414- links: std :: vec![ ]
415+ formatting: alloc :: vec![ ] ,
416+ links: alloc :: vec![ ]
415417 } ,
416418 StyledTextParagraph {
417419 text: "3. c" . into( ) ,
418- formatting: std :: vec![ ] ,
419- links: std :: vec![ ]
420+ formatting: alloc :: vec![ ] ,
421+ links: alloc :: vec![ ]
420422 }
421423 ]
422424 ) ;
@@ -433,18 +435,18 @@ new *line*
433435 [
434436 StyledTextParagraph {
435437 text: "Normal italic strong strikethrough code" . into( ) ,
436- formatting: std :: vec![
438+ formatting: alloc :: vec![
437439 FormattedSpan { range: 7 ..13 , style: Style :: Emphasis } ,
438440 FormattedSpan { range: 14 ..20 , style: Style :: Strong } ,
439441 FormattedSpan { range: 21 ..34 , style: Style :: Strikethrough } ,
440442 FormattedSpan { range: 35 ..39 , style: Style :: Code }
441443 ] ,
442- links: std :: vec![ ]
444+ links: alloc :: vec![ ]
443445 } ,
444446 StyledTextParagraph {
445447 text: "new line" . into( ) ,
446- formatting: std :: vec![ FormattedSpan { range: 4 ..8 , style: Style :: Emphasis } , ] ,
447- links: std :: vec![ ]
448+ formatting: alloc :: vec![ FormattedSpan { range: 4 ..8 , style: Style :: Emphasis } , ] ,
449+ links: alloc :: vec![ ]
448450 }
449451 ]
450452 ) ;
@@ -463,23 +465,23 @@ new *line*
463465 [
464466 StyledTextParagraph {
465467 text: "• root" . into( ) ,
466- formatting: std :: vec![ ] ,
467- links: std :: vec![ ]
468+ formatting: alloc :: vec![ ] ,
469+ links: alloc :: vec![ ]
468470 } ,
469471 StyledTextParagraph {
470472 text: " ◦ child" . into( ) ,
471- formatting: std :: vec![ ] ,
472- links: std :: vec![ ]
473+ formatting: alloc :: vec![ ] ,
474+ links: alloc :: vec![ ]
473475 } ,
474476 StyledTextParagraph {
475477 text: " ▪ grandchild" . into( ) ,
476- formatting: std :: vec![ ] ,
477- links: std :: vec![ ]
478+ formatting: alloc :: vec![ ] ,
479+ links: alloc :: vec![ ]
478480 } ,
479481 StyledTextParagraph {
480482 text: " • great grandchild" . into( ) ,
481- formatting: std :: vec![ ] ,
482- links: std :: vec![ ]
483+ formatting: alloc :: vec![ ] ,
484+ links: alloc :: vec![ ]
483485 } ,
484486 ]
485487 ) ;
@@ -488,47 +490,47 @@ new *line*
488490 StyledText :: parse( "hello [*world*](https://example.com)" ) . unwrap( ) . paragraphs,
489491 [ StyledTextParagraph {
490492 text: "hello world" . into( ) ,
491- formatting: std :: vec![
493+ formatting: alloc :: vec![
492494 FormattedSpan { range: 6 ..11 , style: Style :: Emphasis } ,
493495 FormattedSpan { range: 6 ..11 , style: Style :: Link }
494496 ] ,
495- links: std :: vec![ ( 6 ..11 , "https://example.com" . into( ) ) ]
497+ links: alloc :: vec![ ( 6 ..11 , "https://example.com" . into( ) ) ]
496498 } ]
497499 ) ;
498500
499501 assert_eq ! (
500502 StyledText :: parse( "<u>hello world</u>" ) . unwrap( ) . paragraphs,
501503 [ StyledTextParagraph {
502504 text: "hello world" . into( ) ,
503- formatting: std :: vec![ FormattedSpan { range: 0 ..11 , style: Style :: Underline } , ] ,
504- links: std :: vec![ ]
505+ formatting: alloc :: vec![ FormattedSpan { range: 0 ..11 , style: Style :: Underline } , ] ,
506+ links: alloc :: vec![ ]
505507 } ]
506508 ) ;
507509
508510 assert_eq ! (
509511 StyledText :: parse( r#"<font color="blue">hello world</font>"# ) . unwrap( ) . paragraphs,
510512 [ StyledTextParagraph {
511513 text: "hello world" . into( ) ,
512- formatting: std :: vec![ FormattedSpan {
514+ formatting: alloc :: vec![ FormattedSpan {
513515 range: 0 ..11 ,
514516 style: Style :: Color ( crate :: Color :: from_rgb_u8( 0 , 0 , 255 ) )
515517 } , ] ,
516- links: std :: vec![ ]
518+ links: alloc :: vec![ ]
517519 } ]
518520 ) ;
519521
520522 assert_eq ! (
521523 StyledText :: parse( r#"<u><font color="red">hello world</font></u>"# ) . unwrap( ) . paragraphs,
522524 [ StyledTextParagraph {
523525 text: "hello world" . into( ) ,
524- formatting: std :: vec![
526+ formatting: alloc :: vec![
525527 FormattedSpan {
526528 range: 0 ..11 ,
527529 style: Style :: Color ( crate :: Color :: from_rgb_u8( 255 , 0 , 0 ) )
528530 } ,
529531 FormattedSpan { range: 0 ..11 , style: Style :: Underline } ,
530532 ] ,
531- links: std :: vec![ ]
533+ links: alloc :: vec![ ]
532534 } ]
533535 ) ;
534536}
0 commit comments