use std::{fs::File, io::Write};
use super::{{super::super::Error}, *};

pub fn write(file: File, data: Vec<Specifier>) -> Result<(), Error> {
    write_generic(file, data)
}

pub fn write_generic<T: Write>(mut dst: T, data: Vec<Specifier>) -> Result<(), Error> {
    fn write_indent<T: Write>(dst: &mut T, specifier: &Specifier) -> Result<(), Error> {
        let indent = {
            match specifier {
                Specifier::File{..}  => 0,
                Specifier::Track{..} => 2,
                _                    => 4,
            }
        };

        if indent > 0 {
            write!(dst, "{:indent$}", " ")?;
        }
        Ok(())
    }

    for specifier in data {
        write_indent(&mut dst, &specifier)?;
        
        write!(dst, "{}\n", specifier)?;
    }

    Ok(())
}