1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
use nu_errors::ShellError; use nu_protocol::{did_you_mean, ColumnPath, Primitive, ShellTypeName, UntaggedValue, Value}; use nu_source::{span_for_spanned_list, HasSpan, SpannedItem, Tagged}; use nu_value_ext::{get_data_by_column_path, ValueExt}; #[derive(Debug, Eq, PartialEq)] pub enum Action { SemVerAction(SemVerAction), Default, } #[derive(Debug, Eq, PartialEq)] pub enum SemVerAction { Major, Minor, Patch, } #[derive(Default)] pub struct Inc { pub field: Option<Tagged<ColumnPath>>, pub error: Option<String>, pub action: Option<Action>, } impl Inc { pub fn new() -> Self { Default::default() } fn apply(&self, input: &str) -> UntaggedValue { match &self.action { Some(Action::SemVerAction(act_on)) => { let mut ver = match semver::Version::parse(&input) { Ok(parsed_ver) => parsed_ver, Err(_) => return UntaggedValue::string(input.to_string()), }; match act_on { SemVerAction::Major => ver.increment_major(), SemVerAction::Minor => ver.increment_minor(), SemVerAction::Patch => ver.increment_patch(), } UntaggedValue::string(ver.to_string()) } Some(Action::Default) | None => match input.parse::<u64>() { Ok(v) => UntaggedValue::string(format!("{}", v + 1)), Err(_) => UntaggedValue::string(input), }, } } pub fn for_semver(&mut self, part: SemVerAction) { if self.permit() { self.action = Some(Action::SemVerAction(part)); } else { self.log_error("can only apply one"); } } fn permit(&mut self) -> bool { self.action.is_none() } fn log_error(&mut self, message: &str) { self.error = Some(message.to_string()); } pub fn usage() -> &'static str { "Usage: inc field [--major|--minor|--patch]" } pub fn inc(&self, value: Value) -> Result<Value, ShellError> { match &value.value { UntaggedValue::Primitive(Primitive::Int(i)) => { Ok(UntaggedValue::int(i + 1).into_value(value.tag())) } UntaggedValue::Primitive(Primitive::Filesize(b)) => { Ok(UntaggedValue::filesize(b + 1_u64).into_value(value.tag())) } UntaggedValue::Primitive(Primitive::String(ref s)) => { Ok(self.apply(&s).into_value(value.tag())) } UntaggedValue::Table(values) => { if values.len() == 1 { Ok(UntaggedValue::Table(vec![self.inc(values[0].clone())?]) .into_value(value.tag())) } else { Err(ShellError::type_error( "incrementable value", value.type_name().spanned(value.span()), )) } } UntaggedValue::Row(_) => match self.field { Some(ref f) => { let fields = f.clone(); let replace_for = get_data_by_column_path( &value, &f, move |obj_source, column_path_tried, _| match did_you_mean( &obj_source, column_path_tried.as_string(), ) { Some(suggestions) => ShellError::labeled_error( "Unknown column", format!("did you mean '{}'?", suggestions[0]), span_for_spanned_list(fields.iter().map(|p| p.span)), ), None => ShellError::labeled_error( "Unknown column", "row does not contain this column", span_for_spanned_list(fields.iter().map(|p| p.span)), ), }, ); let got = replace_for?; let replacement = self.inc(got)?; value .replace_data_at_column_path(&f, replacement.value.into_untagged_value()) .ok_or_else(|| { ShellError::labeled_error( "inc could not find field to replace", "column name", value.tag(), ) }) } None => Err(ShellError::untagged_runtime_error( "inc needs a field when incrementing a column in a table", )), }, _ => Err(ShellError::type_error( "incrementable value", value.type_name().spanned(value.span()), )), } } } #[cfg(test)] mod tests { mod semver { use crate::inc::SemVerAction; use crate::Inc; use nu_test_support::value::string; #[test] fn major() { let mut inc = Inc::new(); inc.for_semver(SemVerAction::Major); assert_eq!(inc.apply("0.1.3"), string("1.0.0").value); } #[test] fn minor() { let mut inc = Inc::new(); inc.for_semver(SemVerAction::Minor); assert_eq!(inc.apply("0.1.3"), string("0.2.0").value); } #[test] fn patch() { let mut inc = Inc::new(); inc.for_semver(SemVerAction::Patch); assert_eq!(inc.apply("0.1.3"), string("0.1.4").value); } } }