aboutsummaryrefslogtreecommitdiff
path: root/src/backend/bib.rs
blob: c5203952dbfa0963ce034141f34454e79e5852af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// bibiman - a TUI for managing BibLaTeX databases
// Copyright (C) 2024  lukeflo
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
/////

use super::cliargs::PosArgs;
use std::{fs, path::PathBuf};

use biblatex::{self, Bibliography};
use biblatex::{ChunksExt, Type};

// Set necessary fields
// TODO: can surely be made more efficient/simpler
pub struct BibiMain {
    pub bibfile: PathBuf,           // path to bibfile
    pub bibfilestring: String,      // content of bibfile as string
    pub bibliography: Bibliography, // parsed bibliography
    pub citekeys: Vec<String>,      // list of all citekeys
                                    // pub bibentries: BibiDataSets,
}

impl BibiMain {
    pub fn new() -> Self {
        // TODO: Needs check for config file path as soon as config file is impl
        let bibfile = PosArgs::parse_pos_args().bibfilearg;
        let bibfilestring = fs::read_to_string(&bibfile).unwrap();
        let bibliography = biblatex::Bibliography::parse(&bibfilestring).unwrap();
        let citekeys = Self::get_citekeys(&bibliography);
        Self {
            bibfile,
            bibfilestring,
            bibliography,
            citekeys,
        }
    }

    // get list of citekeys from the given bibfile
    // this list is the base for further operations on the bibentries
    // since it is the entry point of the biblatex crate.
    pub fn get_citekeys(bibstring: &Bibliography) -> Vec<String> {
        let mut citekeys: Vec<String> =
            bibstring.iter().map(|entry| entry.to_owned().key).collect();
        citekeys.sort_by_key(|name| name.to_lowercase());
        citekeys
    }
}

#[derive(Debug)]
pub struct BibiData {
    pub entry_list: BibiDataSets,
}

impl BibiData {
    pub fn new(biblio: &Bibliography, citekeys: &Vec<String>) -> Self {
        Self {
            entry_list: {
                let bibentries = citekeys
                    .into_iter()
                    .map(|citekey| BibiEntry::new(&citekey, &biblio))
                    .collect();
                BibiDataSets { bibentries }
            },
        }
    }
}

// Parent struct which keeps the Vector of all bibentries
// Necessary for implementing FromIterator
#[derive(Debug)]
pub struct BibiDataSets {
    pub bibentries: Vec<Vec<String>>,
}

// Struct which has to be created for every entry of bibdatabase
#[derive(Debug)]
pub struct BibiEntry {
    pub authors: String,
    pub title: String,
    pub year: String,
    pub pubtype: String,
    // pub keywords: Vec<String>,
    pub citekey: String,
}

impl BibiEntry {
    pub fn new(citekey: &str, biblio: &Bibliography) -> Vec<String> {
        vec![
            Self::get_authors(&citekey, &biblio),
            Self::get_title(&citekey, &biblio),
            Self::get_year(&citekey, &biblio),
            Self::get_pubtype(&citekey, &biblio),
            citekey.to_string(),
        ]
    }

    fn get_authors(citekey: &str, biblio: &Bibliography) -> String {
        let authors = {
            if biblio.get(&citekey).unwrap().author().is_ok() {
                let authors = biblio.get(&citekey).unwrap().author().unwrap();
                if authors.len() > 1 {
                    let authors = format!("{} et al.", authors[0].name);
                    authors
                } else if authors.len() == 1 {
                    let authors = authors[0].name.to_string();
                    authors
                } else {
                    let editors_authors = format!("empty");
                    editors_authors
                }
            } else {
                if biblio.get(&citekey).unwrap().editors().is_ok() {
                    let editors = biblio.get(&citekey).unwrap().editors().unwrap();
                    if editors.len() > 1 {
                        let editors = format!("{} (ed.) et al.", editors[0].0[0].name);
                        editors
                    } else if editors.len() == 1 {
                        let editors = format!("{} (ed.)", editors[0].0[0].name);
                        editors
                    } else {
                        let editors_authors = format!("empty");
                        editors_authors
                    }
                } else {
                    let editors_authors = format!("empty");
                    editors_authors
                }
            }
        };
        authors
    }

    fn get_title(citekey: &str, biblio: &Bibliography) -> String {
        let title = {
            if biblio.get(&citekey).unwrap().title().is_ok() {
                let title = biblio
                    .get(&citekey)
                    .unwrap()
                    .title()
                    .unwrap()
                    .format_verbatim();
                title
            } else {
                let title = format!("no title");
                title
            }
        };
        title
    }

    fn get_year(citekey: &str, biblio: &Bibliography) -> String {
        let year = biblio.get(&citekey).unwrap();
        let year = {
            if year.date().is_ok() {
                let year = year.date().unwrap().to_chunks().format_verbatim();
                let year = year[..4].to_string();
                year
            } else {
                let year = format!("n.d.");
                year
            }
        };
        year
    }

    fn get_pubtype(citekey: &str, biblio: &Bibliography) -> String {
        let pubtype = biblio.get(&citekey).unwrap().entry_type.to_string();
        pubtype
    }

    fn get_keywords(citekey: &str, biblio: &Bibliography) -> String {
        let keywords = biblio
            .get(&citekey)
            .unwrap()
            .keywords()
            .unwrap()
            .format_verbatim();
        keywords
    }
}