Skip to content

Commit b1ca558

Browse files
committed
Improve generation
1 parent 0dae8ae commit b1ca558

File tree

6 files changed

+71
-48
lines changed

6 files changed

+71
-48
lines changed

api/src/models/asset.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use serde::{Deserialize, Serialize};
22
use crate::models::Named;
33

4-
// Path: assets/{author}/{name}/asset.toml
4+
// Path: content/{author}/{name}/asset.toml
55
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
66
pub struct Asset {
77
pub author : String,
88
pub name : String,
99
pub id : String,
1010
pub tags : Vec<String>,
11-
pub thumbnail_url : String,
12-
pub preview_urls : Vec<String>,
11+
pub thumbnail_url : Option<String>,
12+
pub preview_urls : Option<Vec<String>>,
1313
pub current_version : Version,
1414
pub previous_versions : Vec<Version>,
1515
pub summary : Option<String>,
@@ -28,7 +28,7 @@ pub struct Version {
2828
pub blake3 : Option<String>,
2929
}
3030

31-
// Path: assets/{author}/author.toml
31+
// Path: content/{author}/author.toml
3232
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
3333
pub struct Author {
3434
pub name : String,

cli/src/directory.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub enum ModelError {
1212
IoError(#[from] io::Error),
1313
#[error(transparent)]
1414
TomlError(#[from] de::Error),
15-
#[error("Model file not found")]
16-
ModelFileNotFound,
15+
#[error("Model file not found (expected {0:?})")]
16+
ModelFileNotFound(Option<PathBuf>),
1717
#[error("Model name not valid (expected {expected:?}, found {found:?})")]
1818
NotValidName { expected: Option<String>, found: String },
1919
}
@@ -29,13 +29,17 @@ pub trait ModelDirectory<T>
2929
fn file_path(&self) -> PathBuf {
3030
self.data_path().join("config.toml")
3131
}
32+
fn content_path(&self) -> PathBuf {
33+
self.data_path().join("content")
34+
}
3235
fn name(&self) -> Option<String>;
3336
fn model(&self) -> Result<T, ModelError> {
37+
let file = self.file_path();
3438
if !self.is_valid() {
35-
return Err(ModelFileNotFound);
39+
return Err(ModelFileNotFound(Some(file)));
3640
}
3741

38-
let data = std::fs::read_to_string(self.file_path())?;
42+
let data = std::fs::read_to_string(file)?;
3943
let model: T = toml::from_str(&data)?;
4044
if self.name().map(|name| name != model.name()).unwrap_or(false) {
4145
return Err(ModelError::NotValidName {
@@ -62,20 +66,27 @@ impl RepositoryDirectory {
6266
}
6367

6468
pub fn authors(&self) -> Result<Vec<String>, io::Error> {
65-
let mut assets = Vec::new();
66-
for entry in self.data_path().read_dir()? {
69+
let mut authors = Vec::new();
70+
let content = self.content_path();
71+
if !content.exists() {
72+
return Ok(authors);
73+
}
74+
for entry in content.read_dir()? {
6775
let Ok(entry) = entry else {
6876
continue;
6977
};
78+
if !entry.path().is_dir() {
79+
continue;
80+
}
7081
let path = entry.path();
7182
let file_name = path.file_name().unwrap().to_str().unwrap().to_string();
7283

7384
if path.is_dir() {
74-
assets.push(file_name);
85+
authors.push(file_name);
7586
}
7687
}
7788

78-
Ok(assets)
89+
Ok(authors)
7990
}
8091

8192
pub fn author(&self, name: &str) -> AuthorDirectory {
@@ -98,7 +109,11 @@ pub struct AuthorDirectory<'a> (&'a RepositoryDirectory, String);
98109
impl AuthorDirectory<'_> {
99110
pub fn assets(&self) -> Result<Vec<String>, io::Error> {
100111
let mut assets = Vec::new();
101-
for entry in self.data_path().read_dir()? {
112+
let path = self.assets_path();
113+
if !path.exists() {
114+
return Ok(assets);
115+
}
116+
for entry in path.read_dir()? {
102117
let Ok(entry) = entry else {
103118
continue;
104119
};
@@ -113,14 +128,18 @@ impl AuthorDirectory<'_> {
113128
Ok(assets)
114129
}
115130

131+
pub fn assets_path(&self) -> PathBuf {
132+
self.data_path().join("assets")
133+
}
134+
116135
pub fn asset(&self, name: &str) -> AssetDirectory {
117136
AssetDirectory(self, name.to_owned())
118137
}
119138
}
120139

121140
impl ModelDirectory<Author> for AuthorDirectory<'_> {
122141
fn data_path(&self) -> PathBuf {
123-
self.0.data_path().join(&self.1)
142+
self.0.content_path().join(&self.1)
124143
}
125144

126145
fn name(&self) -> Option<String> {
@@ -130,32 +149,9 @@ impl ModelDirectory<Author> for AuthorDirectory<'_> {
130149

131150
pub struct AssetDirectory<'a> (&'a AuthorDirectory<'a>, String);
132151

133-
impl AssetDirectory<'_> {
134-
pub fn assets_path(&self) -> PathBuf {
135-
self.data_path().join("assets")
136-
}
137-
138-
pub fn is_valid(&self) -> bool {
139-
self.data_path().is_dir()
140-
}
141-
142-
pub fn get_asset_path(&self, name: &str) -> Option<PathBuf> {
143-
let path = self.assets_path().join(name);
144-
// If path is file and assets_path is parent of path
145-
if !path.is_file() || !path.starts_with(self.assets_path()) {
146-
return None;
147-
}
148-
Some(path)
149-
}
150-
151-
pub fn has_asset(&self, name: &str) -> bool {
152-
self.get_asset_path(name).is_some()
153-
}
154-
}
155-
156152
impl ModelDirectory<Asset> for AssetDirectory<'_> {
157153
fn data_path(&self) -> PathBuf {
158-
self.0.data_path().join(&self.1)
154+
self.0.assets_path().join(&self.1)
159155
}
160156

161157
fn name(&self) -> Option<String> {

cli/src/docs.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::{fs, path::Path, io::{self, Write}};
1+
use std::{fs, path::Path, io::Write};
22

3-
use handlebars::{Handlebars, TemplateError, Context};
3+
use handlebars::{Handlebars, TemplateError};
44
use luna_api::models::RepositoryData;
5-
use serde_json::json;
5+
use serde_json::{json, Value};
66
use thiserror::Error;
77
use rust_embed::RustEmbed;
88

@@ -31,6 +31,18 @@ pub fn generate_docs(data: &RepositoryData, output : String) -> Result<(), DocsE
3131
render_static("index", data, &hb, &output)?;
3232
render_static("search", data, &hb, &output)?;
3333

34+
35+
for author in data.authors.iter() {
36+
fs::create_dir_all(format!("{}/{}", output, author.name))?;
37+
}
38+
for asset in data.assets.iter() {
39+
let context: &Value = &json!({
40+
"asset": asset,
41+
"info": data.info
42+
});
43+
render_dynamic("asset", &format!("{}/{}", asset.author, asset.name), &hb, &output, context)?;
44+
}
45+
3446
for file in Public::iter() {
3547
let path = file.as_ref();
3648
let content = Public::get(path).unwrap();
@@ -47,11 +59,16 @@ pub fn generate_docs(data: &RepositoryData, output : String) -> Result<(), DocsE
4759
}
4860

4961
fn render_static(name : &str, data: &RepositoryData, hb: &Handlebars, output : &str) -> Result<(), DocsError> {
50-
let context = &json!({
62+
let context: &Value = &json!({
5163
"info": data.info
5264
});
53-
let rendered = hb.render(&format!("templates/{}.hbs",name), context)?;
54-
std::fs::write(format!("{}/{}.html", output, name), rendered)?;
55-
println!("Rendered {} at {}/{}.html", name, output, name);
65+
render_dynamic(name, name, hb, output, context)
66+
}
67+
68+
fn render_dynamic(name : &str, output_name : &str, hb: &Handlebars, output : &str, context: &Value) -> Result<(), DocsError> {
69+
let rendered = hb.render(&format!("templates/{}.hbs",name), &context)?;
70+
fs::write(format!("{}/{}.html", output, output_name), rendered)?;
71+
println!("Rendered {} at {}/{}.html", name, output, output_name);
5672
Ok(())
73+
5774
}

cli/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ fn main() {
5858
}
5959

6060
fn docs(path: Option<String>, index: Option<String>) {
61-
let index = index.unwrap_or(String::from("index.json"));
61+
let index = index.unwrap_or(format!("{}/index.json", path.clone().unwrap_or(".".to_string())));
6262
let data = RepositoryData::from_index(
6363
std::fs::read_to_string(&index)
64-
.expect("Could not read index file")
64+
.expect(&format!("Could not read index file {}", &index))
6565
.as_ref(),
6666
)
6767
.expect("Could not parse index file");
@@ -76,7 +76,10 @@ fn docs(path: Option<String>, index: Option<String>) {
7676
}
7777

7878
fn generate(path: Option<String>) {
79-
let path = path.unwrap_or(String::from("index.json"));
79+
if let Some(path) = &path {
80+
std::fs::create_dir_all(&path).expect("Could not create directory");
81+
}
82+
let path = format!("{}/index.json", path.unwrap_or(".".to_string()));
8083
let directory = directory::RepositoryDirectory::new(None);
8184
match { directory.generate_index() } {
8285
Ok(data) => {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "testasset"
2+
author = "obiwan"
3+
id = "LEL"
4+
tags = ["test", "asset"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "codedoctor"
2+
3+
links = ["https://github.com/CodeDoctorDE"]

0 commit comments

Comments
 (0)