Skip to content

Commit bafc0f9

Browse files
authored
feat(content): add inventory command (#80)
* feat(content): add inventory command fixes #75
1 parent 84b6358 commit bafc0f9

File tree

18 files changed

+200
-59
lines changed

18 files changed

+200
-59
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rari-cli/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ use rari_doc::build::{
1919
use rari_doc::cached_readers::{read_and_cache_doc_pages, CACHED_DOC_PAGE_FILES};
2020
use rari_doc::issues::IN_MEMORY;
2121
use rari_doc::pages::json::BuiltPage;
22+
use rari_doc::pages::page::Page;
2223
use rari_doc::pages::types::doc::Doc;
2324
use rari_doc::reader::read_docs_parallel;
2425
use rari_doc::search_index::build_search_index;
2526
use rari_doc::utils::TEMPL_RECORDER_SENDER;
2627
use rari_sitemap::Sitemaps;
2728
use rari_tools::add_redirect::add_redirect;
2829
use rari_tools::history::gather_history;
30+
use rari_tools::inventory::gather_inventory;
2931
use rari_tools::r#move::r#move;
3032
use rari_tools::redirects::fix_redirects;
3133
use rari_tools::remove::remove;
@@ -100,6 +102,8 @@ enum ContentSubcommand {
100102
/// This shortens multiple redirect chains to single ones.
101103
/// This is also run as part of sync_translated_content.
102104
FixRedirects,
105+
/// Create content inventory as JSON
106+
Inventory,
103107
}
104108

105109
#[derive(Args)]
@@ -294,14 +298,14 @@ fn main() -> Result<(), Error> {
294298
if args.all || !args.no_basic || args.content || !args.files.is_empty() {
295299
let start = std::time::Instant::now();
296300
docs = if !args.files.is_empty() {
297-
read_docs_parallel::<Doc>(&args.files, None)?
301+
read_docs_parallel::<Page, Doc>(&args.files, None)?
298302
} else if args.no_cache {
299303
let files: &[_] = if let Some(translated_root) = content_translated_root() {
300304
&[content_root(), translated_root]
301305
} else {
302306
&[content_root()]
303307
};
304-
read_docs_parallel::<Doc>(files, None)?
308+
read_docs_parallel::<Page, Doc>(files, None)?
305309
} else {
306310
read_and_cache_doc_pages()?
307311
};
@@ -439,6 +443,9 @@ fn main() -> Result<(), Error> {
439443
ContentSubcommand::FixRedirects => {
440444
fix_redirects()?;
441445
}
446+
ContentSubcommand::Inventory => {
447+
gather_inventory()?;
448+
}
442449
},
443450
Commands::Update(args) => update(args.version)?,
444451
Commands::ExportSchema(args) => export_schema(args)?,

crates/rari-cli/serve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn get_search_index(locale: Locale) -> Result<Vec<SearchItem>, DocError> {
115115
.join("popularities.json");
116116
let json_str = read_to_string(in_file)?;
117117
let popularities: Popularities = serde_json::from_str(&json_str)?;
118-
let docs = read_docs_parallel::<Doc>(
118+
let docs = read_docs_parallel::<Page, Doc>(
119119
&[&if locale == Locale::EnUs {
120120
content_root()
121121
} else {

crates/rari-doc/src/cached_readers.rs

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub fn doc_page_from_static_files(path: &Path) -> Result<Page, DocError> {
233233
fn gather_blog_posts() -> Result<HashMap<String, Page>, DocError> {
234234
if let Some(blog_root) = blog_root() {
235235
let post_root = blog_root.join("posts");
236-
Ok(read_docs_parallel::<BlogPost>(&[post_root], None)?
236+
Ok(read_docs_parallel::<Page, BlogPost>(&[post_root], None)?
237237
.into_iter()
238238
.map(|page| (page.url().to_ascii_lowercase(), page))
239239
.collect())
@@ -244,23 +244,25 @@ fn gather_blog_posts() -> Result<HashMap<String, Page>, DocError> {
244244

245245
fn gather_generic_content() -> Result<HashMap<String, Page>, DocError> {
246246
if let Some(root) = generic_content_root() {
247-
Ok(read_docs_parallel::<GenericPage>(&[root], Some("*.md"))?
248-
.into_iter()
249-
.filter_map(|page| {
250-
if let Page::GenericPage(generic) = page {
251-
Some(generic)
252-
} else {
253-
None
254-
}
255-
})
256-
.flat_map(|generic| {
257-
Locale::for_generic_and_spas()
258-
.iter()
259-
.map(|locale| Page::GenericPage(Arc::new(generic.as_locale(*locale))))
260-
.collect::<Vec<_>>()
261-
})
262-
.map(|page| (page.url().to_ascii_lowercase(), page))
263-
.collect())
247+
Ok(
248+
read_docs_parallel::<Page, GenericPage>(&[root], Some("*.md"))?
249+
.into_iter()
250+
.filter_map(|page| {
251+
if let Page::GenericPage(generic) = page {
252+
Some(generic)
253+
} else {
254+
None
255+
}
256+
})
257+
.flat_map(|generic| {
258+
Locale::for_generic_and_spas()
259+
.iter()
260+
.map(|locale| Page::GenericPage(Arc::new(generic.as_locale(*locale))))
261+
.collect::<Vec<_>>()
262+
})
263+
.map(|page| (page.url().to_ascii_lowercase(), page))
264+
.collect(),
265+
)
264266
} else {
265267
Err(DocError::NoGenericContentRoot)
266268
}
@@ -270,7 +272,7 @@ fn gather_curriculum() -> Result<CurriculumFiles, DocError> {
270272
if let Some(curriculum_root) = curriculum_root() {
271273
let curriculum_root = curriculum_root.join("curriculum");
272274
let pages: Vec<Page> =
273-
read_docs_parallel::<CurriculumPage>(&[curriculum_root], Some("*.md"))?
275+
read_docs_parallel::<Page, CurriculumPage>(&[curriculum_root], Some("*.md"))?
274276
.into_iter()
275277
.collect();
276278
let by_url: HashMap<String, Page> = pages
@@ -321,23 +323,25 @@ fn gather_curriculum() -> Result<CurriculumFiles, DocError> {
321323

322324
fn gather_contributor_spotlight() -> Result<HashMap<String, Page>, DocError> {
323325
if let Some(root) = contributor_spotlight_root() {
324-
Ok(read_docs_parallel::<ContributorSpotlight>(&[root], None)?
325-
.into_iter()
326-
.filter_map(|page| {
327-
if let Page::ContributorSpotlight(cs) = page {
328-
Some(cs)
329-
} else {
330-
None
331-
}
332-
})
333-
.flat_map(|cs| {
334-
Locale::for_generic_and_spas()
335-
.iter()
336-
.map(|locale| Page::ContributorSpotlight(Arc::new(cs.as_locale(*locale))))
337-
.collect::<Vec<_>>()
338-
})
339-
.map(|page| (page.url().to_ascii_lowercase(), page))
340-
.collect())
326+
Ok(
327+
read_docs_parallel::<Page, ContributorSpotlight>(&[root], None)?
328+
.into_iter()
329+
.filter_map(|page| {
330+
if let Page::ContributorSpotlight(cs) = page {
331+
Some(cs)
332+
} else {
333+
None
334+
}
335+
})
336+
.flat_map(|cs| {
337+
Locale::for_generic_and_spas()
338+
.iter()
339+
.map(|locale| Page::ContributorSpotlight(Arc::new(cs.as_locale(*locale))))
340+
.collect::<Vec<_>>()
341+
})
342+
.map(|page| (page.url().to_ascii_lowercase(), page))
343+
.collect(),
344+
)
341345
} else {
342346
Err(DocError::NoContributorSpotlightRoot)
343347
}
@@ -481,7 +485,7 @@ pub fn blog_author_by_name(name: &str) -> Option<Arc<Author>> {
481485
/// This function will return an error if:
482486
/// - An error occurs while reading the documentation pages from the content root or translated content root directories.
483487
pub fn read_and_cache_doc_pages() -> Result<Vec<Page>, DocError> {
484-
let mut docs = read_docs_parallel::<Doc>(&[content_root()], None)?;
488+
let mut docs = read_docs_parallel::<Page, Doc>(&[content_root()], None)?;
485489
STATIC_DOC_PAGE_FILES
486490
.set(
487491
docs.iter()
@@ -491,7 +495,7 @@ pub fn read_and_cache_doc_pages() -> Result<Vec<Page>, DocError> {
491495
)
492496
.unwrap();
493497
if let Some(translated_root) = content_translated_root() {
494-
let translated_docs = read_docs_parallel::<Doc>(&[translated_root], None)?;
498+
let translated_docs = read_docs_parallel::<Page, Doc>(&[translated_root], None)?;
495499
STATIC_DOC_PAGE_TRANSLATED_FILES
496500
.set(
497501
translated_docs

crates/rari-doc/src/pages/page.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl Page {
217217
}
218218
}
219219

220-
impl PageReader for Page {
220+
impl PageReader<Page> for Page {
221221
fn read(path: impl Into<PathBuf>, locale: Option<Locale>) -> Result<Page, DocError> {
222222
let path = path.into();
223223
let (_, typ) = locale_and_typ_from_path(&path)?;
@@ -322,7 +322,7 @@ impl<T: PageLike> PageLike for Arc<T> {
322322
///
323323
/// The `PageReader` trait defines a method for reading pages from a specified path,
324324
/// optionally considering a locale.
325-
pub trait PageReader {
325+
pub trait PageReader<T> {
326326
/// Reads a page from the given path.
327327
///
328328
/// # Arguments
@@ -333,7 +333,7 @@ pub trait PageReader {
333333
/// # Returns
334334
///
335335
/// * `Result<Page, DocError>` - Returns a `Page` on success, or a `DocError` on failure.
336-
fn read(path: impl Into<PathBuf>, locale: Option<Locale>) -> Result<Page, DocError>;
336+
fn read(path: impl Into<PathBuf>, locale: Option<Locale>) -> Result<T, DocError>;
337337
}
338338

339339
/// A trait for writing pages in the documentation system.

crates/rari-doc/src/pages/types/blog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ impl BlogPost {
211211
}
212212
}
213213

214-
impl PageReader for BlogPost {
214+
impl PageReader<Page> for BlogPost {
215215
fn read(path: impl Into<PathBuf>, _: Option<Locale>) -> Result<Page, DocError> {
216216
read_blog_post(path).map(Arc::new).map(Page::BlogPost)
217217
}

crates/rari-doc/src/pages/types/contributors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl ContributorSpotlight {
156156
}
157157
}
158158

159-
impl PageReader for ContributorSpotlight {
159+
impl PageReader<Page> for ContributorSpotlight {
160160
fn read(path: impl Into<PathBuf>, locale: Option<Locale>) -> Result<Page, DocError> {
161161
read_contributor_spotlight(path, locale.unwrap_or_default())
162162
.map(Arc::new)

crates/rari-doc/src/pages/types/curriculum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl CurriculumPage {
144144
}
145145
}
146146

147-
impl PageReader for CurriculumPage {
147+
impl PageReader<Page> for CurriculumPage {
148148
fn read(path: impl Into<PathBuf>, _: Option<Locale>) -> Result<Page, DocError> {
149149
let full_path = path.into();
150150
let raw = read_to_string(&full_path)?;

crates/rari-doc/src/pages/types/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Doc {
161161
}
162162
}
163163

164-
impl PageReader for Doc {
164+
impl PageReader<Page> for Doc {
165165
fn read(path: impl Into<PathBuf>, _: Option<Locale>) -> Result<Page, DocError> {
166166
let path = path.into();
167167
if let Ok(doc) = doc_page_from_static_files(&path) {

crates/rari-doc/src/pages/types/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl GenericPageMeta {
6262
}
6363
}
6464

65-
impl PageReader for GenericPage {
65+
impl PageReader<Page> for GenericPage {
6666
fn read(
6767
path: impl Into<PathBuf>,
6868
locale: Option<Locale>,

0 commit comments

Comments
 (0)