pub fn compile_module(
        &mut self,
        name: &str,
        load_from_path: Option<PathBuf>,
    ) -> Result<(Rc<Chunk>, PathBuf), LoaderError> {
        // Get either the directory of the provided path, or the current working directory
        let path = match &load_from_path {
            Some(path) => match canonicalize(path) {
                Ok(canonicalized) if canonicalized.is_file() => match canonicalized.parent() {
                    Some(parent_dir) => parent_dir.to_path_buf(),
                    None => {
                        return Err(LoaderError::io_error(
                            "Failed to get parent of provided path".to_string(),
                        ))
                    }
                },
                Ok(canonicalized) => canonicalized,
                Err(e) => return Err(LoaderError::io_error(e.to_string())),
            },
            None => match std::env::current_dir() {
                Ok(path) => path,
                Err(e) => return Err(LoaderError::io_error(e.to_string())),
            },
        };

        let mut load_module_from_path = |module_path: PathBuf| match self.chunks.get(&module_path) {
            Some(chunk) => Ok((chunk.clone(), module_path.clone())),
            None => match std::fs::read_to_string(&module_path) {
                Ok(script) => {
                    let chunk = self.compile(
                        &script,
                        Some(module_path.clone()),
                        CompilerSettings::default(),
                    )?;

                    self.chunks.insert(module_path.clone(), chunk.clone());
                    Ok((chunk, module_path))
                }
                Err(_) => Err(LoaderError::io_error(format!(
                    "File not found: {}",
                    module_path.to_string_lossy()
                ))),
            },
        };

        let extension = "koto";
        let named_path = path.join(name);

        // First, check for a neighbouring file with a matching name.
        let module_path = named_path.with_extension(extension);
        if module_path.exists() {
            load_module_from_path(module_path)
        } else {
            // Alternatively, check for a neighbouring directory with a matching name,
            // that also contains a main file.
            let module_path = named_path.join("main").with_extension(extension);
            if module_path.exists() {
                load_module_from_path(module_path)
            } else {
                Err(LoaderError::io_error(format!(
                    "Unable to find module '{}'",
                    name
                )))
            }
        }
    }
/// Finds a module from its name, and then compiles it
Finds a module from its name, and then compiles it
Finds a module from its name and then compiles it
pub fn compile_module(
        &mut self,
        name: &str,
        load_from_path: Option<PathBuf>,
    ) -> Result<(Rc<Chunk>, PathBuf), LoaderError> {
        // Get either the directory of the provided path, or the current working directory
        let path = match &load_from_path {
            Some(path) => match canonicalize(path) {
                Ok(canonicalized) if canonicalized.is_file() => match canonicalized.parent() {
                    Some(parent_dir) => parent_dir.to_path_buf(),
                    None => {
                        return Err(LoaderError::io_error(
                            "Failed to get parent of provided path".to_string(),
                        ))
                    }
                },
                Ok(canonicalized) => canonicalized,
                Err(e) => return Err(LoaderError::io_error(e.to_string())),
            },
            None => match std::env::current_dir() {
                Ok(path) => path,
                Err(e) => return Err(LoaderError::io_error(e.to_string())),
            },
        };
        let mut load_module_from_path = |module_path: PathBuf| match self.chunks.get(&module_path) {
            Some(chunk) => Ok((chunk.clone(), module_path.clone())),
            None => match std::fs::read_to_string(&module_path) {
                Ok(script) => {
                    let chunk = self.compile(
                        &script,
                        Some(module_path.clone()),
                        CompilerSettings::default(),
                    )?;
                    self.chunks.insert(module_path.clone(), chunk.clone());
                    Ok((chunk, module_path))
                }
                Err(_) => Err(LoaderError::io_error(format!(
                    "File not found: {}",
                    module_path.to_string_lossy()
                ))),
            },
        };
        let extension = "koto";
        let named_path = path.join(name);
        // First, check for a neighbouring file with a matching name.
        let module_path = named_path.with_extension(extension);
        if module_path.exists() {
            load_module_from_path(module_path)
        } else {
            // Alternatively, check for a neighbouring directory with a matching name,
            // that also contains a main file.
            let module_path = named_path.join("main").with_extension(extension);
            if module_path.exists() {
                load_module_from_path(module_path)
            } else {
                Err(LoaderError::io_error(format!(
                    "Unable to find module '{}'",
                    name
                )))
            }
        }
    }
pub fn compile_module ( & mut self , name : & str , load_from_path : Option < PathBuf > , ) -> Result < ( Rc < Chunk > , PathBuf ) , LoaderError > { // Get either the directory of the provided path, or the current working directory let path = match & load_from_path { Some ( path ) => match canonicalize ( path ) { Ok ( canonicalized ) if canonicalized . is_file ( ) => match canonicalized . parent ( ) { Some ( parent_dir ) => parent_dir . to_path_buf ( ) , None => { return Err ( LoaderError :: io_error ( " " . to_string ( ) , ) ) } } , Ok ( canonicalized ) => canonicalized , Err ( e ) => return Err ( LoaderError :: io_error ( e . to_string ( ) ) ) , } , None => match std :: env :: current_dir ( ) { Ok ( path ) => path , Err ( e ) => return Err ( LoaderError :: io_error ( e . to_string ( ) ) ) , } , } ; let mut load_module_from_path = | module_path : PathBuf | match self . chunks . get ( & module_path ) { Some ( chunk ) => Ok ( ( chunk . clone ( ) , module_path . clone ( ) ) ) , None => match std :: fs :: read_to_string ( & module_path ) { Ok ( script ) => { let chunk = self . compile ( & script , Some ( module_path . clone ( ) ) , CompilerSettings :: default ( ) , ) ? ; self . chunks . insert ( module_path . clone ( ) , chunk . clone ( ) ) ; Ok ( ( chunk , module_path ) ) } Err ( _ ) => Err ( LoaderError :: io_error ( format ! ( " " module_path to_string_lossy ( ) ) ) ) , } , } ; let extension = " " ; let named_path = path . join ( name ) ; // First, check for a neighbouring file with a matching name. let module_path = named_path . with_extension ( extension ) ; if module_path . exists ( ) { load_module_from_path ( module_path ) } else { // Alternatively, check for a neighbouring directory with a matching name, // that also contains a main file. let module_path = named_path . join ( " " ) . with_extension ( extension ) ; if module_path . exists ( ) { load_module_from_path ( module_path ) } else { Err ( LoaderError :: io_error ( format ! ( " " name ) ) ) } } }
Finds a module from its name, and then compiles it
Finds a module from its name and then compiles it
&mut self
name
&str
load_from_path
Option<PathBuf>
&mut self
name
&str
load_from_path
Option<PathBuf>