Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plans on doing the other way (editting the front matter) #3

Open
SeaDve opened this issue Sep 3, 2021 · 8 comments
Open

Plans on doing the other way (editting the front matter) #3

SeaDve opened this issue Sep 3, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@SeaDve
Copy link
Contributor

SeaDve commented Sep 3, 2021

Very useful library btw

@yuchanns
Copy link
Collaborator

yuchanns commented Sep 3, 2021

More detail in your description please?

@yuchanns yuchanns added the question Further information is requested label Sep 3, 2021
@SeaDve
Copy link
Contributor Author

SeaDve commented Sep 3, 2021

Something like extracting the front matter, editing it, then writing back to the file

@yuchanns
Copy link
Collaborator

yuchanns commented Sep 3, 2021

Got it. I think add an api such as compose will be a nice idea. Or else do you have any better advice?

For example:

use gray_matter::Matter;
use gray_matter::engine::YAML;
use serde::Deserialize;

const INPUT: &str = r#"---
title: gray-matter-rs
tags:
  - gray-matter
  - rust
---
Some excerpt
---
Other stuff
"#;

fn main() {
	#[derive(Deserialize, Debug)]
    struct FrontMatter {
        title: String,
        tags: Vec<String>
    }
	let mut result_with_struct = matter.parse_with_struct::<FrontMatter>(INPUT).unwrap();
	result_with_struct.data.title = "gray-matter-rs-modified".to_owned();
	let output = matter.compose(result_with_struct);
	println!("{:?}", output)
	// ---
	// title: gray-matter-rs-modified
	// tags:
	//  - gray-matter
	//  - rust
	// ---
	// Some excerpt
	// ---
	// Other stuff

}

@SeaDve
Copy link
Contributor Author

SeaDve commented Sep 3, 2021

i think that's a good api. I dont have any better idea

@yuchanns yuchanns added enhancement New feature or request and removed question Further information is requested labels Sep 3, 2021
@kmaasrud
Copy link
Collaborator

kmaasrud commented Sep 5, 2021

We could implement the Display trait on ParsedEntity and ParsedEntityStruct, which would allow result_with_struct.to_string() to output the full input, but with changes reflected.

@yuchanns
Copy link
Collaborator

yuchanns commented Sep 5, 2021

We could implement the Display trait on ParsedEntity and ParsedEntityStruct, which would allow result_with_struct.to_string() to output the full input, but with changes reflected.

That's great!

@kmaasrud
Copy link
Collaborator

kmaasrud commented Sep 7, 2021

I've set a new requirement for the Engine trait:

https://github.com/kmaasrud/gray-matter-rs/blob/1183ce888bc5473a524629da3b6ac9e7193ac4e4/src/engine.rs#L23

This requirement means every engine will need to define how the front matter they've parsed shall be output, e.g.:

https://github.com/kmaasrud/gray-matter-rs/blob/1183ce888bc5473a524629da3b6ac9e7193ac4e4/src/engine/toml.rs#L20-L23

This is in turn is used in the fmt function for ParsedEntity:

https://github.com/kmaasrud/gray-matter-rs/blob/1183ce888bc5473a524629da3b6ac9e7193ac4e4/src/entity.rs#L58-L66

I was not able to implement Display on ParsedEntityStruct, since we would need to define a trait limitation on what structs can be converted into a Pod, which quickly becomes very complex. JSON and TOML are up and running, and I'm gonna finish YAML soon.

@yuchanns
Copy link
Collaborator

yuchanns commented Sep 7, 2021

I was not able to implement Display on ParsedEntityStruct, since we would need to define a trait limitation on what structs can be converted into a Pod, which quickly becomes very complex. JSON and TOML are up and running, and I'm gonna finish YAML soon.

Since now we can deserialized Pod into data implementing serde::de::DeserializeOwned in parse_with_struct, how about add an extra limitation trait serde::ser::Serialize? In that way we can easily implement Display on ParsedEntityStruct without making things complex, just serialize structs into Pod. Then we can smoothly replace all serd trait by ourselves once traits Deserialize and Serialize of Pod implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants