Skip to content

Commit 9ee17fc

Browse files
authored
Support for storing binary files (#7)
* feat: storing binary files * fix: readme
1 parent 37f1a39 commit 9ee17fc

File tree

4 files changed

+122
-9
lines changed

4 files changed

+122
-9
lines changed

Cargo.lock

Lines changed: 93 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ exclude = ["cra.json", "template.json"]
1717
[dependencies]
1818
clap = "2.33.3"
1919
serde = { version = "1.0.126", features = ["derive"] }
20+
serde_with = { version = "1.11.0", features = ["base64"] }
2021
serde_json = "1.0"
2122
tempdir = "0.3.7"

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,3 @@ $ ./path/to/dgen-rs -r <Username or Organization>/<Repository Name>
7171
```
7272

7373
# Known Issues
74-
75-
- Currently it only stores files with valid UTF-8 content. Which basically means it does not store image, audio, video content and binaries. So far, I am thinking of storing images as base64 strings because some starter repos might have images.

src/main.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
use clap::{App, Arg};
22
use serde::{Deserialize, Serialize};
3+
use serde_with::base64::Base64;
4+
use serde_with::serde_as;
35
use std::env;
46
use std::fs;
57
use std::io::{prelude::*, Error as IOError, ErrorKind as IOErrorKind};
68
use std::path::Path;
79
use std::process::Command;
810
use tempdir::TempDir;
911

12+
#[serde_as]
13+
#[derive(Serialize, Deserialize, Debug)]
14+
enum FileContent {
15+
#[serde(rename = "content")]
16+
Text(String),
17+
#[serde(rename = "binary-content")]
18+
Binary(#[serde_as(as = "Base64")] Vec<u8>),
19+
}
20+
1021
#[derive(Serialize, Deserialize, Debug)]
1122
struct FileNode {
1223
name: String,
13-
content: String,
24+
#[serde(flatten)]
25+
content: FileContent,
1426
}
1527

1628
// The core data structure
@@ -33,7 +45,13 @@ fn dir_from_json(folder: &FolderNode, path: String) -> Result<(), IOError> {
3345
folder.files.iter().for_each(|file| {
3446
println!("Generating new file: {}{}", &new_path, &file.name);
3547
let mut file_handler = fs::File::create(format!("{}{}", &new_path, &file.name)).unwrap();
36-
let _ = file_handler.write_all(file.content.as_bytes()).unwrap();
48+
49+
let bytes_to_write = match &file.content {
50+
FileContent::Text(text) => text.as_bytes(),
51+
FileContent::Binary(binary) => binary,
52+
};
53+
54+
let _ = file_handler.write_all(bytes_to_write).unwrap();
3755
});
3856

3957
// base case [TODO: also handle if folder key does not exist]
@@ -70,10 +88,14 @@ fn json_from_dir(folder: &mut FolderNode, path: String) {
7088
json_from_dir(&mut f, new_path.clone());
7189
folder.folders.push(f);
7290
} else {
73-
if fs::read_to_string(path.path()).is_err() {
91+
let file_content = if let Ok(text) = fs::read_to_string(path.path()) {
92+
FileContent::Text(text)
93+
} else if let Ok(binary) = fs::read(path.path()) {
94+
FileContent::Binary(binary)
95+
} else {
7496
continue;
75-
}
76-
let file_content = fs::read_to_string(path.path()).unwrap();
97+
};
98+
7799
folder.files.push(FileNode {
78100
name: path
79101
.path()
@@ -82,7 +104,7 @@ fn json_from_dir(folder: &mut FolderNode, path: String) {
82104
.to_string_lossy()
83105
.to_string(),
84106
content: file_content,
85-
})
107+
});
86108
}
87109
}
88110
}

0 commit comments

Comments
 (0)