-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77f216b
commit b7e0461
Showing
9 changed files
with
86 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::conditions::{ConditionsConfig, MainConfig}; | ||
use crate::error::Result; | ||
use std::path::Path; | ||
use tokio::{ | ||
fs, | ||
io::{self, AsyncReadExt, AsyncWriteExt}, | ||
}; | ||
|
||
pub(crate) async fn read_file<P>(file_path: P) -> io::Result<String> | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
let mut file = fs::File::open(file_path).await?; | ||
let mut content = String::new(); | ||
file.read_to_string(&mut content).await?; | ||
Ok(content) | ||
} | ||
|
||
async fn write_json_to<T>(target_path: impl AsRef<Path>, value: &T) -> Result<()> | ||
where | ||
T: ?Sized + serde::Serialize, | ||
{ | ||
let mut config_file = fs::File::create(target_path).await?; | ||
let json = serde_json::to_string_pretty(value)?; | ||
config_file.write_all(json.as_bytes()).await?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) async fn write_section_config<P>(oar_dir: P, config_json: ConditionsConfig) -> Result<()> | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
write_json_to(oar_dir.as_ref().join("config.json"), &config_json).await | ||
} | ||
|
||
/// If it exists, do nothing. (This behavior is intended to facilitate the creation of config files | ||
/// for 1st_person and 3rd_person.) | ||
pub(crate) async fn write_name_space_config<P>( | ||
oar_name_space_path: P, | ||
mod_name: &str, | ||
author: Option<&str>, | ||
) -> Result<()> | ||
where | ||
P: AsRef<Path>, | ||
{ | ||
let target_file = oar_name_space_path.as_ref().join("config.json"); | ||
if target_file.exists() { | ||
return Ok(()); | ||
} | ||
|
||
let config_json = MainConfig { | ||
name: mod_name, | ||
author: author.unwrap_or_default(), | ||
..Default::default() | ||
}; | ||
fs::create_dir_all(&oar_name_space_path).await?; | ||
write_json_to(target_file, &config_json).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters