File tree 1 file changed +52
-0
lines changed 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ use book:: metadata:: BookMetadata ;
2
+
3
+ use std:: path;
4
+
5
+ /// The Chapter struct holds the title of the chapter as written in the SUMMARY.md file,
6
+ /// the location of the markdown file containing the content and eventually sub-chapters
7
+ pub struct Chapter {
8
+ title : String ,
9
+ file : path:: PathBuf ,
10
+
11
+ sub_chapters : Vec < Chapter > ,
12
+ }
13
+
14
+
15
+ impl Chapter {
16
+ /// Creates a new chapter with the given title and source file and no sub-chapters
17
+ pub fn new ( title : & str , file : & path:: Path ) -> Self {
18
+ Chapter {
19
+ title : title. to_owned ( ) ,
20
+ file : file. to_owned ( ) ,
21
+
22
+ sub_chapters : Vec :: new ( ) ,
23
+ }
24
+ }
25
+
26
+ /// This function takes a slice `&[x,y,z]` and returns the corresponding sub-chapter if it exists.
27
+ ///
28
+ /// For example: `chapter.get_sub_chapter(&[1,3])` will return the third sub-chapter of the first sub-chapter.
29
+ pub fn get_sub_chapter ( & self , section : & [ usize ] ) -> Option < & Chapter > {
30
+ match section. len ( ) {
31
+ 0 => None ,
32
+ 1 => self . sub_chapters . get ( section[ 0 ] ) ,
33
+ _ => {
34
+ // The lengt of the slice is more than one, this means that we want a sub-chapter of a sub-chapter
35
+ // We call `get_sub_chapter` recursively until we are deep enough and return the asked sub-chapter
36
+ self . sub_chapters
37
+ . get ( section[ 0 ] )
38
+ . and_then ( |ch| ch. get_sub_chapter ( & section[ 1 ..] ) )
39
+ } ,
40
+ }
41
+ }
42
+
43
+ pub fn title ( & self ) -> & str {
44
+ & self . title
45
+ }
46
+ pub fn file ( & self ) -> & path:: Path {
47
+ & self . file
48
+ }
49
+ pub fn sub_chapters ( & self ) -> & [ Chapter ] {
50
+ & self . sub_chapters
51
+ }
52
+ }
You can’t perform that action at this time.
0 commit comments