Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cirup_cli/src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,19 @@ subcommands:
required: true
- output:
index: 3
takes_value: true
takes_value: true
- diff-with-base:
about: "output keys that have values in [new] but not in [old] with the value in [base]"
args:
- old:
index: 1
takes_value: true
required: true
- new:
index: 2
takes_value: true
required: true
- base:
index: 3
takes_value: true
required: true
13 changes: 13 additions & 0 deletions cirup_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ fn sort(file_one: &str, out_file: Option<&str>) {
}
}

fn diff_with_base(old: &str, new: &str, base: &str) {
let query = query::query_diff_with_base(old, new, base);
query.run_triple_interactive();
}

fn run(matches: &clap::ArgMatches, config: Option<Config>) -> Result<(), Box<dyn Error>> {
match matches.subcommand() {
("file-print", Some(args)) => {
Expand Down Expand Up @@ -173,6 +178,14 @@ fn run(matches: &clap::ArgMatches, config: Option<Config>) -> Result<(), Box<dyn
}
None => Err("configuration file required")?,
},
("diff-with-base", Some(args)) => {
diff_with_base(
args.value_of("old").unwrap(),
args.value_of("new").unwrap(),
args.value_of("base").unwrap(),
);
Ok(())
}
_ => Err("unrecognised subcommand")?,
}
}
Expand Down
4 changes: 4 additions & 0 deletions cirup_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ extern crate lazy_static;
mod resource;
use resource::Resource;

mod triple;
use triple::Triple;


pub mod config;
mod error;
mod shell;
Expand Down
93 changes: 83 additions & 10 deletions cirup_core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rusqlite::{Connection, Error, Statement};
use file::{save_resource_file, vfile_set};
use vtab::{create_db, init_db, register_table};

use Resource;
use {Resource, Triple};

