Compare commits

...

2 Commits

Author SHA1 Message Date
e16fef2b14 Write a rudimentary editor 2025-02-22 11:22:14 -05:00
e0392a4150 Write a single phrase to disk 2025-02-22 10:23:46 -05:00
8 changed files with 191 additions and 24 deletions

43
Cargo.lock generated
View File

@ -1497,6 +1497,18 @@ dependencies = [
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8"
dependencies = [
"cfg-if",
"libc",
"wasi 0.13.3+wasi-0.2.2",
"windows-targets 0.52.6",
]
[[package]]
name = "gif"
version = "0.11.4"
@ -2405,6 +2417,7 @@ dependencies = [
"clap",
"icu_locid",
"serde 1.0.218",
"tempfile",
"toml",
]
@ -3502,7 +3515,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
"getrandom 0.2.15",
]
[[package]]
@ -4404,13 +4417,13 @@ dependencies = [
[[package]]
name = "tempfile"
version = "3.15.0"
version = "3.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8a559c81686f576e8cd0290cd2a24a2a9ad80c98b3478856500fcbd7acd704"
checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230"
dependencies = [
"cfg-if",
"fastrand",
"getrandom",
"getrandom 0.3.1",
"once_cell",
"rustix",
"windows-sys 0.59.0",
@ -4968,7 +4981,7 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom",
"getrandom 0.2.15",
"serde 1.0.218",
]
@ -4978,7 +4991,7 @@ version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "744018581f9a3454a9e15beb8a33b017183f1e7c0cd170232a2d1453b23a51c4"
dependencies = [
"getrandom",
"getrandom 0.2.15",
]
[[package]]
@ -5070,6 +5083,15 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasi"
version = "0.13.3+wasi-0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wasite"
version = "0.1.0"
@ -5389,6 +5411,15 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.33.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c"
dependencies = [
"bitflags 2.8.0",
]
[[package]]
name = "write16"
version = "1.0.0"

View File

@ -7,6 +7,7 @@ edition = "2021"
clap = { version = "4.5.30", features = ["derive"] }
icu_locid = { version = "1.5.0", features = ["serde"] }
serde = { version = "1.0.218", features = ["derive"] }
tempfile = "3.17.1"
toml = "0.8.20"
# [lib]

View File

@ -0,0 +1,7 @@
key = "OpenSandbox"
description = "a basic description without any content"
[variants.en-US]
locale = "en-US"
content = ""
modified = 1740241312

View File

@ -1,6 +1,9 @@
use std::{io::{BufReader, Read, Write}, path::PathBuf, process::Command};
use clap::{Parser, Subcommand};
use l10n_db;
use icu_locid::{langid, LanguageIdentifier};
use l10n_db::{self, Bundle, Editor};
#[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,49 @@ 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();
}
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(), langid!("en-US"), &editor),
Some(Commands::ListKeys) => todo!(),
Some(Commands::Export{..}) => todo!(),
None => {},

View File

@ -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 {}

52
l10n-db/src/editor.rs Normal file
View File

@ -0,0 +1,52 @@
use std::{io::{BufReader, Read, Write}, path::Path, process::Command};
use icu_locid::LanguageIdentifier;
use serde::{Deserialize, Serialize};
use crate::{Message, Variant};
#[derive(Serialize, Deserialize, Debug, Clone)]
struct EditorMessage {
description: String,
content: String,
}
impl EditorMessage {
fn from_variant(description: String, variant: &Variant) -> Self {
Self {
description,
content: variant.content().to_string()
}
}
}
pub struct Editor {
}
impl Editor {
pub fn edit(msg: &mut Message, locale: LanguageIdentifier, editor: &str) {
let description = msg.description().to_owned();
let variant = msg.variant_mut(locale);
let editable_content = EditorMessage::from_variant(description, &variant);
let mut file = tempfile::NamedTempFile::new().unwrap();
let _ = file.write(toml::to_string(&editable_content).unwrap().as_bytes());
let _ = file.flush();
let mut cmd = Command::new(editor).args([file.path()]).spawn().unwrap();
cmd.wait().unwrap();
let file = file.reopen().unwrap();
let mut reader = BufReader::new(file);
let mut content = Vec::new();
let _ = reader.read_to_end(&mut content);
println!("content");
println!("{}", String::from_utf8(content.clone()).unwrap());
let new_variant: EditorMessage = toml::from_str(&String::from_utf8(content).unwrap()).unwrap();
variant.set_content(new_variant.content);
msg.set_description(new_variant.description);
}
}

View File

@ -1,6 +1,9 @@
mod bundle;
pub use bundle::Bundle;
mod editor;
pub use editor::Editor;
mod types;
pub use types::{Message, Variant};

View File

@ -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,26 @@ 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 content(&self) -> &str {
&self.content
}
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())
}