1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
use std::fs;
use std::error::Error;
use chrono::{Datelike, Timelike, Utc};
///It is a method to aid memory for me.
type Result<String> = ::std::result::Result<String, Box<dyn Error>>;
///Read string from file.
/// Example:
///
/// ```rust
/// # use std::error::Error;
/// # use once_cell::sync::OnceCell;
/// # use toml::Value;
/// # use jay_lib::fns::fn_io::f_string;
/// # type Result<> = ::std::result::Result<(), Box<dyn Error>>;
/// # pub static DB_LOCATION: OnceCell<String> = OnceCell::new();
/// # pub static DICTIONARY_FROM: OnceCell<String> = OnceCell::new();
/// # pub static RUST_FROM: OnceCell<String> = OnceCell::new();
/// #
/// # pub fn set_conf() -> Result<> {
/// let mut s = String::from("");
/// match f_string("./config.toml") {
/// Ok(st) => s = st,
/// Err(e) => eprintln!("{e}"),
/// }
/// # let toml_info: Value = toml::from_str(&s)?;
/// # if let Some(from) = toml_info["data"]["dictionary_from"].as_str() {
/// # DICTIONARY_FROM.get_or_init(|| from.to_string());
/// # }
/// # if let Some(tidy) = toml_info["data"]["rust_from"].as_str() {
/// # RUST_FROM.get_or_init(|| tidy.to_string());
/// # }
/// # if let Some(location) = toml_info["db"]["location"].as_str() {
/// # DB_LOCATION.get_or_init(|| location.to_string());
/// # }
/// # Ok(())
/// }
/// ```
pub fn f_string<P: AsRef<str>>(s: P) -> Result<String> {
let r = fs::read_to_string(s.as_ref())?;
Ok(r)
}
///get a file name from today.
/// Often use for temporary file.
///```rust
/// # use std::fs;
/// # use jay_lib::fns::fn_io::get_file_date_name;
/// # fn main()->std::io::Result<()> {
/// let p=get_file_date_name();
/// let s="12345";
/// fs::write(p, s)?;
/// # Ok(())
/// # }
/// ```
pub fn get_file_date_name() -> String {
let mut r: String =String::new();
let now = Utc::now();
r.push_str(&now.year().to_string());
r.push_str(&now.month().to_string());
r.push_str(&now.day().to_string());
r
}
///get a file name from now.
/// it is year month day hour and minute.
/// Often use for temporary file.
///```rust
/// # use std::fs;
/// # use jay_lib::fns::fn_io::get_file_time_name;
/// # fn main()->std::io::Result<()> {
/// let p=get_file_time_name();
/// let s="12345";
/// fs::write(p, s)?;
/// # Ok(())
/// # }
///
pub fn get_file_time_name() -> String {
let mut r= String::new();
let now = Utc::now();
let add_str = now.year().to_string()
+ &now.month().to_string()
+ &now.day().to_string()
+ &now.hour().to_string()
+ &now.minute().to_string();
r.push_str(&add_str);
r
}
///writen `Vec<String>` to a temporary file.
/// It to check temporary data.
///```no_run
/// use jay_lib::fns::fn_io::v_f;
/// let v=["1".to_string(),"2".to_string(),"3".to_string()];
/// if let Err(_) = v_f(v.to_vec()) {
/// eprintln!("Save v to file has some err");
/// }
/// ```
pub fn v_f(v: Vec<String>) -> std::io::Result<()> {
let mut s = String::from("");
for i in v {
s.push_str(&i);
s.push('\n');
}
let mut p = ::std::string::String::from("./t/");
p.push_str(&get_file_time_name());
fs::write(p, s)?;
Ok(())
}
///Writen a String to a file.
///```no_run
///
/// use jay_lib::fns::fn_io::s_f;
/// //s is a sql string.
/// let s="Inst into xxxx".to_string();
/// if let Err(_) = s_f(s) {
/// eprintln!("Save v to file has some err");
/// }
/// ```
pub fn s_f(s:String) -> std::io::Result<()> {
let mut p = ::std::string::String::from("./t/");
p.push_str(&get_file_time_name());
fs::write(p, s)?;
Ok(())
}