Compare commits
No commits in common. "b78ef63bdf72946ca85b3b37a80c2202504a9b1a" and "559afb01fa4e82b635952d654f2afb066f9583c8" have entirely different histories.
b78ef63bdf
...
559afb01fa
5 changed files with 697 additions and 157 deletions
|
@ -10,8 +10,10 @@ keywords = ["git-config", "git", "config", "gitoxide"]
|
|||
categories = ["config", "parser-implementations"]
|
||||
exclude = ["fuzz/**/*", ".vscode/**/*", "benches/**/*"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[features]
|
||||
# serde = ["serde_crate"]
|
||||
serde = ["serde_crate"]
|
||||
|
||||
[dependencies]
|
||||
memchr = "2"
|
||||
|
|
410
src/de.rs
Normal file
410
src/de.rs
Normal file
|
@ -0,0 +1,410 @@
|
|||
use std::ops::{AddAssign, MulAssign, Neg};
|
||||
|
||||
use crate::values::Boolean;
|
||||
use crate::{
|
||||
error::{Error, Result},
|
||||
values::PeekParse,
|
||||
};
|
||||
use serde::de::{
|
||||
self, DeserializeSeed, EnumAccess, IntoDeserializer, MapAccess, SeqAccess, VariantAccess,
|
||||
Visitor,
|
||||
};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
pub struct Deserializer<'de> {
|
||||
// This string starts with the input data and characters are truncated off
|
||||
// the beginning as data is parsed.
|
||||
input: &'de str,
|
||||
}
|
||||
|
||||
impl<'de> Deserializer<'de> {
|
||||
// By convention, `Deserializer` constructors are named like `from_xyz`.
|
||||
// That way basic use cases are satisfied by something like
|
||||
// `serde_json::from_str(...)` while advanced use cases that require a
|
||||
// deserializer can make one with `serde_json::Deserializer::from_str(...)`.
|
||||
pub fn from_str(input: &'de str) -> Self {
|
||||
Deserializer { input }
|
||||
}
|
||||
}
|
||||
|
||||
// By convention, the public API of a Serde deserializer is one or more
|
||||
// `from_xyz` methods such as `from_str`, `from_bytes`, or `from_reader`
|
||||
// depending on what Rust types the deserializer is able to consume as input.
|
||||
//
|
||||
// This basic deserializer supports only `from_str`.
|
||||
pub fn from_str<'a, T>(s: &'a str) -> Result<T>
|
||||
where
|
||||
T: Deserialize<'a>,
|
||||
{
|
||||
let mut deserializer = Deserializer::from_str(s);
|
||||
let t = T::deserialize(&mut deserializer)?;
|
||||
if deserializer.input.is_empty() {
|
||||
Ok(t)
|
||||
} else {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserializer<'de> {
|
||||
fn peek(&mut self) -> Result<char> {
|
||||
self.input.chars().next().ok_or(Error::Eof)
|
||||
}
|
||||
|
||||
fn next(&mut self) -> Result<char> {
|
||||
let ch = self.peek()?;
|
||||
self.input = &self.input[ch.len_utf8()..];
|
||||
Ok(ch)
|
||||
}
|
||||
|
||||
fn parse_bool(&mut self) -> Result<bool> {
|
||||
let (value, size) = Boolean::peek_parse(self.input)?;
|
||||
self.input = &self.input[size..];
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
fn parse_unsigned<T>(&mut self) -> Result<T> {
|
||||
self.parse_int(true)
|
||||
}
|
||||
|
||||
fn parse_signed<T>(&mut self) -> Result<T> {
|
||||
self.parse_int(false)
|
||||
}
|
||||
|
||||
fn parse_int<T>(&mut self, positive_only: bool) -> Result<T> {
|
||||
self.consume_whitespace()?;
|
||||
|
||||
match self.next()? {
|
||||
c @ '0'..='9' => {
|
||||
let mut significand = (c as u8 - b'0') as u64;
|
||||
|
||||
loop {
|
||||
match self.peek()? {
|
||||
c @ '0'..='9' => {
|
||||
let digit = (c as u8 - b'0') as u64;
|
||||
|
||||
if significand.wrapping_mul(10).wrapping_add(digit) < u64::MAX {}
|
||||
|
||||
let _ = self.next();
|
||||
significand = significand * 10 + digit;
|
||||
}
|
||||
_ => {
|
||||
// return self.parse_number(positive, significand);
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => Err(Error::InvalidInteger),
|
||||
}
|
||||
}
|
||||
|
||||
fn consume_whitespace(&mut self) -> Result<()> {
|
||||
loop {
|
||||
match self.peek()? {
|
||||
' ' | '\n' | '\t' | '\r' => {
|
||||
let _ = self.next();
|
||||
}
|
||||
_ => {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
match self.peek()? {
|
||||
_ => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_bool(self.parse_bool()?)
|
||||
}
|
||||
|
||||
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_i8(self.parse_signed()?)
|
||||
}
|
||||
|
||||
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_i16(self.parse_signed()?)
|
||||
}
|
||||
|
||||
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_i32(self.parse_signed()?)
|
||||
}
|
||||
|
||||
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_i64(self.parse_signed()?)
|
||||
}
|
||||
|
||||
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_u8(self.parse_unsigned()?)
|
||||
}
|
||||
|
||||
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_u16(self.parse_unsigned()?)
|
||||
}
|
||||
|
||||
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_u32(self.parse_unsigned()?)
|
||||
}
|
||||
|
||||
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_u64(self.parse_unsigned()?)
|
||||
}
|
||||
|
||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_f64(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
unimplemented!("Cannot deserialize into a float value! Use a integer variant instead.")
|
||||
}
|
||||
|
||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_str(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_str(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_seq(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_tuple_struct<V>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_len: usize,
|
||||
visitor: V,
|
||||
) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.deserialize_seq(visitor)
|
||||
}
|
||||
|
||||
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
self.consume_whitespace()?;
|
||||
Ok(visitor.visit_map(self)?)
|
||||
}
|
||||
|
||||
fn deserialize_struct<V>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_fields: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
visitor.visit_map(self)
|
||||
}
|
||||
|
||||
fn deserialize_enum<V>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variants: &'static [&'static str],
|
||||
visitor: V,
|
||||
) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
|
||||
where
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, 'a> MapAccess<'de> for Deserializer<'de> {
|
||||
type Error = Error;
|
||||
|
||||
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
|
||||
where
|
||||
K: DeserializeSeed<'de>,
|
||||
{
|
||||
// A map section is ended when another section begins or we hit EOL.
|
||||
// Therefore, we only check if a next section begins or in the case of
|
||||
// EOL indicate that we're done.
|
||||
if self.peek().unwrap_or('[') == '[' {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
seed.deserialize(self).map(Some)
|
||||
}
|
||||
|
||||
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
|
||||
where
|
||||
V: DeserializeSeed<'de>,
|
||||
{
|
||||
seed.deserialize(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod deserialize {
|
||||
use crate::from_str;
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
#[test]
|
||||
fn unit() {
|
||||
#[derive(Deserialize, PartialEq, Debug)]
|
||||
struct Test;
|
||||
assert_eq!(Test, from_str("").unwrap());
|
||||
assert_eq!((), from_str("").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn float() {
|
||||
from_str::<f64>("").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
#[derive(Deserialize, PartialEq, Debug)]
|
||||
struct Config {
|
||||
user: User,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, PartialEq, Debug)]
|
||||
struct User {
|
||||
email: String,
|
||||
name: String,
|
||||
}
|
||||
|
||||
let expected = Config {
|
||||
user: User {
|
||||
email: "code@eddie.sh".to_string(),
|
||||
name: "Edward Shen".to_string(),
|
||||
},
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
expected,
|
||||
from_str("[user]\nemail=code@eddie.sh\nname=Edward Shen\n").unwrap()
|
||||
);
|
||||
}
|
||||
}
|
45
src/error.rs
Normal file
45
src/error.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use std::fmt::{self, Display};
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
use serde::{de, ser};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
// This is a bare-bones implementation. A real library would provide additional
|
||||
// information in its error type, for example the line and column at which the
|
||||
// error occurred, the byte offset into the input, or the current key being
|
||||
// processed.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
Message(String),
|
||||
Eof,
|
||||
InvalidInteger,
|
||||
InvalidBoolean(String),
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl ser::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl de::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Error::Message(msg) => formatter.write_str(msg),
|
||||
Error::Eof => formatter.write_str("unexpected end of input"),
|
||||
Error::InvalidInteger => formatter.write_str("invalid integer given"),
|
||||
Error::InvalidBoolean(_) => formatter.write_str("invalid boolean given"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
197
src/file.rs
197
src/file.rs
|
@ -92,13 +92,6 @@ impl MutableValue<'_, '_, '_> {
|
|||
.ok_or(GitConfigError::KeyDoesNotExist(self.key))
|
||||
}
|
||||
|
||||
/// Update the value to the provided one. This modifies the value such that
|
||||
/// the Value event(s) are replaced with a single new event containing the
|
||||
/// new value.
|
||||
pub fn set_string(&mut self, input: String) {
|
||||
self.set_bytes(input.into_bytes());
|
||||
}
|
||||
|
||||
/// Update the value to the provided one. This modifies the value such that
|
||||
/// the Value event(s) are replaced with a single new event containing the
|
||||
/// new value.
|
||||
|
@ -108,116 +101,20 @@ impl MutableValue<'_, '_, '_> {
|
|||
self.section
|
||||
.insert(self.index, Event::Value(Cow::Owned(input)));
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MutableMultiValue<'borrow, 'lookup, 'event> {
|
||||
section: &'borrow mut HashMap<SectionId, Vec<Event<'event>>>,
|
||||
key: &'lookup str,
|
||||
indices_and_sizes: Vec<(SectionId, usize, usize)>,
|
||||
}
|
||||
|
||||
impl<'event> MutableMultiValue<'_, '_, 'event> {
|
||||
/// Returns the actual value. This is computed each time this is called, so
|
||||
/// it's best to reuse this value or own it if an allocation is acceptable.
|
||||
pub fn value(&self) -> Result<Vec<Cow<'_, [u8]>>, GitConfigError> {
|
||||
let mut found_key = false;
|
||||
let mut values = vec![];
|
||||
let mut partial_value = None;
|
||||
// section_id is guaranteed to exist in self.sections, else we have a
|
||||
// violated invariant.
|
||||
for (section_id, index, size) in &self.indices_and_sizes {
|
||||
for event in &self.section.get(section_id).unwrap()[*index..*size] {
|
||||
match event {
|
||||
Event::Key(event_key) if *event_key == self.key => found_key = true,
|
||||
Event::Value(v) if found_key => {
|
||||
found_key = false;
|
||||
values.push(normalize_bytes(v.borrow()));
|
||||
}
|
||||
Event::ValueNotDone(v) if found_key => {
|
||||
partial_value = Some((*v).to_vec());
|
||||
}
|
||||
Event::ValueDone(v) if found_key => {
|
||||
found_key = false;
|
||||
partial_value.as_mut().unwrap().extend(&**v);
|
||||
values.push(normalize_vec(partial_value.take().unwrap()));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
/// Update the value to the provided one. This modifies the value such that
|
||||
/// the Value event(s) are replaced with a single new event containing the
|
||||
/// new value.
|
||||
pub fn set_string(&mut self, input: String) {
|
||||
self.set_bytes(input.into_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
if values.is_empty() {
|
||||
return Err(GitConfigError::KeyDoesNotExist(self.key));
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.indices_and_sizes.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.indices_and_sizes.is_empty()
|
||||
}
|
||||
|
||||
pub fn set_string(&mut self, index: usize, input: String) {
|
||||
self.set_bytes(index, input.into_bytes())
|
||||
}
|
||||
|
||||
pub fn set_bytes(&mut self, index: usize, input: Vec<u8>) {
|
||||
self.set_value(index, Cow::Owned(input))
|
||||
}
|
||||
|
||||
pub fn set_value(&mut self, index: usize, input: Cow<'event, [u8]>) {
|
||||
let (section_id, index, size) = &mut self.indices_and_sizes[index];
|
||||
self.section
|
||||
.get_mut(section_id)
|
||||
.unwrap()
|
||||
.drain(*index..*index + *size);
|
||||
*size = 1;
|
||||
self.section
|
||||
.get_mut(section_id)
|
||||
.unwrap()
|
||||
.insert(*index, Event::Value(input));
|
||||
}
|
||||
|
||||
pub fn set_values(&mut self, input: impl Iterator<Item = Cow<'event, [u8]>>) {
|
||||
for ((section_id, index, size), value) in self.indices_and_sizes.iter_mut().zip(input) {
|
||||
self.section
|
||||
.get_mut(section_id)
|
||||
.unwrap()
|
||||
.drain(*index..*index + *size);
|
||||
*size = 1;
|
||||
self.section
|
||||
.get_mut(section_id)
|
||||
.unwrap()
|
||||
.insert(*index, Event::Value(value));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_string_all(&mut self, input: String) {
|
||||
self.set_bytes_all(input.into_bytes())
|
||||
}
|
||||
|
||||
pub fn set_bytes_all(&mut self, input: Vec<u8>) {
|
||||
self.set_values_all(Cow::Owned(input))
|
||||
}
|
||||
|
||||
pub fn set_values_all(&mut self, input: Cow<'event, [u8]>) {
|
||||
for (section_id, index, size) in &mut self.indices_and_sizes {
|
||||
self.section
|
||||
.get_mut(section_id)
|
||||
.unwrap()
|
||||
.drain(*index..*index + *size);
|
||||
*size = 1;
|
||||
self.section
|
||||
.get_mut(section_id)
|
||||
.unwrap()
|
||||
.insert(*index, Event::Value(input.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
// pub struct MutableMultiValue<'borrow, 'lookup, 'event> {
|
||||
// section: &'borrow mut Vec<Event<'event>>,
|
||||
// key: &'lookup str,
|
||||
// indices_and_sizes: Vec<(usize, usize)>,
|
||||
// }
|
||||
|
||||
/// High level `git-config` reader and writer.
|
||||
///
|
||||
|
@ -420,7 +317,7 @@ impl<'event> GitConfig<'event> {
|
|||
|
||||
// section_id is guaranteed to exist in self.sections, else we have a
|
||||
// violated invariant.
|
||||
for event in self.sections.get(section_id).unwrap() {
|
||||
for event in self.sections.get(§ion_id).unwrap() {
|
||||
match event {
|
||||
Event::Key(event_key) if *event_key == key => found_key = true,
|
||||
Event::Value(v) if found_key => {
|
||||
|
@ -439,8 +336,10 @@ impl<'event> GitConfig<'event> {
|
|||
_ => (),
|
||||
}
|
||||
}
|
||||
if let Some(v) = latest_value.or_else(|| partial_value.map(normalize_vec)) {
|
||||
return Ok(v);
|
||||
|
||||
match latest_value.or_else(|| partial_value.map(normalize_vec)) {
|
||||
Some(v) => return Ok(v),
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -470,7 +369,7 @@ impl<'event> GitConfig<'event> {
|
|||
let mut size = 0;
|
||||
let mut index = 0;
|
||||
let mut found_key = false;
|
||||
for (i, event) in self.sections.get(section_id).unwrap().iter().enumerate() {
|
||||
for (i, event) in self.sections.get(§ion_id).unwrap().iter().enumerate() {
|
||||
match event {
|
||||
Event::Key(event_key) if *event_key == key => found_key = true,
|
||||
Event::Value(_) if found_key => {
|
||||
|
@ -495,7 +394,7 @@ impl<'event> GitConfig<'event> {
|
|||
}
|
||||
|
||||
return Ok(MutableValue {
|
||||
section: self.sections.get_mut(section_id).unwrap(),
|
||||
section: self.sections.get_mut(§ion_id).unwrap(),
|
||||
key,
|
||||
size,
|
||||
index,
|
||||
|
@ -615,9 +514,9 @@ impl<'event> GitConfig<'event> {
|
|||
/// Cow::Borrowed(b"d")
|
||||
/// ]
|
||||
/// );
|
||||
///
|
||||
/// git_config.get_raw_multi_value_mut("core", None, "a")?.set_string_all("g".to_string());
|
||||
///
|
||||
/// for value in git_config.get_raw_multi_value_mut("core", None, "a")? {
|
||||
/// *value = Cow::Borrowed(b"g");
|
||||
///}
|
||||
/// assert_eq!(
|
||||
/// git_config.get_raw_multi_value("core", None, "a")?,
|
||||
/// vec![
|
||||
|
@ -645,45 +544,42 @@ impl<'event> GitConfig<'event> {
|
|||
section_name: &'lookup str,
|
||||
subsection_name: Option<&'lookup str>,
|
||||
key: &'lookup str,
|
||||
) -> Result<MutableMultiValue<'_, 'lookup, 'event>, GitConfigError<'lookup>> {
|
||||
) -> Result<Vec<&mut Cow<'event, [u8]>>, GitConfigError<'lookup>> {
|
||||
let key = key;
|
||||
let section_ids = self
|
||||
.get_section_ids_by_name_and_subname(section_name, subsection_name)?
|
||||
.to_vec();
|
||||
|
||||
let mut indices = vec![];
|
||||
for section_id in section_ids.iter().rev() {
|
||||
let mut size = 0;
|
||||
let mut index = 0;
|
||||
let mut found_key = false;
|
||||
for (i, event) in self.sections.get(section_id).unwrap().iter().enumerate() {
|
||||
let values: Vec<&mut Cow<'event, [u8]>> = self
|
||||
.sections
|
||||
.iter_mut()
|
||||
.filter_map(|(k, v)| {
|
||||
if section_ids.contains(k) {
|
||||
let mut values = vec![];
|
||||
for event in v {
|
||||
match event {
|
||||
Event::Key(event_key) if *event_key == key => found_key = true,
|
||||
Event::Value(_) if found_key => {
|
||||
indices.push((*section_id, i, 1));
|
||||
Event::Value(v) if found_key => {
|
||||
values.push(v);
|
||||
found_key = false;
|
||||
}
|
||||
Event::ValueNotDone(_) if found_key => {
|
||||
size = 1;
|
||||
index = i;
|
||||
}
|
||||
Event::ValueDone(_) if found_key => {
|
||||
found_key = false;
|
||||
size += 1;
|
||||
indices.push((*section_id, index, size));
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !indices.is_empty() {
|
||||
Ok(MutableMultiValue {
|
||||
section: &mut self.sections,
|
||||
key,
|
||||
indices_and_sizes: indices,
|
||||
})
|
||||
Some(values)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
|
||||
// TODO: return mutable entry instead
|
||||
// TODO: support partial values
|
||||
if values.is_empty() {
|
||||
Err(GitConfigError::KeyDoesNotExist(key))
|
||||
} else {
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -821,8 +717,11 @@ impl<'event> GitConfig<'event> {
|
|||
key: &'lookup str,
|
||||
new_values: impl Iterator<Item = Cow<'event, [u8]>>,
|
||||
) -> Result<(), GitConfigError<'lookup>> {
|
||||
self.get_raw_multi_value_mut(section_name, subsection_name, key)
|
||||
.map(|mut v| v.set_values(new_values))
|
||||
let values = self.get_raw_multi_value_mut(section_name, subsection_name, key)?;
|
||||
for (old, new) in values.into_iter().zip(new_values) {
|
||||
*old = new;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
184
src/ser.rs
Normal file
184
src/ser.rs
Normal file
|
@ -0,0 +1,184 @@
|
|||
use crate::error::{Error, Result};
|
||||
use ser::SerializeSeq;
|
||||
use serde::{de::MapAccess, ser, Serialize};
|
||||
|
||||
pub struct Serializer {
|
||||
output: String,
|
||||
}
|
||||
|
||||
pub fn to_string<T>(value: &T) -> Result<String>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let mut serializer = Serializer {
|
||||
output: String::new(),
|
||||
};
|
||||
value.serialize(&mut serializer)?;
|
||||
Ok(serializer.output)
|
||||
}
|
||||
|
||||
impl<'a> ser::Serializer for &'a mut Serializer {
|
||||
type Ok = ();
|
||||
type Error = Error;
|
||||
type SerializeSeq = Self;
|
||||
type SerializeTuple = Self;
|
||||
type SerializeTupleStruct = Self;
|
||||
type SerializeTupleVariant = Self;
|
||||
type SerializeMap = Self;
|
||||
type SerializeStruct = Self;
|
||||
type SerializeStructVariant = Self;
|
||||
|
||||
fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
|
||||
self.output += if v { "true" } else { "false" };
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> Result<Self::Ok> {
|
||||
self.serialize_i64(i64::from(v))
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> Result<Self::Ok> {
|
||||
self.serialize_i64(i64::from(v))
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> Result<Self::Ok> {
|
||||
self.serialize_i64(i64::from(v))
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> Result<Self::Ok> {
|
||||
self.output += &v.to_string();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> Result<Self::Ok> {
|
||||
self.serialize_u64(u64::from(v))
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> Result<Self::Ok> {
|
||||
self.serialize_u64(u64::from(v))
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> Result<Self::Ok> {
|
||||
self.serialize_u64(u64::from(v))
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> Result<Self::Ok> {
|
||||
self.output += &v.to_string();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<Self::Ok> {
|
||||
self.serialize_f64(f64::from(v))
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> Result<Self::Ok> {
|
||||
self.output += &v.to_string();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_char(self, v: char) -> Result<Self::Ok> {
|
||||
self.output += &v.to_string();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
|
||||
self.output += v;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<Self::Ok> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<Self::Ok> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<Self::Ok> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized>(self, name: &'static str, value: &T) -> Result<Self::Ok>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized>(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<Self::Ok>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
name: &'static str,
|
||||
variant_index: u32,
|
||||
variant: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStructVariant> {
|
||||
todo!()
|
||||
}
|
||||
}
|
Reference in a new issue