Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interleaved text #263

Closed
chrsan opened this issue Feb 12, 2021 · 1 comment
Closed

Interleaved text #263

chrsan opened this issue Feb 12, 2021 · 1 comment
Labels
enhancement serde Issues related to mapping from Rust types to XML

Comments

@chrsan
Copy link

chrsan commented Feb 12, 2021

Here's a quick fix for supporting interleaved text (see #257).

diff --git a/src/de/var.rs b/src/de/var.rs
index 781c7d9..c05fefe 100644
--- a/src/de/var.rs
+++ b/src/de/var.rs
@@ -17,6 +17,8 @@ impl<'a, R: BufRead> EnumAccess<'a, R> {
     }
 }

+const TEXT_VARIANT: &[u8] = b"$text";
+
 impl<'de, 'a, R: 'a + BufRead> de::EnumAccess<'de> for EnumAccess<'a, R> {
     type Error = DeError;
     type Variant = VariantAccess<'a, R>;
@@ -27,7 +29,7 @@ impl<'de, 'a, R: 'a + BufRead> de::EnumAccess<'de> for EnumAccess<'a, R> {
     ) -> Result<(V::Value, VariantAccess<'a, R>), DeError> {
         let decoder = self.de.reader.decoder();
         let de = match self.de.peek()? {
-            Some(Event::Text(t)) => EscapedDeserializer::new(t.to_vec(), decoder, true),
+            Some(Event::Text(_)) => EscapedDeserializer::new(TEXT_VARIANT.to_vec(), decoder, false),
             Some(Event::Start(e)) => EscapedDeserializer::new(e.name().to_vec(), decoder, false),
             Some(e) => return Err(DeError::InvalidEnum(e.to_owned())),
             None => return Err(DeError::Eof),

Let's say I have the following TTML where the Text variant represents interleaved text:

#[derive(Debug, Deserialize)]
struct Div {
    #[serde(rename = "p")]
    pub paragraphs: Vec<Paragraph>,
}

#[derive(Debug, Deserialize)]
struct Paragraph {
    #[serde(rename = "$value")]
    pub content: Vec<Content>,
}

#[derive(Debug, Deserialize)]
enum Content {
    #[serde(rename = "br")]
    Br,
    #[serde(rename = "span")]
    Span {
        #[serde(rename = "tts:fontWeight")]
        font_weight: Option<String>,
        #[serde(rename = "tts:fontStyle")]
        font_style: Option<String>,
        #[serde(rename = "$value")]
        text: String,
    },
    #[serde(rename = "$text")]
    Text(String),
}
@Mingun Mingun added enhancement serde Issues related to mapping from Rust types to XML labels May 21, 2022
@Mingun
Copy link
Collaborator

Mingun commented May 25, 2022

Thanks for the sharing your code, I'll investigate it in its time.
Closed as duplicate of #257

@Mingun Mingun closed this as not planned Won't fix, can't repro, duplicate, stale May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants