Is it possible to derive a parser for an arbitrary type that parses the corresponding Debug format?












3















In Haskell, it is possible, as a language feature, to derive a parser from a string to an arbitrary datatype: that's called the Read class. Is it possible to do so in Rust? That is, given an arbitrary enum such as:



#[derive(Debug)]
enum Foo {
A { x: u32, s: String },
B { v: Vec<u8> },
}


and, given that Rust includes the Debug trait as a language feature that serializes an arbitrary datatype, is it possible to also automatically generate its corresponding Parse trait?



In other words, is there a default Rust feature that allows me to derive a parse(&str) -> Foo function such that, for any string s either parse(&s) == None or format!("{:?}", parse(&s).unwrap()) == s?










share|improve this question




















  • 4





    serde is probably what you are looking for.

    – Stargateur
    Nov 27 '18 at 0:56






  • 1





    You may be interested in RON, a data format that is something of a middle ground between JSON and what Debug outputs, and does support deserialization through serde.

    – trentcl
    Nov 27 '18 at 13:47











  • This question is being discussed on meta.

    – Script47
    Nov 27 '18 at 14:35
















3















In Haskell, it is possible, as a language feature, to derive a parser from a string to an arbitrary datatype: that's called the Read class. Is it possible to do so in Rust? That is, given an arbitrary enum such as:



#[derive(Debug)]
enum Foo {
A { x: u32, s: String },
B { v: Vec<u8> },
}


and, given that Rust includes the Debug trait as a language feature that serializes an arbitrary datatype, is it possible to also automatically generate its corresponding Parse trait?



In other words, is there a default Rust feature that allows me to derive a parse(&str) -> Foo function such that, for any string s either parse(&s) == None or format!("{:?}", parse(&s).unwrap()) == s?










share|improve this question




















  • 4





    serde is probably what you are looking for.

    – Stargateur
    Nov 27 '18 at 0:56






  • 1





    You may be interested in RON, a data format that is something of a middle ground between JSON and what Debug outputs, and does support deserialization through serde.

    – trentcl
    Nov 27 '18 at 13:47











  • This question is being discussed on meta.

    – Script47
    Nov 27 '18 at 14:35














3












3








3


1






In Haskell, it is possible, as a language feature, to derive a parser from a string to an arbitrary datatype: that's called the Read class. Is it possible to do so in Rust? That is, given an arbitrary enum such as:



#[derive(Debug)]
enum Foo {
A { x: u32, s: String },
B { v: Vec<u8> },
}


and, given that Rust includes the Debug trait as a language feature that serializes an arbitrary datatype, is it possible to also automatically generate its corresponding Parse trait?



In other words, is there a default Rust feature that allows me to derive a parse(&str) -> Foo function such that, for any string s either parse(&s) == None or format!("{:?}", parse(&s).unwrap()) == s?










share|improve this question
















In Haskell, it is possible, as a language feature, to derive a parser from a string to an arbitrary datatype: that's called the Read class. Is it possible to do so in Rust? That is, given an arbitrary enum such as:



#[derive(Debug)]
enum Foo {
A { x: u32, s: String },
B { v: Vec<u8> },
}


and, given that Rust includes the Debug trait as a language feature that serializes an arbitrary datatype, is it possible to also automatically generate its corresponding Parse trait?



In other words, is there a default Rust feature that allows me to derive a parse(&str) -> Foo function such that, for any string s either parse(&s) == None or format!("{:?}", parse(&s).unwrap()) == s?







parsing rust






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 15:25









trentcl

6,85131334




6,85131334










asked Nov 27 '18 at 0:15









MaiaVictorMaiaVictor

18.9k34104220




18.9k34104220








  • 4





    serde is probably what you are looking for.

    – Stargateur
    Nov 27 '18 at 0:56






  • 1





    You may be interested in RON, a data format that is something of a middle ground between JSON and what Debug outputs, and does support deserialization through serde.

    – trentcl
    Nov 27 '18 at 13:47











  • This question is being discussed on meta.

    – Script47
    Nov 27 '18 at 14:35














  • 4





    serde is probably what you are looking for.

    – Stargateur
    Nov 27 '18 at 0:56






  • 1





    You may be interested in RON, a data format that is something of a middle ground between JSON and what Debug outputs, and does support deserialization through serde.

    – trentcl
    Nov 27 '18 at 13:47











  • This question is being discussed on meta.

    – Script47
    Nov 27 '18 at 14:35








4




4





serde is probably what you are looking for.

– Stargateur
Nov 27 '18 at 0:56





serde is probably what you are looking for.

– Stargateur
Nov 27 '18 at 0:56




1




1





You may be interested in RON, a data format that is something of a middle ground between JSON and what Debug outputs, and does support deserialization through serde.

– trentcl
Nov 27 '18 at 13:47





You may be interested in RON, a data format that is something of a middle ground between JSON and what Debug outputs, and does support deserialization through serde.

– trentcl
Nov 27 '18 at 13:47













This question is being discussed on meta.

– Script47
Nov 27 '18 at 14:35





This question is being discussed on meta.

– Script47
Nov 27 '18 at 14:35












1 Answer
1






active

oldest

votes


















6














No, there is no such feature. Debug is intended for human/programmer consumption, not for machines.



There's no guarantee that the Debug output is even in a parseable format or that it contains the complete data of a type.



I recommend using Serde instead, paired with an existing defined serialization format of your choice.



If you wanted to, you could define your own derive attributes that implement Debug and FromStr by calling into Serde.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490988%2fis-it-possible-to-derive-a-parser-for-an-arbitrary-type-that-parses-the-correspo%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    No, there is no such feature. Debug is intended for human/programmer consumption, not for machines.



    There's no guarantee that the Debug output is even in a parseable format or that it contains the complete data of a type.



    I recommend using Serde instead, paired with an existing defined serialization format of your choice.



    If you wanted to, you could define your own derive attributes that implement Debug and FromStr by calling into Serde.






    share|improve this answer






























      6














      No, there is no such feature. Debug is intended for human/programmer consumption, not for machines.



      There's no guarantee that the Debug output is even in a parseable format or that it contains the complete data of a type.



      I recommend using Serde instead, paired with an existing defined serialization format of your choice.



      If you wanted to, you could define your own derive attributes that implement Debug and FromStr by calling into Serde.






      share|improve this answer




























        6












        6








        6







        No, there is no such feature. Debug is intended for human/programmer consumption, not for machines.



        There's no guarantee that the Debug output is even in a parseable format or that it contains the complete data of a type.



        I recommend using Serde instead, paired with an existing defined serialization format of your choice.



        If you wanted to, you could define your own derive attributes that implement Debug and FromStr by calling into Serde.






        share|improve this answer















        No, there is no such feature. Debug is intended for human/programmer consumption, not for machines.



        There's no guarantee that the Debug output is even in a parseable format or that it contains the complete data of a type.



        I recommend using Serde instead, paired with an existing defined serialization format of your choice.



        If you wanted to, you could define your own derive attributes that implement Debug and FromStr by calling into Serde.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 27 '18 at 14:04

























        answered Nov 27 '18 at 13:43









        ShepmasterShepmaster

        155k14307452




        155k14307452
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490988%2fis-it-possible-to-derive-a-parser-for-an-arbitrary-type-that-parses-the-correspo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)