Aliases
This commit is contained in:
1 parent
8b593939e5
commit
7224dc5431
3 files changed
+70
-15
No files matched your search
+23
-6
@@ -2,9 +2,9 @@ mod handling;
|
||||
mod testing;
|
||||
|
||||
use litemap::LiteMap;
|
||||
use minicbor::Decoder;
|
||||
use minicbor::data::Type;
|
||||
use minicbor::decode::Error;
|
||||
use minicbor::Decoder;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::io::Write;
|
||||
@@ -14,6 +14,7 @@ pub enum Command {
|
||||
Set(String, StoredData),
|
||||
Define(String, LiteMap<String, StoredData>),
|
||||
Push(String, StoredData),
|
||||
Alias(String, String),
|
||||
Display(String),
|
||||
SetOutput(fn() -> Box<dyn Write>),
|
||||
}
|
||||
@@ -37,8 +38,11 @@ impl Display for StoredData {
|
||||
StoredData::Bool(v) => v.fmt(f),
|
||||
StoredData::Line(v) => v.fmt(f),
|
||||
StoredData::Vec(vec) => {
|
||||
vec.iter().for_each(|d| {
|
||||
_ = write!(f, "{}\n", d);
|
||||
vec.iter().enumerate().for_each(|(i, d)| {
|
||||
write!(f, "{}", d).unwrap();
|
||||
if i != vec.len() - 1 {
|
||||
write!(f, "\n").unwrap();
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
@@ -90,10 +94,9 @@ fn parse_command(data: &[u8]) -> Result<Command, Error> {
|
||||
return Ok(Command::Set(id, value));
|
||||
}
|
||||
if data.starts_with(b"PUSH:") {
|
||||
println!("{}", String::from_utf8_lossy(data));
|
||||
let payload = &data[b"PUSH:".len()..];
|
||||
let mut decoder = Decoder::new(&payload);
|
||||
decoder.array()?;
|
||||
_ = decoder.array()?;
|
||||
let id = decoder.str()?.to_string();
|
||||
let value = decode_value(&mut decoder)?;
|
||||
return Ok(Command::Push(id, value));
|
||||
@@ -105,6 +108,14 @@ fn parse_command(data: &[u8]) -> Result<Command, Error> {
|
||||
let id = decoder.str()?.to_string();
|
||||
return Ok(Command::Display(id));
|
||||
}
|
||||
if data.starts_with(b"ALIAS:") {
|
||||
let payload = &data[b"ALIAS:".len()..];
|
||||
let mut decoder = Decoder::new(&payload);
|
||||
decoder.array()?;
|
||||
let id = decoder.str()?.to_string();
|
||||
let alias = decoder.str()?.to_string();
|
||||
return Ok(Command::Alias(id, alias));
|
||||
}
|
||||
Ok(Command::Unknown)
|
||||
}
|
||||
|
||||
@@ -131,7 +142,7 @@ fn decode_value(decoder: &mut Decoder) -> Result<StoredData, Error> {
|
||||
} else {
|
||||
vec![]
|
||||
};
|
||||
if array.is_empty() {
|
||||
if array.capacity() == 0 {
|
||||
loop {
|
||||
let dt = decoder.datatype()?;
|
||||
if let Type::Break = dt {
|
||||
@@ -139,6 +150,10 @@ fn decode_value(decoder: &mut Decoder) -> Result<StoredData, Error> {
|
||||
}
|
||||
array.push(decode_value(decoder)?);
|
||||
}
|
||||
} else {
|
||||
for _ in 0..array.capacity() {
|
||||
array.push(decode_value(decoder)?);
|
||||
}
|
||||
}
|
||||
StoredData::Line(Line::from(array))
|
||||
}
|
||||
@@ -151,6 +166,7 @@ fn decode_value(decoder: &mut Decoder) -> Result<StoredData, Error> {
|
||||
|
||||
pub struct NoteContext {
|
||||
entries: HashMap<String, StoredData>,
|
||||
aliases: HashMap<String, String>,
|
||||
output: Box<dyn Write>,
|
||||
}
|
||||
|
||||
@@ -158,6 +174,7 @@ impl NoteContext {
|
||||
pub fn default() -> Self {
|
||||
Self {
|
||||
entries: Default::default(),
|
||||
aliases: Default::default(),
|
||||
output: Box::new(std::io::stdout()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user