Skip to content
4 changes: 2 additions & 2 deletions src/c_api/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::haystack::val::Value;
pub unsafe extern "C" fn haystack_value_get_str_len(val: *const Value) -> usize {
match unsafe { val.as_ref() } {
Some(value) => match value {
Value::Str(str) => return str.value.len(),
Value::Str(str) => return str.len(),
_ => new_error("Not a Str Value"),
},
None => new_error("Invalid Value reference"),
Expand Down Expand Up @@ -69,7 +69,7 @@ pub unsafe extern "C" fn haystack_value_get_str_len(val: *const Value) -> usize
pub unsafe extern "C" fn haystack_value_get_str_value(val: *const Value) -> *const c_char {
match unsafe { val.as_ref() } {
Some(value) => match value {
Value::Str(str) => match CString::new(str.value.as_bytes()) {
Value::Str(str) => match CString::new(str.as_bytes()) {
Ok(str) => return str.into_raw(),
Err(err) => update_last_error(err),
},
Expand Down
4 changes: 2 additions & 2 deletions src/c_api/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::haystack::val::Value;
pub unsafe extern "C" fn haystack_value_get_symbol_value_len(val: *const Value) -> usize {
match unsafe { val.as_ref() } {
Some(value) => match value {
Value::Symbol(symbol) => return symbol.value.len(),
Value::Symbol(symbol) => return symbol.value().len(),
_ => new_error("Not a Symbol Value"),
},
None => new_error("Invalid Value reference"),
Expand Down Expand Up @@ -68,7 +68,7 @@ pub unsafe extern "C" fn haystack_value_get_symbol_value_len(val: *const Value)
pub unsafe extern "C" fn haystack_value_get_symbol_value(val: *const Value) -> *const c_char {
match unsafe { val.as_ref() } {
Some(value) => match value {
Value::Symbol(symbol) => match CString::new(symbol.value.as_bytes()) {
Value::Symbol(symbol) => match CString::new(symbol.value().as_bytes()) {
Ok(str) => return str.into_raw(),
Err(err) => update_last_error(err),
},
Expand Down
4 changes: 2 additions & 2 deletions src/c_api/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::haystack::val::Value;
pub unsafe extern "C" fn haystack_value_get_uri_value_len(val: *const Value) -> usize {
match unsafe { val.as_ref() } {
Some(value) => match value {
Value::Uri(uri) => return uri.value.len(),
Value::Uri(uri) => return uri.value().len(),
_ => new_error("Not a Uri Value"),
},
None => new_error("Invalid Value reference"),
Expand Down Expand Up @@ -69,7 +69,7 @@ pub unsafe extern "C" fn haystack_value_get_uri_value_len(val: *const Value) ->
pub unsafe extern "C" fn haystack_value_get_uri_value(val: *const Value) -> *const c_char {
match unsafe { val.as_ref() } {
Some(value) => match value {
Value::Uri(uri) => match CString::new(uri.value.as_bytes()) {
Value::Uri(uri) => match CString::new(uri.value().as_bytes()) {
Ok(str) => return str.into_raw(),
Err(err) => update_last_error(err),
},
Expand Down
8 changes: 4 additions & 4 deletions src/haystack/defs/containment_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn get_contained_by_refs_for_super_type(
.filter(|def| options.deprecated || !def.has("deprecated"))
.filter_map(|def| {
let contained_by_sym = def.get_symbol("containedBy")?;
if namespace.fits(&Symbol::make(contained_by_sym.value.as_str()), &super_sym) {
if namespace.fits(&Symbol::make(contained_by_sym.value()), &super_sym) {
Some(def.def_name().to_string())
} else {
None
Expand Down Expand Up @@ -129,15 +129,15 @@ pub fn add_containment_refs(
let entity_type_name = namespace
.def_of_dict(parent)
.get_symbol("def")
.map(|s| s.value.clone())
.map(|s| s.value())
.unwrap_or_default();

if entity_type_name.is_empty() {
return String::new();
}

let ref_name = find_containment_ref_for_type(namespace, &entity_type_name)
.map(|def| def.def_name().to_string())
let ref_name = find_containment_ref_for_type(namespace, entity_type_name)
.map(|def| def.def_name().to_owned())
.unwrap_or_default();

if !ref_name.is_empty()
Expand Down
2 changes: 1 addition & 1 deletion src/haystack/defs/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
/// A list of dicts.
///
pub(super) fn parse_multi_line_string_to_dicts(val: &Str) -> Vec<Dict> {
val.value
val.value()
.split('\n')
.map(|line| line.trim())
.filter(|line| !line.is_empty() && !line.starts_with("//"))
Expand Down
22 changes: 11 additions & 11 deletions src/haystack/defs/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub trait DefDict: HaystackDict {
}

/// Return the `def` [Symbol](crate::val::Symbol) name
fn def_name(&self) -> &String {
&self.def_symbol().value
fn def_name(&self) -> &str {
self.def_symbol().value()
}
}

Expand Down Expand Up @@ -169,12 +169,12 @@ impl Namespace {

/// True if the name is for a conjunct.
pub fn is_conjunct(symbol: &Symbol) -> bool {
symbol.value.contains('-')
symbol.value().contains('-')
}

/// Decomposes a conjunct into its respective defs and returns them
pub fn conjuncts_defs(&self, symbol: &Symbol) -> Vec<&Dict> {
self.all_matching_names(&symbol.value.split('-').collect::<Vec<&str>>())
self.all_matching_names(&symbol.value().split('-').collect::<Vec<&str>>())
}

/// Computes a list of feature defs.
Expand All @@ -195,7 +195,7 @@ impl Namespace {

/// True if the name is for a feature.
pub fn is_feature(symbol: &Symbol) -> bool {
symbol.value.contains(':')
symbol.value().contains(':')
}

/// Computes a list of all the libs implemented by this namespace.
Expand Down Expand Up @@ -314,7 +314,7 @@ impl Namespace {
let mut features = HashSet::<&str>::new();
for sym in self.defs.keys() {
if Namespace::is_feature(sym)
&& let Some((first, _second)) = sym.value.split_once(':')
&& let Some((first, _second)) = sym.value().split_once(':')
{
features.insert(first);
}
Expand All @@ -330,7 +330,7 @@ impl Namespace {
for def in self.defs.values() {
if let Some(tag_on) = def.get_list("tagOn") {
let names = tag_on.iter().filter_map(|v| match v {
Value::Symbol(sym) => Some(sym.value.as_str()),
Value::Symbol(sym) => Some(sym.value()),
_ => None,
});

Expand Down Expand Up @@ -391,7 +391,7 @@ impl Namespace {
if !association_def.has("computedFromReciprocal") {
return self
.get(parent)
.and_then(|def| def.get_list(&association.value))
.and_then(|def| def.get_list(association.value()))
.unwrap_or(&Vec::default())
.iter()
.filter_map(|value| match value {
Expand Down Expand Up @@ -423,7 +423,7 @@ impl Namespace {
let mut matches = HashSet::<&Dict>::new();

for def in self.defs.values() {
if let Some(Value::List(list)) = def.get(&reciprocal_of.value) {
if let Some(Value::List(list)) = def.get(reciprocal_of.value()) {
list.iter()
.filter_map(|value| match value {
Value::Symbol(sym) => self.get(sym),
Expand Down Expand Up @@ -803,7 +803,7 @@ impl Namespace {
let id = cur_subject.get_ref("id").cloned();
while let Some((subject_key, subject_val)) = cur_subject.pop_first() {
let subject_def = self.get_by_name(&subject_key);
let mut rel_val = subject_def.and_then(|def| def.get(&rel_name.value));
let mut rel_val = subject_def.and_then(|def| def.get(rel_name.value()));

// Handle a reciprocal relationship. A reciprocal relationship can only
// be inverted when a ref is specified.
Expand All @@ -812,7 +812,7 @@ impl Namespace {
&& subject_val.is_ref()
&& let Some(reciprocal_of) = reciprocal_of
{
rel_val = subject_def.and_then(|def| def.get(&reciprocal_of.value));
rel_val = subject_def.and_then(|def| def.get(reciprocal_of.value()));

if rel_val.is_some()
&& let Value::Ref(ref val) = subject_val
Expand Down
34 changes: 20 additions & 14 deletions src/haystack/encoding/brio/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn decode_buf_as_xstr<R: Read>(reader: &mut R) -> Result<XStr> {
.read_exact(&mut bytes)
.map_err(|e| Error::Message(e.to_string()))?;
let hex: String = bytes.iter().map(|b| format!("{b:02x}")).collect();
Ok(XStr::make("Bin", &hex))
Ok(XStr::make("Bin", hex))
}

/// Decode a non-empty Dict payload: `'{' varint(count) (key value)* '}'`
Expand Down Expand Up @@ -439,20 +439,18 @@ impl FromBrio for Value {
let u = decode_str(reader)?;
Ok(Value::from(make_number(v, &u)))
}
CTRL_STR => Ok(Value::from(decode_str(reader)?.as_str())),
CTRL_STR => Ok(Value::from(decode_str(reader)?)),
CTRL_REF_STR => {
let id = decode_str(reader)?;
let dis = decode_str_chars(reader)?;
Ok(Value::from(Ref::make(&id, non_empty(dis.as_str()))))
Ok(Value::from(make_ref(id, dis)))
}
CTRL_REF_I8 => {
let id = i8_to_ref_id(read_i64(reader)?);
let dis = decode_str_chars(reader)?;
Ok(Value::from(Ref::make(&id, non_empty(dis.as_str()))))
Ok(Value::from(make_ref(id, dis)))
}
CTRL_URI => Ok(Value::from(Uri {
value: decode_str(reader)?,
})),
CTRL_URI => Ok(Value::from(Uri::from(decode_str(reader)?))),
CTRL_DATE => {
let year = read_i16(reader)? as i32;
let month = read_u8(reader)? as u32;
Expand Down Expand Up @@ -488,10 +486,10 @@ impl FromBrio for Value {
Ok(Value::from(Coord::make(lat, lng)))
}
CTRL_XSTR => Ok(Value::from(XStr::make(
&decode_str(reader)?,
&decode_str(reader)?,
decode_str(reader)?,
decode_str(reader)?,
))),
CTRL_SYMBOL => Ok(Value::from(Symbol::make(&decode_str(reader)?))),
CTRL_SYMBOL => Ok(Value::from(Symbol::from(decode_str(reader)?))),
CTRL_BUF => decode_buf_as_xstr(reader).map(Value::from),
CTRL_DICT_EMPTY => Ok(Value::from(Dict::default())),
CTRL_DICT => decode_dict_payload(reader).map(Value::from),
Expand Down Expand Up @@ -547,6 +545,16 @@ fn non_empty(s: &str) -> Option<&str> {
if s.is_empty() { None } else { Some(s) }
}

/// Build a `Ref` from an owned id and an owned (possibly empty) display name,
/// moving both strings in directly to avoid re-allocating them.
fn make_ref(id: String, dis: String) -> Ref {
let mut r = Ref::from(id);
if non_empty(&dis).is_some() {
r.set_dis(Some(dis));
}
r
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -668,9 +676,7 @@ mod tests {

#[test]
fn test_uri() {
let v = Value::from(Uri {
value: "https://project-haystack.org".into(),
});
let v = Value::from(Uri::from("https://project-haystack.org"));
assert_eq!(round_trip(&v), v);
}

Expand Down Expand Up @@ -1161,7 +1167,7 @@ mod tests {
"n" => Value::from(Number::make(123.0)),
"s" => Value::from("hi"),
"r" => Value::from(Ref::make("1deb31b8-7508b187", None)),
"u" => Value::from(Uri { value: "a/b".to_string() }),
"u" => Value::from(Uri::from("a/b")),
"d" => Value::from(Date::from_ymd(2021, 6, 15).unwrap()),
"dt" => Value::from(DateTime::parse_from_rfc3339("2021-06-15T12:00:00Z").unwrap())
});
Expand Down
10 changes: 4 additions & 6 deletions src/haystack/encoding/brio/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,23 +265,23 @@ impl ToBrio for Bool {
impl ToBrio for Str {
fn to_brio<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_all(&[CTRL_STR])?;
encode_str(writer, &self.value)?;
encode_str(writer, self.value())?;
Ok(())
}
}

impl ToBrio for Uri {
fn to_brio<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_all(&[CTRL_URI])?;
encode_str(writer, &self.value)?;
encode_str(writer, self.value())?;
Ok(())
}
}

impl ToBrio for Symbol {
fn to_brio<W: Write>(&self, writer: &mut W) -> Result<()> {
writer.write_all(&[CTRL_SYMBOL])?;
encode_str(writer, &self.value)?;
encode_str(writer, self.value())?;
Ok(())
}
}
Expand Down Expand Up @@ -679,9 +679,7 @@ mod tests {

#[test]
fn test_uri() {
let v = Value::from(Uri {
value: "http://example.com".into(),
});
let v = Value::from(Uri::from("http://example.com"));
let bytes = enc(&v);
assert_eq!(bytes[0], CTRL_URI);
}
Expand Down
22 changes: 11 additions & 11 deletions src/haystack/encoding/json/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'de> Visitor<'de> for JsonValueDecoderVisitor {
if key == "_kind" {
match value {
HVal::Str(str_kind) => {
kind = str_kind.value;
kind = str_kind.to_string();
match kind.as_str() {
"marker" => return Ok(HVal::make_marker()),
"remove" => return Ok(HVal::make_remove()),
Expand Down Expand Up @@ -285,23 +285,23 @@ fn parse_number(dict: &Dict) -> Result<HVal, JsonErr> {
fn parse_ref(dict: &Dict) -> Result<HVal, JsonErr> {
match dict.get_str("val") {
Some(val) => {
let dis = dict.get_str("dis").map(|d| d.value.as_str());
Ok(Ref::make(val.value.as_str(), dis).into())
let dis = dict.get_str("dis").map(|d| d.value());
Ok(Ref::make(val.value(), dis).into())
}
None => Err(JsonErr::custom("Missing or invalid 'val'")),
}
}

fn parse_symbol(dict: &Dict) -> Result<HVal, JsonErr> {
match dict.get_str("val") {
Some(val) => Ok(HVal::make_symbol(&val.value)),
Some(val) => Ok(HVal::make_symbol(val.value())),
None => Err(JsonErr::custom("Missing or invalid 'val'")),
}
}

fn parse_uri(dict: &Dict) -> Result<HVal, JsonErr> {
match dict.get_str("val") {
Some(val) => Ok(HVal::make_uri(&val.value)),
Some(val) => Ok(HVal::make_uri(val.value())),
None => Err(JsonErr::custom("Missing or invalid 'val'")),
}
}
Expand All @@ -328,11 +328,11 @@ fn parse_time(dict: &Dict) -> Result<HVal, JsonErr> {

fn parse_datetime(dict: &Dict) -> Result<HVal, JsonErr> {
match dict.get_str("val") {
Some(val) => match DateTime::parse_from_rfc3339(&val.value) {
Some(val) => match DateTime::parse_from_rfc3339(val.value()) {
Ok(date) => match dict.get_str("tz") {
Some(tz) => {
let datetime =
make_date_time_with_tz(&date.with_timezone(&Utc.fix()), &tz.value);
make_date_time_with_tz(&date.with_timezone(&Utc.fix()), tz.value());
match datetime {
Ok(datetime) => Ok(HVal::DateTime(datetime.into())),
Err(err) => Err(JsonErr::custom(err)),
Expand All @@ -359,7 +359,7 @@ fn parse_coord(dict: &Dict) -> Result<HVal, JsonErr> {
fn parse_xstr(dict: &Dict) -> Result<HVal, JsonErr> {
match dict.get_str("type") {
Some(r#type) => match dict.get_str("val") {
Some(val) => Ok(HVal::make_xstr_from(&r#type.value, &val.value)),
Some(val) => Ok(HVal::make_xstr_from(r#type.value(), val.value())),
None => Err(JsonErr::custom("Missing or invalid 'val'")),
},
None => Err(JsonErr::custom("Missing or invalid 'type'")),
Expand Down Expand Up @@ -391,7 +391,7 @@ fn parse_grid_meta_and_ver(dict: &Dict) -> (Option<Dict>, String) {

if let Some(ref mut meta_dict) = meta {
if let Some(ver) = meta_dict.get_str(VER) {
grid_ver = ver.value.to_owned();
grid_ver = ver.to_string();
meta_dict.remove(VER);
}

Expand All @@ -410,12 +410,12 @@ fn parse_grid_columns(cols: &List) -> Result<Vec<Column>, JsonErr> {
HVal::Dict(dict) => match dict.get_str("name") {
Some(name) => match dict.get("meta") {
Some(HVal::Dict(meta)) => Ok(Column {
name: name.value.clone(),
name: name.to_string(),
meta: Some(meta.clone()),
}),
Some(_) => Err(JsonErr::custom("Invalid 'meta'")),
None => Ok(Column {
name: name.value.clone(),
name: name.to_string(),
meta: None,
}),
},
Expand Down
Loading
Loading