From 10548bf0e32209e02460021cc8ca4865c4c75bf7 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 23 Jun 2021 01:04:04 +0800 Subject: Fix previous broken refactor key into helix-view Need to be used for autoinfo Revert "Revert "Refactor key into helix-view"" This reverts commit 10f9f72232f5789323d689bf0f9cd359715770d6. --- helix-view/src/document.rs | 61 +++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'helix-view/src/document.rs') diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index dc004336..f26e9c1a 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -1,5 +1,7 @@ use anyhow::{anyhow, Context, Error}; +use serde::de::{self, Deserialize, Deserializer}; use std::cell::Cell; +use std::collections::HashMap; use std::fmt::Display; use std::future::Future; use std::path::{Component, Path, PathBuf}; @@ -17,8 +19,6 @@ use helix_core::{ use crate::{DocumentId, Theme, ViewId}; -use std::collections::HashMap; - const BUF_SIZE: usize = 8192; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] @@ -28,6 +28,40 @@ pub enum Mode { Insert, } +impl Display for Mode { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Mode::Normal => f.write_str("normal"), + Mode::Select => f.write_str("select"), + Mode::Insert => f.write_str("insert"), + } + } +} + +impl FromStr for Mode { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "normal" => Ok(Mode::Normal), + "select" => Ok(Mode::Select), + "insert" => Ok(Mode::Insert), + _ => Err(anyhow!("Invalid mode '{}'", s)), + } + } +} + +// toml deserializer doesn't seem to recognize string as enum +impl<'de> Deserialize<'de> for Mode { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + s.parse().map_err(de::Error::custom) + } +} + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum IndentStyle { Tabs, @@ -97,29 +131,6 @@ impl fmt::Debug for Document { } } -impl Display for Mode { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Mode::Normal => f.write_str("normal"), - Mode::Select => f.write_str("select"), - Mode::Insert => f.write_str("insert"), - } - } -} - -impl FromStr for Mode { - type Err = Error; - - fn from_str(s: &str) -> Result { - match s { - "normal" => Ok(Mode::Normal), - "select" => Ok(Mode::Select), - "insert" => Ok(Mode::Insert), - _ => Err(anyhow!("Invalid mode '{}'", s)), - } - } -} - // The documentation and implementation of this function should be up-to-date with // its sibling function, `to_writer()`. // -- cgit v1.2.3-70-g09d2