Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Implementing your own custom parser

The ComponentParser and ComponentSerializer traits are designed for extensibility. In case you need to support a different markup format (e.g., a custom Markdown-like syntax, or integration with another rich text system), these traits come in handy.

At a high level:

  • The ComponentParser trait defines how to convert a string representation of a text component into a Component enum. Its primary method is

    #![allow(unused)]
    fn main() {
    fn from_string(input: impl AsRef<str>) -> Result<Component, Self::Err>;
    }
  • The ComponentSerializer trait defines how to convert a Component enum back into a string representation. Its primary method is:

    #![allow(unused)]
    fn main() {
    fn to_string(component: &Component) -> Result<String, Self::Err>;
    }

The input type in from_string is deliberately generic to allow you to provide multiple types, such as:

  • &str
  • String
  • &String
  • Box<str>
  • Cow<'_, str> without restrictions.
  1. Define your parser/serializer struct: Create a new struct (like MyCustomParser).
  2. Implement ComponentParser:
    • Implement the from_string method, which will contain your logic to parse the input string and construct a Component object. This often involves tokenizing the input, managing a style stack, and building Components based on your format’s rules.
  3. Implement ComponentSerializer (optional):
    • Implement the to_string method, which will traverse a Component object and convert it into your desired string format.

Both traits have an Err associated type for your serialization errors. You have to define one, but it can be any type, including () as a placeholder.

The minimessage.rs source file is an excellent reference for how to implement these traits. By following this pattern, you can integrate kyori-component-json with virtually any rich text input or output format.

You can find more about these traits on docs.rs: