ch8alib/util/
conv_filename.rs

1/*
2 * conv_filename.rs
3 * Defines a function that converts a filename from .c8a to .c8
4 * Created on 12/21/2019
5 * Created by Andrew Davis
6 *
7 * Copyright (C) 2019  Andrew Davis
8 *
9 * This program is free software: you can redistribute it and/or modify   
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 * 
19 * You should have received a copy of the GNU General Public License
20 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23//usage statements
24use std::ffi::OsString;
25use std::path::Path;
26
27/// Converts a filename extension from source (`.c8a`) to binary (`.c8`) 
28///
29/// # Argument
30///
31/// * `fname` - The filename to convert
32/// 
33/// # Returns
34///
35/// The argument filename with the extension `.c8`
36pub fn conv_filename(fname: &str) -> String {
37    //create the path
38    let path = Path::new(fname);
39
40    //get the filename without the extension
41    let tmp = OsString::from(path.file_stem().unwrap());
42    let mut ret = tmp.into_string().unwrap();
43
44    //append the new extension
45    ret.push_str(".c8");
46
47    //and return the final filename
48    return ret;
49}
50
51//unit tests
52#[cfg(test)]
53mod tests {
54    //import the conv_filename function
55    use super::*;
56
57    //this test checks filename conversion
58    #[test]
59    fn test_filename_conversion() {
60        let fname = "hello.c8a";
61        let fnc = conv_filename(fname);
62        assert_eq!(fnc.as_str(), "hello.c8");
63    }
64}
65
66//end of file