Start reading the bundle
This commit is contained in:
parent
e16fef2b14
commit
1c3d0711e1
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -2414,6 +2414,7 @@ dependencies = [
|
||||
name = "l10n-db"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
"icu_locid",
|
||||
"serde 1.0.218",
|
||||
|
@ -4,6 +4,7 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
chrono = { version = "0.4.39", features = ["serde"] }
|
||||
clap = { version = "4.5.30", features = ["derive"] }
|
||||
icu_locid = { version = "1.5.0", features = ["serde"] }
|
||||
serde = { version = "1.0.218", features = ["derive"] }
|
||||
|
@ -1,7 +1,7 @@
|
||||
key = "OpenSandbox"
|
||||
description = "a basic description without any content"
|
||||
description = "A sandbox vault refers to a stock vault that contains test data and allows the user to make edits and run experiments on test data."
|
||||
|
||||
[variants.en-US]
|
||||
locale = "en-US"
|
||||
content = ""
|
||||
modified = 1740241312
|
||||
content = "Open Sandbox vault"
|
||||
modified = "2025-02-22T21:35:02.032300406Z"
|
||||
|
@ -31,34 +31,7 @@ enum Commands {
|
||||
|
||||
fn edit_key(bundle: &mut Bundle, key: String, locale: LanguageIdentifier, editor: &str) {
|
||||
let message = bundle.message(key);
|
||||
|
||||
Editor::edit(message, locale, editor);
|
||||
|
||||
println!("final version");
|
||||
println!("{}", toml::to_string(&message).unwrap());
|
||||
|
||||
/*
|
||||
let mut editor_file = tempfile::NamedTempFile::new().unwrap();
|
||||
let _ = editor_file.write(toml::to_string(message).unwrap().as_bytes());
|
||||
let _ = editor_file.flush();
|
||||
let mut cmd = Command::new(editor).args([editor_file.path()]).spawn().unwrap();
|
||||
cmd.wait().unwrap();
|
||||
let _ = editor_file.flush();
|
||||
let mut reader = BufReader::new(editor_file);
|
||||
let mut content = Vec::new();
|
||||
let _ = reader.read_to_end(&mut content);
|
||||
|
||||
println!("content");
|
||||
println!("{}", String::from_utf8(content).unwrap());
|
||||
|
||||
|
||||
println!("message: {:?}", message);
|
||||
|
||||
message.set_description("Open a sandbox".to_owned());
|
||||
let variant = message.variant_mut(langid!("en-US"));
|
||||
variant.set_content("Open a sandbox vault".to_owned());
|
||||
*/
|
||||
|
||||
bundle.save();
|
||||
}
|
||||
|
||||
@ -72,7 +45,11 @@ fn main() {
|
||||
|
||||
match &cli.command {
|
||||
Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), langid!("en-US"), &editor),
|
||||
Some(Commands::ListKeys) => todo!(),
|
||||
Some(Commands::ListKeys) => {
|
||||
for (key, _) in bundle.message_iter() {
|
||||
println!("{}", key);
|
||||
}
|
||||
},
|
||||
Some(Commands::Export{..}) => todo!(),
|
||||
None => {},
|
||||
}
|
||||
|
@ -9,10 +9,22 @@ pub struct Bundle {
|
||||
|
||||
impl Bundle {
|
||||
pub fn load_from_disk(path: PathBuf) -> Self {
|
||||
Self {
|
||||
path,
|
||||
messages: HashMap::new(),
|
||||
let mut messages = HashMap::new();
|
||||
if path.is_dir() {
|
||||
for entry in std::fs::read_dir(&path).unwrap() {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path().clone();
|
||||
let key = path.file_stem().unwrap();
|
||||
|
||||
let message = Message::from_file(&entry.path());
|
||||
messages.insert(key.to_str().unwrap().to_owned(), message);
|
||||
}
|
||||
}
|
||||
Self { path, messages }
|
||||
}
|
||||
|
||||
pub fn message_iter(&self) -> impl Iterator<Item = (&String, &Message)> {
|
||||
self.messages.iter()
|
||||
}
|
||||
|
||||
pub fn message(&mut self, name: String) -> &mut Message {
|
||||
|
@ -1,7 +1,8 @@
|
||||
use std::{collections::HashMap, time::{SystemTime, UNIX_EPOCH}};
|
||||
use std::{collections::HashMap, io::{BufReader, Read}, path::Path};
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use icu_locid::LanguageIdentifier;
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Message {
|
||||
@ -19,6 +20,14 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_file(path: &Path) -> Message {
|
||||
let file = std::fs::File::open(path).unwrap();
|
||||
let mut content = Vec::new();
|
||||
let mut reader = BufReader::new(file);
|
||||
let _ = reader.read_to_end(&mut content);
|
||||
toml::from_str(&String::from_utf8(content).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
pub fn set_description(&mut self, desc: String) {
|
||||
self.description = desc;
|
||||
}
|
||||
@ -31,7 +40,7 @@ impl Message {
|
||||
self.variants.entry(locale.clone()).or_insert(Variant {
|
||||
locale,
|
||||
content: "".to_owned(),
|
||||
modified: SystemTime::now(),
|
||||
modified: Utc::now(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -40,8 +49,7 @@ impl Message {
|
||||
pub struct Variant {
|
||||
locale: LanguageIdentifier,
|
||||
content: String,
|
||||
#[serde(serialize_with = "time_to_number")]
|
||||
modified: SystemTime,
|
||||
modified: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl Variant {
|
||||
@ -51,11 +59,21 @@ impl Variant {
|
||||
|
||||
pub fn set_content(&mut self, content: String) {
|
||||
self.content = content;
|
||||
self.modified = SystemTime::now();
|
||||
self.modified = Utc::now();
|
||||
}
|
||||
}
|
||||
|
||||
fn time_to_number<S>(time: &SystemTime, s: S) -> Result<S::Ok, S::Error>
|
||||
/*
|
||||
fn time_to_number<S>(time: &DateTime, s: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer {
|
||||
s.serialize_u64(time.duration_since(UNIX_EPOCH).unwrap().as_secs())
|
||||
let seconds: u64 = time.as_secs();
|
||||
s.serialize_u64(seconds)
|
||||
}
|
||||
|
||||
fn number_to_time<'de, D>(d: D) -> Result<DateTime, D::Error>
|
||||
where D: Deserializer<'de> {
|
||||
let buf = String::deserialize(d)?;
|
||||
let num = buf.parse::<u64>().unwrap();
|
||||
Ok(DateTime::try_from(num).unwrap())
|
||||
}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user