diff options
| author | diogo464 <[email protected]> | 2023-04-07 17:00:41 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2023-04-07 17:00:41 +0100 |
| commit | 11616308d0f0c531a4d3690e17bfe714234a9955 (patch) | |
| tree | 2943208afba25ca60bdff5cab70b3d742d6027e0 | |
| parent | 60647747a2e0531a2b7d2cdf4279395ae369c41c (diff) | |
removed unused code
| -rw-r--r-- | src/config/mod.rs | 97 | ||||
| -rw-r--r-- | src/config/parse.rs | 254 | ||||
| -rw-r--r-- | src/config/parser.rs | 20 |
3 files changed, 0 insertions, 371 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs deleted file mode 100644 index 98ba9fb..0000000 --- a/src/config/mod.rs +++ /dev/null | |||
| @@ -1,97 +0,0 @@ | |||
| 1 | mod parse; | ||
| 2 | |||
| 3 | use std::path::Path; | ||
| 4 | |||
| 5 | pub struct Config { | ||
| 6 | groups: Vec<Group>, | ||
| 7 | } | ||
| 8 | |||
| 9 | pub struct Group { | ||
| 10 | includes: Vec<IncludeAction>, | ||
| 11 | links: Vec<LinkAction>, | ||
| 12 | copies: Vec<CopyAction>, | ||
| 13 | } | ||
| 14 | |||
| 15 | pub struct IncludeAction { | ||
| 16 | group: String, | ||
| 17 | } | ||
| 18 | |||
| 19 | pub struct LinkAction { | ||
| 20 | source: String, | ||
| 21 | target: String, | ||
| 22 | } | ||
| 23 | |||
| 24 | pub struct CopyAction { | ||
| 25 | source: String, | ||
| 26 | target: String, | ||
| 27 | } | ||
| 28 | |||
| 29 | pub fn parse(content: &str) -> std::io::Result<Config> { | ||
| 30 | todo!() | ||
| 31 | } | ||
| 32 | |||
| 33 | pub fn parse_path(path: impl AsRef<Path>) -> std::io::Result<Config> { | ||
| 34 | todo!() | ||
| 35 | } | ||
| 36 | |||
| 37 | pub fn format(content: &str) -> std::io::Result<String> { | ||
| 38 | todo!() | ||
| 39 | } | ||
| 40 | |||
| 41 | pub fn format_path(path: impl AsRef<Path>) -> std::io::Result<String> { | ||
| 42 | todo!() | ||
| 43 | } | ||
| 44 | |||
| 45 | pub fn format_inplace(path: impl AsRef<Path>) -> std::io::Result<()> { | ||
| 46 | todo!() | ||
| 47 | } | ||
| 48 | |||
| 49 | impl Config { | ||
| 50 | pub fn groups(&self) -> impl Iterator<Item = &Group> { | ||
| 51 | std::iter::empty() | ||
| 52 | } | ||
| 53 | |||
| 54 | pub fn group(&self, name: &str) -> Option<&Group> { | ||
| 55 | todo!() | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | impl Group { | ||
| 60 | pub fn groups(&self) -> impl Iterator<Item = &IncludeAction> { | ||
| 61 | std::iter::empty() | ||
| 62 | } | ||
| 63 | |||
| 64 | pub fn links(&self) -> impl Iterator<Item = &LinkAction> { | ||
| 65 | std::iter::empty() | ||
| 66 | } | ||
| 67 | |||
| 68 | pub fn copies(&self) -> impl Iterator<Item = &CopyAction> { | ||
| 69 | std::iter::empty() | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | impl IncludeAction { | ||
| 74 | pub fn name(&self) -> &str { | ||
| 75 | todo!() | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | impl LinkAction { | ||
| 80 | pub fn source(&self) -> &str { | ||
| 81 | todo!() | ||
| 82 | } | ||
| 83 | |||
| 84 | pub fn dest(&self) -> &str { | ||
| 85 | todo!() | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | impl CopyAction { | ||
| 90 | pub fn source(&self) -> &str { | ||
| 91 | todo!() | ||
| 92 | } | ||
| 93 | |||
| 94 | pub fn dest(&self) -> &str { | ||
| 95 | todo!() | ||
| 96 | } | ||
| 97 | } | ||
diff --git a/src/config/parse.rs b/src/config/parse.rs deleted file mode 100644 index f1e33b0..0000000 --- a/src/config/parse.rs +++ /dev/null | |||
| @@ -1,254 +0,0 @@ | |||
| 1 | use nom::{ | ||
| 2 | branch::alt, | ||
| 3 | bytes::complete::{tag, take_while, take_while1}, | ||
| 4 | character::complete::{alphanumeric0, alphanumeric1, multispace0, space1}, | ||
| 5 | combinator::map, | ||
| 6 | multi::{many0, separated_list0}, | ||
| 7 | sequence::{delimited, preceded}, | ||
| 8 | }; | ||
| 9 | |||
| 10 | type Span<'s> = nom_locate::LocatedSpan<&'s str>; | ||
| 11 | type IResult<'s, I, O, E = ParserError<'s>> = nom::IResult<I, O, E>; | ||
| 12 | |||
| 13 | #[derive(Debug, PartialEq, Eq)] | ||
| 14 | struct ParserError<'s> { | ||
| 15 | location: Span<'s>, | ||
| 16 | message: Option<String>, | ||
| 17 | } | ||
| 18 | |||
| 19 | #[derive(Debug)] | ||
| 20 | struct KeyValueParser<'s> { | ||
| 21 | span: Span<'s>, | ||
| 22 | kvs: Vec<KeyValue<'s>>, | ||
| 23 | } | ||
| 24 | |||
| 25 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 26 | struct KeyValue<'s> { | ||
| 27 | key: &'s str, | ||
| 28 | value: &'s str, | ||
| 29 | } | ||
| 30 | |||
| 31 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
| 32 | struct LinkAction { | ||
| 33 | source: String, | ||
| 34 | target: String, | ||
| 35 | } | ||
| 36 | |||
| 37 | enum RichAction { | ||
| 38 | Link(LinkAction), | ||
| 39 | } | ||
| 40 | |||
| 41 | struct RichGroup { | ||
| 42 | name: String, | ||
| 43 | items: Vec<RichItem>, | ||
| 44 | } | ||
| 45 | |||
| 46 | enum RichItem { | ||
| 47 | Group(RichGroup), | ||
| 48 | } | ||
| 49 | |||
| 50 | struct RichConfig {} | ||
| 51 | |||
| 52 | impl<'s> ParserError<'s> { | ||
| 53 | fn custom(location: Span<'s>, message: impl Into<String>) -> Self { | ||
| 54 | Self { | ||
| 55 | location, | ||
| 56 | message: Some(message.into()), | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | fn missing_key(span: Span<'s>, key: &'s str) -> Self { | ||
| 61 | Self::custom(span, format!("missing key: {key}")) | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | impl<'s> From<ParserError<'s>> for nom::Err<ParserError<'s>> { | ||
| 66 | fn from(e: ParserError<'s>) -> Self { | ||
| 67 | Self::Failure(e) | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | impl<'s> nom::error::ParseError<Span<'s>> for ParserError<'s> { | ||
| 72 | fn from_error_kind(input: Span<'s>, kind: nom::error::ErrorKind) -> Self { | ||
| 73 | Self::custom(input, format!("error kind: {kind:?}")) | ||
| 74 | } | ||
| 75 | |||
| 76 | fn append(input: Span, kind: nom::error::ErrorKind, other: Self) -> Self { | ||
| 77 | todo!() | ||
| 78 | } | ||
| 79 | |||
| 80 | fn or(self, other: Self) -> Self { | ||
| 81 | other | ||
| 82 | } | ||
| 83 | |||
| 84 | fn from_char(input: Span<'s>, c: char) -> Self { | ||
| 85 | Self::custom(input, format!("invalid character: {c}")) | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | impl<'s> KeyValueParser<'s> { | ||
| 90 | fn new(span: Span<'s>, kvs: Vec<KeyValue<'s>>) -> Self { | ||
| 91 | Self { span, kvs } | ||
| 92 | } | ||
| 93 | |||
| 94 | fn get(&self, key: &'static str) -> Option<&'s str> { | ||
| 95 | self.kvs.iter().find(|kv| kv.key == key).map(|kv| kv.value) | ||
| 96 | } | ||
| 97 | |||
| 98 | fn expect(&self, key: &'static str) -> Result<&'s str, ParserError<'s>> { | ||
| 99 | self.get(key) | ||
| 100 | .ok_or(ParserError::missing_key(self.span, key)) | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | fn is_value_char(c: char) -> bool { | ||
| 105 | c.is_alphanumeric() || c == '_' || c == '-' || c == '.' || c == '/' | ||
| 106 | } | ||
| 107 | |||
| 108 | fn whitespace0(i: Span) -> IResult<Span, Span> { | ||
| 109 | take_while(char::is_whitespace)(i) | ||
| 110 | } | ||
| 111 | |||
| 112 | fn whitespace1(i: Span) -> IResult<Span, Span> { | ||
| 113 | take_while1(char::is_whitespace)(i) | ||
| 114 | } | ||
| 115 | |||
| 116 | fn linesep(i: Span) -> IResult<Span, Span> { | ||
| 117 | take_while(|c: char| c.is_whitespace() && c != '\n')(i)?; | ||
| 118 | take_while(|c: char| c == '\n')(i)?; | ||
| 119 | take_while(char::is_whitespace)(i) | ||
| 120 | } | ||
| 121 | |||
| 122 | fn keyvalue(i: Span) -> IResult<Span, KeyValue> { | ||
| 123 | let (i, key) = alphanumeric1(i)?; | ||
| 124 | let (i, _) = tag("=")(i)?; | ||
| 125 | let (i, val) = delimited(tag("\""), take_while(is_value_char), tag("\""))(i)?; | ||
| 126 | Ok(( | ||
| 127 | i, | ||
| 128 | KeyValue { | ||
| 129 | key: key.fragment(), | ||
| 130 | value: val.fragment(), | ||
| 131 | }, | ||
| 132 | )) | ||
| 133 | } | ||
| 134 | |||
| 135 | fn keyvalues(i: Span) -> IResult<Span, Vec<KeyValue>> { | ||
| 136 | separated_list0(space1, keyvalue)(i) | ||
| 137 | } | ||
| 138 | |||
| 139 | fn link_action(i: Span) -> IResult<Span, LinkAction> { | ||
| 140 | let (i, kvs) = preceded(tag("link"), preceded(space1, keyvalues))(i)?; | ||
| 141 | eprintln!("{kvs:#?}"); | ||
| 142 | eprintln!("{i:?}"); | ||
| 143 | let kvparser = KeyValueParser::new(i, kvs); | ||
| 144 | let src = kvparser.expect("src")?.to_string(); | ||
| 145 | let dst = kvparser.expect("dst")?.to_string(); | ||
| 146 | Ok(( | ||
| 147 | i, | ||
| 148 | LinkAction { | ||
| 149 | source: src, | ||
| 150 | target: dst, | ||
| 151 | }, | ||
| 152 | )) | ||
| 153 | } | ||
| 154 | |||
| 155 | fn rich_action(i: Span) -> IResult<Span, RichAction> { | ||
| 156 | todo!() | ||
| 157 | } | ||
| 158 | |||
| 159 | fn rich_group(i: Span) -> IResult<Span, RichGroup> { | ||
| 160 | let mut header = preceded(tag("group"), preceded(multispace0, alphanumeric1)); | ||
| 161 | let mut open_bracket = delimited(multispace0, tag("{"), multispace0); | ||
| 162 | let mut close_bracket = preceded(multispace0, tag("}")); | ||
| 163 | let mut body = separated_list0(linesep, rich_item); | ||
| 164 | |||
| 165 | let (i, name) = header(i)?; | ||
| 166 | let (i, _) = open_bracket(i)?; | ||
| 167 | let (i, items) = body(i)?; | ||
| 168 | let (i, _) = close_bracket(i)?; | ||
| 169 | |||
| 170 | Ok(( | ||
| 171 | i, | ||
| 172 | RichGroup { | ||
| 173 | name: name.to_string(), | ||
| 174 | items, | ||
| 175 | }, | ||
| 176 | )) | ||
| 177 | } | ||
| 178 | |||
| 179 | fn rich_item(i: Span) -> IResult<Span, RichItem> { | ||
| 180 | alt((map(rich_group, RichItem::Group),))(i) | ||
| 181 | } | ||
| 182 | |||
| 183 | fn config(i: Span) -> IResult<Span, RichConfig> { | ||
| 184 | let (_, groups) = many0(rich_group)(i)?; | ||
| 185 | todo!() | ||
| 186 | } | ||
| 187 | |||
| 188 | #[cfg(test)] | ||
| 189 | mod tests { | ||
| 190 | use super::*; | ||
| 191 | |||
| 192 | #[test] | ||
| 193 | fn parse_keyvalue() { | ||
| 194 | let input = Span::new(r#"key="value""#); | ||
| 195 | let (rem, kv) = keyvalue(input).unwrap(); | ||
| 196 | assert!(rem.is_empty()); | ||
| 197 | assert_eq!( | ||
| 198 | kv, | ||
| 199 | KeyValue { | ||
| 200 | key: "key", | ||
| 201 | value: "value" | ||
| 202 | } | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | |||
| 206 | #[test] | ||
| 207 | fn parse_keyvalues() { | ||
| 208 | let kvs = vec![ | ||
| 209 | KeyValue { | ||
| 210 | key: "key1", | ||
| 211 | value: "value1", | ||
| 212 | }, | ||
| 213 | KeyValue { | ||
| 214 | key: "key2", | ||
| 215 | value: "value2", | ||
| 216 | }, | ||
| 217 | ]; | ||
| 218 | |||
| 219 | let input = Span::new(r#"key1="value1" key2="value2""#); | ||
| 220 | let (rem, res) = keyvalues(input).unwrap(); | ||
| 221 | assert!(rem.is_empty()); | ||
| 222 | assert_eq!(res, kvs); | ||
| 223 | |||
| 224 | let kvs = vec![ | ||
| 225 | KeyValue { | ||
| 226 | key: "src", | ||
| 227 | value: "tmux/", | ||
| 228 | }, | ||
| 229 | KeyValue { | ||
| 230 | key: "dst", | ||
| 231 | value: ".config/tmux", | ||
| 232 | }, | ||
| 233 | ]; | ||
| 234 | |||
| 235 | let input = Span::new(r#"src="tmux/" dst=".config/tmux""#); | ||
| 236 | let (rem, res) = keyvalues(input).unwrap(); | ||
| 237 | assert!(rem.is_empty()); | ||
| 238 | assert_eq!(res, kvs); | ||
| 239 | } | ||
| 240 | |||
| 241 | #[test] | ||
| 242 | fn parse_link_action() { | ||
| 243 | let input = Span::new(r#"link src="tmux/" dst=".config/tmux""#); | ||
| 244 | let (rem, res) = link_action(input).unwrap(); | ||
| 245 | assert!(rem.is_empty()); | ||
| 246 | assert_eq!( | ||
| 247 | res, | ||
| 248 | LinkAction { | ||
| 249 | source: "tmux/".to_string(), | ||
| 250 | target: ".config/tmux".to_string() | ||
| 251 | } | ||
| 252 | ); | ||
| 253 | } | ||
| 254 | } | ||
diff --git a/src/config/parser.rs b/src/config/parser.rs deleted file mode 100644 index 102f15a..0000000 --- a/src/config/parser.rs +++ /dev/null | |||
| @@ -1,20 +0,0 @@ | |||
| 1 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 2 | struct Location { | ||
| 3 | line: u32, | ||
| 4 | column: u32, | ||
| 5 | } | ||
| 6 | |||
| 7 | #[derive(Debug, Clone)] | ||
| 8 | struct Scanner<'s> { | ||
| 9 | location: Location, | ||
| 10 | content: std::str::Chars<'s>, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl<'s> Scanner<'s> { | ||
| 14 | fn new(content: &'s str) -> Self { | ||
| 15 | Self { | ||
| 16 | location: Default::default(), | ||
| 17 | content: content.chars(), | ||
| 18 | } | ||
| 19 | } | ||
| 20 | } | ||
