From 01a7fb5f9a9c19fd7d4c8ce0e67ce677d6c62409 Mon Sep 17 00:00:00 2001 From: Krusty/Benediction Date: Wed, 7 Aug 2024 13:33:04 +0200 Subject: [PATCH] [visualbndbuild] Fix rendering issues since it is possible to provide paths instead of files --- cpclib-bndbuild/src/builder.rs | 18 +++++++++++------- cpclib-bndbuild/src/lib.rs | 2 +- cpclib-bndbuild/tests/build_dummy.rs | 2 +- cpclib-visual-bndbuild/src/lib.rs | 7 ++++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cpclib-bndbuild/src/builder.rs b/cpclib-bndbuild/src/builder.rs index a254648d..f7657118 100644 --- a/cpclib-bndbuild/src/builder.rs +++ b/cpclib-bndbuild/src/builder.rs @@ -2,9 +2,9 @@ use std::collections::HashSet; use std::io::{BufReader, Read}; use std::ops::Deref; -use cpclib_common::camino::Utf8Path; +use cpclib_common::camino::{Utf8Path, Utf8PathBuf}; use cpclib_common::itertools::Itertools; -use minijinja::{context, path_loader, Environment, Error, ErrorKind}; +use minijinja::{context, Environment, Error, ErrorKind}; use crate::rules::{self, Graph, Rule}; use crate::BndBuilderError; @@ -21,8 +21,10 @@ self_cell::self_cell! { } } + + pub struct BndBuilder { - inner: BndBuilderInner + inner: BndBuilderInner, } impl Deref for BndBuilder { @@ -48,12 +50,13 @@ impl BndBuilder { BndBuilder { inner } } - pub fn from_fname>(fname: P) -> Result { - let content = Self::decode_from_fname(fname)?; + pub fn from_path>(fname: P) -> Result<(Utf8PathBuf, Self), BndBuilderError> { + let (p, content) = Self::decode_from_fname(fname)?; Self::from_string(content) + .map(|build| (p, build)) } - pub fn decode_from_fname>(fname: P) -> Result { + pub fn decode_from_fname>(fname: P) -> Result<(Utf8PathBuf, String), BndBuilderError> { Self::decode_from_fname_with_definitions(fname, &Vec::<(String, String)>::new()) } @@ -64,7 +67,7 @@ impl BndBuilder { >( fname: P, definitions: &[(S1, S2)] - ) -> Result { + ) -> Result<(Utf8PathBuf, String), BndBuilderError> { let fname = fname.as_ref(); // when a folder is provided try to look for a build file @@ -96,6 +99,7 @@ impl BndBuilder { let rdr = BufReader::new(file); Self::decode_from_reader(rdr, working_directory, definitions) + .map(|s| (fname.to_owned(), s)) } pub fn save>(&self, path: P) -> std::io::Result<()> { diff --git a/cpclib-bndbuild/src/lib.rs b/cpclib-bndbuild/src/lib.rs index 467deb31..d2ed4ba5 100644 --- a/cpclib-bndbuild/src/lib.rs +++ b/cpclib-bndbuild/src/lib.rs @@ -173,7 +173,7 @@ pub fn process_matches(cmd: Command, matches: &ArgMatches) -> Result<(), BndBuil Default::default() }; - let content = BndBuilder::decode_from_fname_with_definitions(fname, &definitions)?; + let (_path, content) = BndBuilder::decode_from_fname_with_definitions(fname, &definitions)?; if matches.get_flag("show") { println!("{content}"); return Ok(()); diff --git a/cpclib-bndbuild/tests/build_dummy.rs b/cpclib-bndbuild/tests/build_dummy.rs index 95ed0feb..c35ed8ce 100644 --- a/cpclib-bndbuild/tests/build_dummy.rs +++ b/cpclib-bndbuild/tests/build_dummy.rs @@ -34,7 +34,7 @@ fn test_dummy_phony() { use cpclib_common::itertools::Itertools; let builder_fname = "tests/dummy/bndbuild.yml"; - let builder = BndBuilder::from_fname(builder_fname).unwrap(); + let builder = BndBuilder::from_path(builder_fname).unwrap(); println!( "{:#?}", diff --git a/cpclib-visual-bndbuild/src/lib.rs b/cpclib-visual-bndbuild/src/lib.rs index 1f1b1a60..4409208f 100644 --- a/cpclib-visual-bndbuild/src/lib.rs +++ b/cpclib-visual-bndbuild/src/lib.rs @@ -295,10 +295,11 @@ impl BndBuildApp { pub fn load>(&mut self, path: P) { let path = path.as_ref(); - match cpclib_bndbuild::BndBuilder::from_fname(path) { - Ok(builder) => { + match cpclib_bndbuild::BndBuilder::from_path(path) { + Ok((ref path, builder)) => { self.filename = Some(path.into()); - self.file_content = std::fs::read_to_string(self.filename.as_ref().unwrap()).ok(); // read a second time, but the file exists + self.file_content = std::fs::read_to_string(path).ok().map(|s| s.replace('\r',"")); // read a second time, but the file exists + self.builder_and_layers = BuilderAndCache::from(builder).into(); if let Some(position) = self.recent_files.iter().position(|elem| elem == path) {