~calmbit/suki

2443711b8e4effb6352572918eb7fcaa1d6fe5e6 — CalmBit 6 months ago 238c1b6
add SukiFile#serialize()
2 files changed, 42 insertions(+), 5 deletions(-)

M src/main.rs
M src/suki.rs
M src/main.rs => src/main.rs +6 -3
@@ 1,6 1,5 @@
const VERSION: &'static str = env!("CARGO_PKG_VERSION");


mod suki;

fn main() -> Result<(), String> {


@@ 44,10 43,14 @@ fn tag(filename: &str, tags: &[String]) -> Result<(), String> {
    let dir = curr_dir();
    println!("file: {}, tags: {:?}", filename, tags);

    let file = suki::SukiFile::new(&dir);
    let file = suki::SukiFile::new(&dir)?;

    println!("{:?}", file);
    Ok(())


    file.serialize("contrib")

    //Ok(())
}

fn print_version() {

M src/suki.rs => src/suki.rs +36 -2
@@ 1,5 1,5 @@
use std::error::Error;
use std::io::Read;
use std::io::{Read, Write};

/// SukiTags represent the tag -> filename collections that are contained by
/// SukiFiles.


@@ 17,6 17,29 @@ pub struct SukiFile {
}

impl SukiFile {
    pub fn serialize(self, path: &str) -> Result<(), String> {
        let mut file = match open_and_clear_suki(path) {
            Ok(f) => f,
            Err(e) => return Err(String::from(e.description())) 
        };

        for tag in self.tags {
            match write!(file, "{}:\n", tag.tag) {
                Ok(_) => (),
                Err(e) => return Err(String::from(e.description()))
            };

            for f in tag.files {
                match write!(file, "\t{}\n", f) {
                    Ok(_) => (),
                    Err(e) => return Err(String::from(e.description()))
                }; 
            }
        }

        Ok(())
    }

    pub fn new(path: &str) -> Result<SukiFile, String> {

        let mut file_buffer = SukiFile { tags: Vec::new() };


@@ 61,7 84,8 @@ impl SukiFile {
                // Set up the brand new tag with its label, and an empty vector
                // for its filenames.
                tag_buffer = Some(SukiTag {
                    tag: String::from(l),
                    // Right here we cut off the colon at the end of the line.
                    tag: String::from(&l[..l.len()-1]),
                    files: Vec::new(),
                });
                continue;


@@ 97,6 121,16 @@ fn open_suki(path: &str) -> std::io::Result<std::fs::File> {
        .open(std::path::Path::new(&path))
}

fn open_and_clear_suki(path: &str) -> std::io::Result<std::fs::File> {
    let path = suki_path(path);
    std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .truncate(true)
        .create(true)
        .open(std::path::Path::new(&path))
}

/// A convienince function for divining the actual literal path of a suki file
/// in a directory.
///