Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 29, 2022
2 parents c8ebe6c + aa65331 commit 0ecf0e5
Show file tree
Hide file tree
Showing 30 changed files with 1,060 additions and 249 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
target/
target/
*.relf
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ anyhow = "1.0.65"
regex = "1.6.0"
lazy_static = "1.4.0"
log = "0.4.17"
sxd-xpath = "0.4.2"
sxd-document = "0.3.2"
clap = { version = "3.2.22", features = ["cargo"] }
simple_logger = "2.3.0"
xml-builder = "0.5.0"
ctor = "0.1.23"
itertools = "0.10.5"
glob = "0.3.0"
Expand Down
5 changes: 4 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ fn main() {
println!("cargo:rerun-if-changed=test-pom.xml");
println!("cargo:rerun-if-changed=target/eo");
assert!(Command::new("mvn")
.arg("--batch-mode")
.arg("--errors")
.arg("--debug")
.arg("--file")
.arg("test-pom.xml")
.arg("compile")
.arg("process-resources")
.spawn()
.unwrap()
.wait()
Expand Down
File renamed without changes.
80 changes: 42 additions & 38 deletions src/bin/reo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

extern crate reo;

use anyhow::Context;
use anyhow::Result;
use anyhow::{anyhow, Context};
use clap::{crate_version, AppSettings, Arg, ArgAction, Command};
use filetime::FileTime;
use glob::glob;
Expand Down Expand Up @@ -152,6 +152,13 @@ pub fn main() -> Result<()> {
.takes_value(true)
.action(ArgAction::Set),
)
.arg(
Arg::new("object")
.required(true)
.help("Fully qualified object name")
.takes_value(false)
.action(ArgAction::Set),
)
.arg_required_else_help(true),
)
.subcommand(
Expand Down Expand Up @@ -209,31 +216,26 @@ pub fn main() -> Result<()> {
"Relf file '{}' is up to date ({} bytes), no need to compile (use --force to compile anyway)",
relf.display(), fs::metadata(relf)?.len()
);
} else {
info!(
"Deploying instructions from a single file '{}'",
file.display()
);
let total = Gmi::from_file(file)?.deploy_to(&mut uni)?;
info!(
"Deployed {} GMI instructions in {:?}",
total,
start.elapsed()
);
return Ok(());
}
info!(
"Deploying instructions from a single file '{}'",
file.display()
);
let total = Gmi::from_file(file)?.deploy_to(&mut uni)?;
info!(
"Deployed {} GMI instructions in {:?}",
total,
start.elapsed()
);
} else {
let home = subs.value_of("dir").unwrap_or_else(|| {
if subs.contains_id("eoc") {
info!("Running in eoc-compatible mode");
".eoc/gmi"
} else {
"."
}
});
let home = if subs.contains_id("eoc") {
info!("Running in eoc-compatible mode");
".eoc/gmi"
} else {
subs.value_of("dir").unwrap()
};
info!("Home requested as '{}'", home);
if !Path::new(home).exists() {
return Err(anyhow!("Directory '{}' doesn't exist", home));
}
let full_home =
fs::canonicalize(home).context(format!("Can't access '{}'", home))?;
let cwd = full_home.as_path();
Expand All @@ -244,22 +246,22 @@ pub fn main() -> Result<()> {
&& !subs.contains_id("force")
{
info!(
"Relf file '{}' is up to date ({} bytes), no need to compile (use --force to compile anyway)",
"Relf file '{}' ({} bytes) is newer than that directory, no need to compile (use --force to compile anyway)",
relf.display(), fs::metadata(relf)?.len()
);
} else {
info!(
"Deploying instructions from a directory '{}'",
cwd.display()
);
uni.add(0)?;
let total = setup(&mut uni, cwd)?;
info!(
"Deployed {} GMI instructions in {:?}",
total,
start.elapsed()
);
return Ok(());
}
info!(
"Deploying instructions from a directory '{}'",
cwd.display()
);
uni.add(0)?;
let total = setup(&mut uni, cwd)?;
info!(
"Deployed {} GMI instructions in {:?}",
total,
start.elapsed()
);
}
let size = uni.save(relf)?;
info!(
Expand Down Expand Up @@ -290,14 +292,16 @@ pub fn main() -> Result<()> {
}
Some(("inspect", subs)) => {
let relf = Path::new(subs.value_of("relf").unwrap());
let object = subs
.get_one::<String>("object")
.context("Object name is required")?;
let uni = Universe::load(relf).unwrap();
info!(
"Deserialized {} bytes in {:?}",
fs::metadata(relf).unwrap().len(),
start.elapsed()
);
let json = serde_json::to_string_pretty(&uni).unwrap();
println!("Universe is: {}", json);
println!("{}", uni.inspect(object.as_str())?);
}
Some(("link", subs)) => {
let target = Path::new(subs.value_of("relf").unwrap());
Expand Down
81 changes: 78 additions & 3 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ impl Data {
self.bytes.len() == 0
}

/// Turn it into `bool`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x01].to_vec());
/// assert_eq!(true, d.as_bool().unwrap());
/// ```
pub fn as_bool(&self) -> Result<bool> {
Ok(self.bytes[0] == 0x01)
}

/// Turn it into `i64`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2A].to_vec());
/// assert_eq!(42, d.as_int().unwrap());
/// ```
pub fn as_int(&self) -> Result<i64> {
let a: &[u8; 8] = &self
.bytes
Expand All @@ -100,6 +118,13 @@ impl Data {
Ok(i64::from_be_bytes(*a))
}

/// Turn it into `f64`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18].to_vec());
/// assert_eq!(std::f64::consts::PI, d.as_float().unwrap());
/// ```
pub fn as_float(&self) -> Result<f64> {
let a: &[u8; 8] = &self
.bytes
Expand All @@ -109,28 +134,64 @@ impl Data {
Ok(f64::from_be_bytes(*a))
}

/// Turn it into `string`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x41, 0x42].to_vec());
/// assert_eq!("AB", d.as_string().unwrap());
/// ```
pub fn as_string(&self) -> Result<String> {
Ok(String::from_utf8(self.bytes.clone())?)
}

/// Turn it into a hexadecimal string.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0xCA, 0xFE].to_vec());
/// assert_eq!("CA-FE", d.as_hex());
/// ```
pub fn as_hex(&self) -> String {
if self.bytes.is_empty() {
"--".to_string()
} else {
self.bytes
.iter()
.map(|b| format!("{:02x}", b).to_string())
.map(|b| format!("{:02X}", b).to_string())
.collect::<Vec<String>>()
.join("-")
}
}

/// Turn it into a vector of bytes.
pub fn as_bytes(&self) -> Vec<u8> {
self.bytes.clone()
}
}

#[test]
fn simple_int() {
let i = 42;
let d = Data::from_int(42);
let d = Data::from_int(i);
assert_eq!(i, d.as_int().unwrap());
assert_eq!("00-00-00-00-00-00-00-2A", d.as_hex());
}

#[test]
fn simple_bool() {
let b = true;
let d = Data::from_bool(b);
assert_eq!(b, d.as_bool().unwrap());
assert_eq!("01", d.as_hex());
}

#[test]
fn simple_float() {
let f = std::f64::consts::PI;
let d = Data::from_float(f);
assert_eq!(f, d.as_float().unwrap());
assert_eq!("40-09-21-FB-54-44-2D-18", d.as_hex());
}

#[test]
Expand All @@ -145,7 +206,7 @@ fn compares_with_data() {
fn prints_bytes() {
let txt = "привет";
let d = Data::from_str(txt);
assert_eq!("d0-bf-d1-80-d0-b8-d0-b2-d0-b5-d1-82", d.as_hex());
assert_eq!("D0-BF-D1-80-D0-B8-D0-B2-D0-B5-D1-82", d.as_hex());
assert_eq!(txt, Data::from_hex(d.as_hex()).as_string().unwrap());
}

Expand All @@ -155,3 +216,17 @@ fn prints_empty_bytes() {
let d = Data::from_str(txt);
assert_eq!("--", d.as_hex());
}

#[test]
fn broken_int_from_small_data() {
let d = Data::from_bytes([0x01, 0x02].to_vec());
let ret = d.as_int();
assert!(ret.is_err());
}

#[test]
fn broken_float_from_small_data() {
let d = Data::from_bytes([0x00].to_vec());
let ret = d.as_float();
assert!(ret.is_err());
}

0 comments on commit 0ecf0e5

Please sign in to comment.