-
Notifications
You must be signed in to change notification settings - Fork 111
added feature synth-doc #99
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
f170fe3
fd5fc82
e635d07
d943582
61f2b30
b7ec98b
4bdcf35
fbe83bf
127519f
4f735b3
4a9713c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,128 @@ | ||||||||||||
| use synth_core::{Content, Name, Namespace, schema::{ArrayContent, ObjectContent}}; | ||||||||||||
| use std::{convert::TryFrom, fs::{File, OpenOptions}, io::{BufWriter, Write}, path::{Path, PathBuf}, str::FromStr}; | ||||||||||||
| use crate::sampler::Sampler; | ||||||||||||
| use super::{ExportStrategy, ExportParams}; | ||||||||||||
| use anyhow::{Result, Context}; | ||||||||||||
|
|
||||||||||||
| #[derive(Clone, Debug)] | ||||||||||||
| pub struct DocExportStrategy{ | ||||||||||||
| file_path: PathBuf | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| impl ExportStrategy for DocExportStrategy{ | ||||||||||||
| fn export(self, params: ExportParams) -> Result<()> { | ||||||||||||
| let generator = Sampler::try_from(¶ms.namespace)?; | ||||||||||||
| let values = generator.sample_seeded(params.collection_name, params.target, params.seed)?; | ||||||||||||
| let file = OpenOptions::new().create(true).read(true).append(true).open(self.file_path.clone())?; | ||||||||||||
| let mut file = BufWriter::new(file); | ||||||||||||
| write_doc(¶ms.namespace, &mut file)?; | ||||||||||||
| //example result | ||||||||||||
| writeln!(&mut file, "```json")?; | ||||||||||||
| serde_json::to_writer_pretty(&mut file, &values)?; | ||||||||||||
| writeln!(file, "")?; | ||||||||||||
| writeln!(&mut file, "```")?; | ||||||||||||
| writeln!(file, "> Created with [synth](https://github.com/getsynth/synth)")?; | ||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| impl DocExportStrategy { | ||||||||||||
| pub fn new(path: PathBuf) -> Result<Self> { | ||||||||||||
| let file_path = Path::join(&path, Path::new("README.md")); | ||||||||||||
| Ok( | ||||||||||||
| Self { | ||||||||||||
| file_path | ||||||||||||
| } | ||||||||||||
| ) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn write_doc(ns: &Namespace, file: &mut BufWriter<File>) -> Result<()>{ | ||||||||||||
| write_colls(ns, file)?; | ||||||||||||
| writeln!(file, "---")?; | ||||||||||||
| let pairs = ns.iter(); | ||||||||||||
| for (n,c) in pairs { | ||||||||||||
| write_coll_details((n,c), file)?; | ||||||||||||
| writeln!(file, "---")?; | ||||||||||||
| }; | ||||||||||||
| writeln!(file, "---")?; | ||||||||||||
| writeln!(file, "### Example")?; | ||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| fn write_colls(ns: &Namespace, file: &mut BufWriter<File>) -> Result<()> { | ||||||||||||
| writeln!(file, "```text\n### Description\n\n\n\n\n```")?; | ||||||||||||
| writeln!(file, "## Collections")?; | ||||||||||||
| for key in ns.keys().map(|n| n.as_ref()) { | ||||||||||||
| writeln!(file, "- [{}]({})", key, format!("#collection-{}", key)).context("error while writing to file")?; | ||||||||||||
| }; | ||||||||||||
| Ok(()) | ||||||||||||
| } | ||||||||||||
| fn write_coll_details((name, content ): (&Name,&Content), file: &mut BufWriter<File>) -> Result<()>{ | ||||||||||||
|
||||||||||||
| match content { | ||||||||||||
| Content::Array(ArrayContent { content: box Content::Object(object_content), .. }) => { | ||||||||||||
| let name_str = format!("Collection {}", name); | ||||||||||||
| writeln!(file, "#### {}", name_str)?; | ||||||||||||
| let name = Name::from_str("Collection")?; | ||||||||||||
| write_coll_details((&name, &Content::Object(object_content.clone()) ), file)?; | ||||||||||||
| write_foreigns((&name, &object_content), file)?; | ||||||||||||
| } | ||||||||||||
| Content::Object(obj) => { | ||||||||||||
| writeln!(file, "##### Fields")?; | ||||||||||||
|
||||||||||||
| let ObjectContent { fields } = obj; | ||||||||||||
| for (field_name, field_content) in fields.into_iter() { | ||||||||||||
| let cont = &field_content.content; | ||||||||||||
| let f_name = &Name::from_str(field_name)?; | ||||||||||||
| if let box Content::Array(arr) = &cont { | ||||||||||||
| write_subcoll(name, (f_name, arr), file)?; | ||||||||||||
| } else { | ||||||||||||
| write_coll_details((f_name, cont ), file)?; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| }, | ||||||||||||
| Content::Null => { | ||||||||||||
| writeln!(file, "##### {} [Type: *Null*]", name)?; | ||||||||||||
| writeln!(file, "> Description goes here: ")?; | ||||||||||||
| writeln!(file, "")?; | ||||||||||||
| writeln!(file, "")?; | ||||||||||||
|
||||||||||||
| writeln!(file, "##### {} [Type: *Null*]", name)?; | |
| writeln!(file, "> Description goes here: ")?; | |
| writeln!(file, "")?; | |
| writeln!(file, "")?; | |
| writeln!(file, "##### {} [Type: *Null*]\n> Description goes here: \n\n", name)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh.. No need? I'll do that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll write it all once as a single formatted string
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This "```text" makes the whole thing a code block. I don't think that's right here. Also below the
Descriptionheader there should be a(please enter a descriptive text here)or some such.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I'll fix this. Thanks.