Discussion:
[rust-dev] Dynamic format template
Vadim Chugunov
2014-08-24 21:48:04 UTC
Permalink
Hi,
Is there any way to make Rust's fmt module to consume format template
specified at runtime?
This might be useful for localization of format!'ed strings, or, if one
wants to use format! as a rudimentary template engine.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140824/4a4c7e7f/attachment.html>
Kevin Ballard
2014-08-24 23:33:25 UTC
Permalink
It?s technically possible, but horribly unsafe. The only thing that makes it safe to do normally is the syntax extension that implements `format!()` ensures all the types match. If you really think you need this, you can look at the implementation of core::fmt. But it?s certainly not appropriate for localization, or template engines.

-Kevin Ballard
Hi,
Is there any way to make Rust's fmt module to consume format template specified at runtime?
This might be useful for localization of format!'ed strings, or, if one wants to use format! as a rudimentary template engine.
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4118 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140824/8dad974d/attachment.p7s>
Matthieu Monrocq
2014-08-25 16:29:48 UTC
Permalink
While not possible today, there is actually nothing preventing you to
create a safe alternative (or even improving format so it works in this
way).

In a sense, a formatting function has two set of inputs:

- the format itself, from which you extract a set of constraints (expected
type-signature)
- the arguments to format, which can be seen as a single tuple (provided
type-signature)

And as long as you can ensure at compile time that you never attempt to
apply an "expected type-signature" to an incompatible "provided
type-signature", then you are safe.


I would suppose that as far as having runtime formats go, you would need to
introduce an intermediary step: the expected type-signature.

You could have a "Format" object, generic over the expected type-signature,
and a "new" constructor method taking a &str and returning an
"Option<Format<...>>".


Now, you have two phases:

- the "new" constructor checks, at runtime, that the specified format
matches the expected type-signature
- the compiler checks, at compile-time, that the provided type-signature
(arguments) match the expected type-signature (or it can be coerced to)

It might require variadic generics and subtle massaging of the type system,
however I do think it would be possible.


It might not be the best way to attack the issue though.
Post by Kevin Ballard
It?s technically possible, but horribly unsafe. The only thing that makes
it safe to do normally is the syntax extension that implements `format!()`
ensures all the types match. If you really think you need this, you can
look at the implementation of core::fmt. But it?s certainly not appropriate
for localization, or template engines.
-Kevin Ballard
Post by Vadim Chugunov
Hi,
Is there any way to make Rust's fmt module to consume format template
specified at runtime?
Post by Vadim Chugunov
This might be useful for localization of format!'ed strings, or, if one
wants to use format! as a rudimentary template engine.
Post by Vadim Chugunov
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140825/435863e6/attachment.html>
Manish Goregaokar
2014-08-26 20:12:19 UTC
Permalink
Why not create a trait Localizer , and make Box<Localizer> fmt!able?

-Manish Goregaokar


On Mon, Aug 25, 2014 at 9:59 PM, Matthieu Monrocq <
Post by Matthieu Monrocq
While not possible today, there is actually nothing preventing you to
create a safe alternative (or even improving format so it works in this
way).
- the format itself, from which you extract a set of constraints (expected
type-signature)
- the arguments to format, which can be seen as a single tuple (provided
type-signature)
And as long as you can ensure at compile time that you never attempt to
apply an "expected type-signature" to an incompatible "provided
type-signature", then you are safe.
I would suppose that as far as having runtime formats go, you would need
to introduce an intermediary step: the expected type-signature.
You could have a "Format" object, generic over the expected
type-signature, and a "new" constructor method taking a &str and returning
an "Option<Format<...>>".
- the "new" constructor checks, at runtime, that the specified format
matches the expected type-signature
- the compiler checks, at compile-time, that the provided type-signature
(arguments) match the expected type-signature (or it can be coerced to)
It might require variadic generics and subtle massaging of the type
system, however I do think it would be possible.
It might not be the best way to attack the issue though.
Post by Kevin Ballard
It?s technically possible, but horribly unsafe. The only thing that makes
it safe to do normally is the syntax extension that implements `format!()`
ensures all the types match. If you really think you need this, you can
look at the implementation of core::fmt. But it?s certainly not appropriate
for localization, or template engines.
-Kevin Ballard
Post by Vadim Chugunov
Hi,
Is there any way to make Rust's fmt module to consume format template
specified at runtime?
Post by Vadim Chugunov
This might be useful for localization of format!'ed strings, or, if one
wants to use format! as a rudimentary template engine.
Post by Vadim Chugunov
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
_______________________________________________
Rust-dev mailing list
Rust-dev at mozilla.org
https://mail.mozilla.org/listinfo/rust-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140827/8da2ade9/attachment.html>
Loading...