Write a single phrase to disk
This commit is contained in:
parent
359ab96779
commit
e0392a4150
7
l10n-db/i18n/OpenSandbox.toml
Normal file
7
l10n-db/i18n/OpenSandbox.toml
Normal file
@ -0,0 +1,7 @@
|
||||
key = "OpenSandbox"
|
||||
description = "Open a sandbox"
|
||||
|
||||
[variants.en-US]
|
||||
locale = "en-US"
|
||||
content = "Open a sandbox vault"
|
||||
modified = 1740198524
|
@ -1,6 +1,9 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use l10n_db;
|
||||
use icu_locid::{langid, LanguageIdentifier};
|
||||
use l10n_db::{self, Bundle};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
@ -11,9 +14,9 @@ struct Cli {
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
// Edit, potentially creating, a key
|
||||
// EditKey { },
|
||||
// List al keys in the database
|
||||
/// Edit, potentially creating, a key
|
||||
EditKey { name: String, locale: String },
|
||||
/// List al keys in the database
|
||||
ListKeys,
|
||||
// Search the database
|
||||
// Search { },
|
||||
@ -26,10 +29,28 @@ enum Commands {
|
||||
},
|
||||
}
|
||||
|
||||
fn edit_key(bundle: &mut Bundle, key: String, editor: &str) {
|
||||
let message = bundle.message(key);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let editor = std::env::var("EDITOR").unwrap();
|
||||
let db_path = std::env::var("DB_PATH").unwrap();
|
||||
|
||||
let cli = Cli::parse();
|
||||
|
||||
let mut bundle = Bundle::load_from_disk(PathBuf::from(&db_path));
|
||||
|
||||
match &cli.command {
|
||||
Some(Commands::EditKey{ name, locale } ) => edit_key(&mut bundle, name.to_owned(), &editor),
|
||||
Some(Commands::ListKeys) => todo!(),
|
||||
Some(Commands::Export{..}) => todo!(),
|
||||
None => {},
|
||||
|
@ -1,24 +1,40 @@
|
||||
use std::{collections::HashMap, path::Path};
|
||||
use std::{collections::HashMap, fs::File, io::Write, path::PathBuf};
|
||||
|
||||
use crate::Message;
|
||||
|
||||
pub struct Bundle {
|
||||
messages: HashMap<String, Message>
|
||||
path: PathBuf,
|
||||
messages: HashMap<String, Message>,
|
||||
}
|
||||
|
||||
impl Bundle {
|
||||
pub fn new() -> Self {
|
||||
Self{ messages: HashMap::new() }
|
||||
}
|
||||
|
||||
pub fn load_from_disk(path: &Path) -> Self {
|
||||
pub fn load_from_disk(path: PathBuf) -> Self {
|
||||
Self {
|
||||
messages: HashMap::new()
|
||||
path,
|
||||
messages: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn message(&mut self, name: String) -> &mut Message {
|
||||
self.messages
|
||||
.entry(name.to_owned())
|
||||
.or_insert(Message::new(name.to_owned()))
|
||||
}
|
||||
|
||||
pub fn save(&self) {
|
||||
self.messages.iter().for_each(|(key, value)| {
|
||||
let mut path = self.path.clone();
|
||||
path.push(key);
|
||||
path.set_extension("toml");
|
||||
save_file(&path, toml::to_string(value).unwrap().as_bytes());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn save_file(path: &PathBuf, s: &[u8]) {
|
||||
let mut f = File::create(path).unwrap();
|
||||
f.write(s).unwrap();
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
}
|
||||
mod test {}
|
||||
|
@ -1,9 +1,9 @@
|
||||
use std::{collections::HashMap, time::SystemTime};
|
||||
use std::{collections::HashMap, time::{SystemTime, UNIX_EPOCH}};
|
||||
|
||||
use icu_locid::LanguageIdentifier;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::{Deserialize, Serialize, Serializer};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Message {
|
||||
key: String,
|
||||
description: String,
|
||||
@ -36,11 +36,22 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Variant {
|
||||
locale: LanguageIdentifier,
|
||||
content: String,
|
||||
#[serde(serialize_with = "time_to_number")]
|
||||
modified: SystemTime,
|
||||
}
|
||||
|
||||
impl Variant {
|
||||
pub fn set_content(&mut self, content: String) {
|
||||
self.content = content;
|
||||
self.modified = SystemTime::now();
|
||||
}
|
||||
}
|
||||
|
||||
fn time_to_number<S>(time: &SystemTime, s: S) -> Result<S::Ok, S::Error>
|
||||
where S: Serializer {
|
||||
s.serialize_u64(time.duration_since(UNIX_EPOCH).unwrap().as_secs())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user