helix-mods/helix-core/src/register.rs

84 lines
1.8 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
#[derive(Debug)]
pub struct Register {
name: char,
values: Vec<String>,
}
impl Register {
2021-10-24 10:28:29 +02:00
pub const fn new(name: char) -> Self {
Self {
name,
values: Vec::new(),
}
}
pub fn new_with_values(name: char, values: Vec<String>) -> Self {
2021-11-28 02:21:40 +01:00
if name == '_' {
Self::new(name)
} else {
Self { name, values }
}
}
2021-10-24 10:28:29 +02:00
pub const fn name(&self) -> char {
self.name
}
pub fn read(&self) -> &[String] {
&self.values
}
pub fn write(&mut self, values: Vec<String>) {
2021-11-28 02:21:40 +01:00
if self.name != '_' {
self.values = values;
}
}
pub fn push(&mut self, value: String) {
2021-11-28 02:21:40 +01:00
if self.name != '_' {
self.values.push(value);
}
}
2020-10-06 09:00:23 +02:00
}
/// Currently just wraps a `HashMap` of `Register`s
#[derive(Debug, Default)]
pub struct Registers {
inner: HashMap<char, Register>,
}
impl Registers {
pub fn get(&self, name: char) -> Option<&Register> {
self.inner.get(&name)
}
pub fn get_mut(&mut self, name: char) -> &mut Register {
self.inner
.entry(name)
.or_insert_with(|| Register::new(name))
}
pub fn write(&mut self, name: char, values: Vec<String>) {
self.inner
.insert(name, Register::new_with_values(name, values));
}
pub fn read(&self, name: char) -> Option<&[String]> {
self.get(name).map(|reg| reg.read())
}
2021-11-04 19:33:31 +01:00
pub fn first(&self, name: char) -> Option<&String> {
self.read(name).and_then(|entries| entries.first())
}
pub fn last(&self, name: char) -> Option<&String> {
self.read(name).and_then(|entries| entries.last())
}
2021-11-04 19:33:31 +01:00
pub fn inner(&self) -> &HashMap<char, Register> {
&self.inner
}
2020-10-06 09:00:23 +02:00
}