pub fn print_pretty(columns: Vec<String>, values: &mut Rows) {
let mut row = Row::empty();
Expand Down Expand Up @@ -54,6 +54,15 @@ pub fn print_resources_pretty(resources: &Vec<Resource>) {
println!("{}", table);
}

pub fn print_triples_pretty(triples: &Vec<Triple>) {
for triple in triples.iter() {
println!("name: {}", triple.name);
println!("base: {}", triple.base);
println!("value: {}", triple.value);
println!("");
};
}

fn get_statement_column_names(statement: &Statement) -> Vec<String> {
let mut column_names = Vec::new();
for column_name in statement.column_names().iter() {
Expand Down Expand Up @@ -112,6 +121,28 @@ pub fn execute_query_resource(db: &Connection, query: &str) -> Vec<Resource> {
resources
}

pub fn execute_query_triple(db: &Connection, query: &str) -> Vec<Triple> {
let mut resources: Vec<Triple> = Vec::new();
let mut statement = db.prepare(&query).unwrap();
let mut response = statement.query(&[]).unwrap();

loop {
if let Some(v) = response.next() {
if let Some(res) = v.ok() {
let name = &res.get::<usize, String>(0);
let value = &res.get::<usize, String>(1);
let base = &res.get::<usize, String>(2);
let resource = Triple::new(name, value, base);
resources.push(resource);
}
} else {
break;
}
}

resources
}

pub fn query_file(input: &str, table: &str, query: &str) {
let db = init_db(table, input);
execute_query(&db, query);
Expand Down Expand Up @@ -140,6 +171,10 @@ impl CirupEngine {
execute_query_resource(&self.db, query)
}

pub fn query_triple(&self, query: &str) -> Vec<Triple> {
execute_query_triple(&self.db, query)
}

pub fn query(&self, query: &str) {
execute_query(&self.db, query);
}
Expand All @@ -156,6 +191,12 @@ const DIFF_QUERY: &str = r"
FROM A
LEFT OUTER JOIN B ON A.key = B.key
WHERE (B.val IS NULL)";
const DIFF_WITH_BASE_QUERY: &str = r"
SELECT B.key, B.val, C.val
FROM B
LEFT OUTER JOIN A ON B.key = A.key
INNER JOIN C ON B.key = C.key
WHERE (A.val IS NULL)";
const CHANGE_QUERY: &str = r"
SELECT A.key, A.val, B.val
FROM A
Expand All @@ -182,46 +223,54 @@ const CONVERT_QUERY: &str = "SELECT * FROM A";
const SORT_QUERY: &str = "SELECT * FROM A ORDER BY A.key";

pub fn query_print(file: &str) -> CirupQuery {
CirupQuery::new(PRINT_QUERY, file, None)
CirupQuery::new(PRINT_QUERY, file, None, None)
}

pub fn query_convert(file: &str) -> CirupQuery {
CirupQuery::new(CONVERT_QUERY, file, None)
CirupQuery::new(CONVERT_QUERY, file, None, None)
}

pub fn query_sort(file: &str) -> CirupQuery {
CirupQuery::new(SORT_QUERY, file, None)
CirupQuery::new(SORT_QUERY, file, None, None)
}

pub fn query_diff(file_one: &str, file_two: &str) -> CirupQuery {
CirupQuery::new(DIFF_QUERY, file_one, Some(file_two))
CirupQuery::new(DIFF_QUERY, file_one, Some(file_two), None)
}

pub fn query_diff_with_base(old: &str, new: &str, base: &str) -> CirupQuery {
CirupQuery::new(DIFF_WITH_BASE_QUERY, old, Some(new), Some(base))
}

pub fn query_change(file_one: &str, file_two: &str) -> CirupQuery {
CirupQuery::new(CHANGE_QUERY, file_one, Some(file_two))
CirupQuery::new(CHANGE_QUERY, file_one, Some(file_two), None)
}

pub fn query_merge(file_one: &str, file_two: &str) -> CirupQuery {
CirupQuery::new(MERGE_QUERY, file_one, Some(file_two))
CirupQuery::new(MERGE_QUERY, file_one, Some(file_two), None)
}

pub fn query_intersect(file_one: &str, file_two: &str) -> CirupQuery {
CirupQuery::new(INTERSECT_QUERY, file_one, Some(file_two))
CirupQuery::new(INTERSECT_QUERY, file_one, Some(file_two), None)
}

pub fn query_subtract(file_one: &str, file_two: &str) -> CirupQuery {
CirupQuery::new(SUBTRACT_QUERY, file_one, Some(file_two))
CirupQuery::new(SUBTRACT_QUERY, file_one, Some(file_two), None)
}

impl CirupQuery {
pub fn new(query: &str, file_one: &str, file_two: Option<&str>) -> Self {
pub fn new(query: &str, file_one: &str, file_two: Option<&str>, file_three: Option<&str>) -> Self {
let engine = CirupEngine::new();
engine.register_table_from_file("A", file_one);

if file_two.is_some() {
engine.register_table_from_file("B", file_two.unwrap());
}

if file_two.is_some() {
engine.register_table_from_file("C", file_three.unwrap());
}

CirupQuery {
engine: engine,
query: query.to_string(),
Expand All @@ -232,6 +281,10 @@ impl CirupQuery {
return self.engine.query_resource(&self.query);
}

pub fn run_triple(&self) -> Vec<Triple> {
return self.engine.query_triple(&self.query);
}

pub fn run_interactive(&self, out_file: Option<&str>) {
let resources = self.run();

Expand All @@ -241,6 +294,11 @@ impl CirupQuery {
print_resources_pretty(&resources);
}
}

pub fn run_triple_interactive(&self) {
let triples = self.run_triple();
print_triples_pretty(&triples);
}
}

#[cfg(test)]
Expand Down Expand Up @@ -285,3 +343,18 @@ fn test_query_subtract() {
let actual = engine.query_resource("SELECT * FROM A WHERE A.key NOT IN (SELECT B.key FROM B)");
assert_eq!(actual, expected);
}

#[test]
fn test_query_diff_with_base() {
let engine = CirupEngine::new();
engine.register_table_from_str("A", "test_old.resx", include_str!("../test/test_old.resx"));
engine.register_table_from_str("B", "test_new.resx", include_str!("../test/test_new.resx"));
engine.register_table_from_str("C", "test.resx", include_str!("../test/test.resx"));

let triples = engine.query_triple(DIFF_WITH_BASE_QUERY);

assert_eq!(triples.len(), 2);
assert_eq!(triples[0].name, String::from("lblYolo"));
assert_eq!(triples[0].base, String::from("You only live once"));
assert_eq!(triples[0].value, String::from("Juste une vie a vivre"));
}
2 changes: 2 additions & 0 deletions cirup_core/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl Sync {
query_string,
&source_path_out.to_string_lossy(),
Some(&translation_file.path.to_string_lossy()),
None
);
let file_path = rev.append_to_file_name(
Path::new(self.temp_dir.path()).join(&translation_file.file_name),
Expand Down Expand Up @@ -380,6 +381,7 @@ impl Sync {
query_string,
&source_path_out.to_string_lossy(),
Some(&file_path.to_string_lossy()),
None
);
}

Expand Down
36 changes: 36 additions & 0 deletions cirup_core/src/triple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::fmt;

#[derive(Clone)]
pub struct Triple {
pub name: String,
pub value: String,
pub base: String,
}

impl fmt::Debug for Triple {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{},{},{}", self.name, self.value, self.base)
}
}

impl fmt::Display for Triple {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{},{},{}", self.name, self.value, self.base)
}
}

impl PartialEq for Triple {
fn eq(&self, other: &Triple) -> bool {
(self.name == other.name) && (self.value == other.value)
}
}

impl Triple {
pub fn new(name: &str, value: &str, base: &str) -> Self {
Triple {
name: name.to_owned(),
value: value.to_owned(),
base: base.to_owned(),
}
}
}
16 changes: 16 additions & 0 deletions cirup_core/test/test_new.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<data name="lblBoat" xml:space="preserve">
<value>Je suis sur un bateau</value>
</data>
<data name="lblYolo" xml:space="preserve">
<value>Juste une vie a vivre</value>
</data>
<data name="lblDogs" xml:space="preserve">
<value>Qui a laissé les chiens sortir?</value>
</data>
<data name="lblOnlyInNew" xml:space="preserve">
<value>Francais seulement</value>
</data>
</root>

7 changes: 7 additions & 0 deletions cirup_core/test/test_old.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<data name="lblBoat" xml:space="preserve">
<value>Je suis sur un bateau</value>
</data>
</root>