Skip to content

Commit

Permalink
Adding FileSection structure
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 6, 2023
1 parent 869e441 commit 4a59326
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
2 changes: 1 addition & 1 deletion ecosystem/rust/cargo/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl FileGenerator for CargoGenerator {
let path = PathBuf::default();
let path = path.display().to_string().replace('\\', "/");
let content = format!(include_str!("Cargo.template.toml"), name = name, version = version, path = path);
file.writeln(content);
file.section("root").writeln(content);
Ok(())
}
}
83 changes: 70 additions & 13 deletions ligen/traits/src/generator/file_generator/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
use crate::prelude::*;
use ligen_utils::fs::write_file;
use std::path::{Path, PathBuf};
use std::collections::HashMap;
use std::collections::{HashMap, BTreeMap};

/// Structure representing a file path and its content.
#[derive(Debug, Clone, PartialEq)]
pub struct File {
/// File path.
pub path: PathBuf,
/// File content.
#[derive(Default, Debug, Clone, PartialEq)]
pub struct FileSection {
/// File section content.
pub content: String
}

impl File {
/// Creates a new file with the specified path and content.
pub fn new(path: PathBuf, content: String) -> Self {
Self { path, content }
impl FileSection {
/// Creates a new FileSection.
pub fn new() -> Self {
Self::default()
}

/// Writes the content to the file buffer.
Expand All @@ -29,11 +26,57 @@ impl File {
pub fn writeln<S: AsRef<str>>(&mut self, content: S) {
self.content.push_str(content.as_ref());
self.content.push('\n');
}
}

/// Structure representing a file path and its content.
#[derive(Debug, Clone, PartialEq)]
pub struct File {
/// File path.
pub path: PathBuf,
/// File sections.
pub sections: BTreeMap<String, FileSection>,
}

impl File {
/// Creates a new file with the specified path and content.
pub fn new(path: impl AsRef<std::path::Path>) -> Self {
let sections = Default::default();
let path = path.as_ref().to_path_buf();
Self { path, sections }
}

/// Gets or creates a new section with the specified name.
pub fn section(&mut self, name: impl AsRef<str>) -> &mut FileSection {
self
.sections
.entry(name.as_ref().to_string())
.or_insert_with(Default::default)
}

/// Gets content.
pub fn content(&self) -> String {
let mut content = String::new();
for section in self.sections.values() {
content.push_str(&section.content);
}
content
}

/// Gets content in order.
pub fn content_in_order<S: AsRef<str>, A: AsRef<[S]>>(&self, order: A) -> String {
let mut content = String::new();
for section in order.as_ref() {
if let Some(section) = self.sections.get(section.as_ref()) {
content.push_str(&section.content);
}
}
content
}

/// Saves the file.
pub fn save(&self) -> Result<()> {
write_file(&self.path, &self.content)
write_file(&self.path, &self.content())
}
}

Expand All @@ -56,6 +99,20 @@ impl FileSet {

/// Returns an existing File assigned to an entry or creates a new one if it isn't present.
pub fn entry(&mut self, path: &Path) -> &mut File {
self.files.entry(path.to_path_buf()).or_insert(File::new(path.to_path_buf(), Default::default()))
self.files.entry(path.to_path_buf()).or_insert(File::new(path.to_path_buf()))
}
}

#[cfg(test)]
mod tests {
use super::File;

#[test]
fn order() {
let mut file = File::new("path");
file.section("b").write("B");
file.section("a").write("A");
assert_eq!(file.content(), "AB");
assert_eq!(file.content_in_order(["b", "a"]), "BA");
}
}
4 changes: 2 additions & 2 deletions ligen/traits/src/generator/file_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub trait FileGenerator {
.join(self.base_path());
let library_dir = target_ligen_dir.join(library.identifier.clone().to_string());
for (_path, file) in file_set.files {
let file_path = library_dir.join(file.path);
write_file(&file_path, &file.content)?;
let file_path = library_dir.join(&file.path);
write_file(&file_path, &file.content())?;
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait TemplateBasedGenerator: TemplateRegister {

let path = self.module_generation_path(library, module);

file_set.entry(&path).writeln(content);
file_set.entry(&path).section("root").writeln(content);
for module in &module.modules {
self.generate_module(library, module, file_set, template)?;
}
Expand Down

0 comments on commit 4a59326

Please sign in to comment.