Fluent (l10n system) parser/AST/serializer decision

Hi Rustaceans!

I’m the leading engineer behind fluent-rs - Rust implementation of the Fluent Localization System.

I’m working on final touches on the low level crate that operates on the FTL file format used by Fluent.
The AST is currently using &str over a source String which makes the AST read-only. The most critical use of the parser/AST is runtime where read-only is totally enough.
But it’s tempting to use the same AST/parser for tooling where ability to modify the AST and serialize it after is important.

I see several possible approaches to that, and I’m not sure how to weight pros and cons and if maybe I missed some other option.

Here’s the issue about it - https://github.com/projectfluent/fluent-rs/issues/104

I’d appreciate help with making the decision and any feedback from other crates that implemented parser/AST/serializer and had to make similar decisions.

We also have a couple more decisions to make prior to 1.0 release - https://github.com/projectfluent/fluent-rs/labels/fluent-syntax

Appreciate feedback and help!
zb.

1 Like

Using Cow<'_, str> seems fine to me, although I am not a user of fluent-rs :sweat_smile:.

But here is a neat trick that allows you to have both:

  • whenever your AST<'s> uses &'s str, you should be able to use some parametric type Str, where your struct is generic over
    <'s, Str : From<&'s str> + Clone + Deref<Target = str> + 's>.

    • to avoid typing too much you can create a trait alias:
      pub
      trait StrView<'s>
          : From<&'s str> + Clone + Deref<Target = str> + 's
      {}
      impl<'s, T> StrView<'s> for T
      where
          T : From<&'s str> + Clone + Deref<Target = str> + 's,
      {}
      
  • And then, add mutation functions for a specific monomoprhisation of your AST: Str = Cow<'a, str> or even Str = String

1 Like

I’m not knowledgeable enough about this myself, but if I would look for inspiration, there are two places that come to mind that work with strings, with parsing them zero copy, and for the latter maybe mutate them as well:

  1. Servo which parses html, using tendril
  2. xi-editor which uses ropes

Unfortunately the last update on both tendril and xi-rope starts to date a bit, but there are other rope implementations, like ropey which seems pretty active.

Cow seems to be made for the job, unless you have large strings and you only want to store some delta modification, rather than making a copy of the whole string on change.

1 Like