glowdust 0.0.1

A DBMS with a data model based on functions and pattern matching
Documentation
mod uring;

use crate::compiler::cursor::{FunctionCursor, FunctionCursorMethods};
use crate::compiler::value::parameters::Parameter;
use crate::compiler::value::values::{FunctionDefinitionObject, TypeDefinition, Value};
use crate::store::Store;
use std::fs::{File, OpenOptions};
use std::io::{BufReader, Error, Seek, SeekFrom};
use std::path::PathBuf;

pub(crate) struct CsvInput {
    function_name: String,
    file_name: PathBuf,
}

impl CsvInput {
    pub fn new(function_name: &str, file_name: PathBuf) -> Self {
        CsvInput {
            function_name: function_name.to_string(),
            file_name,
        }
    }
}

impl Store for CsvInput {
    fn is_function_name_defined(&self, name: &str) -> Result<bool, Error> {
        Ok(name.eq(&self.function_name))
    }

    fn get_function_value(&self, _name: &str, _argument: &[Value]) -> Result<Option<Value>, Error> {
        todo!()
    }

    fn set_function_value(
        &mut self,
        _name: &str,
        _argument: &[Value],
        _value: Value,
    ) -> Result<(), Error> {
        panic!("CSV import sources are not writeable")
    }

    fn get_function_definition(
        &self,
        _name: &str,
        _argument: &[Value],
    ) -> Result<Option<FunctionDefinitionObject>, Error> {
        panic!("Function definitions not supported in CSV files yet")
    }

    fn set_function_definition(
        &mut self,
        _name: &str,
        _parameters: &[Parameter],
        _function: FunctionDefinitionObject,
    ) -> Result<(), Error> {
        panic!("Function definitions not supported in CSV files yet")
    }

    fn function_cursor(&self, _name: &str) -> FunctionCursor {
        let file = OpenOptions::new()
            .create(true)
            .read(true)
            .open(self.file_name.clone())
            .unwrap();

        let reader = BufReader::new(file);
        FunctionCursor::Csv(CsvFunctionValueCursor { reader })
    }

    fn has_values(&self, _name: &str) -> bool {
        todo!()
    }

    fn domain_arity(&self, _name: &str) -> Option<u8> {
        todo!()
    }

    fn codomain_arity(&self, _name: &str) -> Option<u8> {
        todo!()
    }

    fn set_type_definition(&mut self, _name: &str, _the_type: TypeDefinition) -> Result<(), Error> {
        panic!("CSV stores cannot have their type definitions set")
    }

    fn type_definition_by_name(&self, _name: &str) -> Result<Option<TypeDefinition>, Error> {
        todo!()
    }
}

#[derive(Debug)]
pub struct CsvFunctionValueCursor {
    reader: BufReader<File>,
}

impl FunctionCursorMethods for CsvFunctionValueCursor {
    fn next(&mut self) -> Option<Vec<Value>> {
        todo!()
    }

    fn reset(&mut self) {
        self.reader.seek(SeekFrom::Start(0));
    }
}