Molten icon indicating copy to clipboard operation
Molten copied to clipboard

Add static "from" methods and allow writing to files

Open LeopoldArkham opened this issue 6 years ago • 4 comments

  • Parser::new() should become Parser::from() and accept anything that AsRef's to &str
  • Parser::from_file() should be added and accept anything that AsRef's to Path
  • TOMLDocument::save_to() should be added and accept anything that AsRef's to Path
  • TOMLDocument::save() should be added, it overwrites the source file if the parser was created through the Parser::from_file() method and returns an error otherwise.

The tests will need to be updated to use these new methods

LeopoldArkham avatar Nov 22 '17 16:11 LeopoldArkham

Taking #5 in mind, should Parser::::from() automatically parse the data? For example, the test in parser.rs line 757, could look like either of these::

let _ = Parser::from(text).parse();

or

let _ = Parser::from(text);

The later feels better/more ergonomic to me.

If you want to make all of the Parser functions private, you could add a from() and from_file() onto the TOMLDocument, and then have the document control when parsing happens. It depends on what shape the API would be for the users.

markcol avatar Dec 01 '17 22:12 markcol

Actually, from() and from_file() could even be free functions, that's how most parsing libraries seem to do it:

extern crate molten;

let doc = molten::from_file("cargo.toml");

Seems like the best solution to me!

LeopoldArkham avatar Dec 02 '17 12:12 LeopoldArkham

I agree. I'm working on it.

markcol avatar Dec 04 '17 16:12 markcol

Great! Note that from_file may have to become from_reader, otherwise the references in the parsed document will outlive the original file

LeopoldArkham avatar Dec 04 '17 19:12 LeopoldArkham