/// <summary> /// Sets the action to use on the context. /// </summary> /// <param name="setting"></param> /// <param name="context"></param> /// <param name="value"></param> /// <returns></returns> public static IResult TryGetCMAction <T>(this IValueParser <T> setting, Context context, ref string value) { var split = value.Split(new[] { ' ' }, 2); //Do not allow numbers being parsed as the enum in case someone wants to add numbers to a collection var names = Enum.GetNames(typeof(CollectionModificationAction)); if (split.Length == 1) { //Only return success if this only value is NOT CMAction //b/c if someone messes up quotes it would attempt to set this otherwise var valid = !names.CaseInsContains(split[0]); return(valid ? SetValueResult.FromSuccess(setting, value, $"Defaulting action to {CollectionModificationAction.Toggle}.") : SetValueResult.FromError(setting, value, "Cannot provide only an action.")); } if (names.CaseInsContains(split[0])) { context.Action = (CollectionModificationAction)Enum.Parse(typeof(CollectionModificationAction), split[0], true); value = split[1]; return(SetValueResult.FromSuccess(setting, value, $"Set action to {context.Action}.")); } else { return(SetValueResult.FromSuccess(setting, value, $"Defaulting action to {CollectionModificationAction.Toggle}.")); } }
/// <summary> /// Modifies the collection in the specified way. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="setting"></param> /// <param name="source"></param> /// <param name="value"></param> /// <param name="context"></param> /// <returns></returns> public static IResult ModifyCollection <T>(this IValueParser <T> setting, ICollection <T> source, T value, Context context) { setting.ThrowIfInvalid(value); bool success; switch (context.Action) { case CollectionModificationAction.Toggle: var rCount = source.RemoveAll(value, setting.EqualityComparer, context.MaxRemovalCount); if (rCount <= 0) //Only add if nothing was removed { source.Add(value); } success = true; break; case CollectionModificationAction.Add: source.Add(value); success = true; break; case CollectionModificationAction.AddIfMissing: var contains = source.Contains(value, setting.EqualityComparer); if (!contains) //Only add if not contained in collection { source.Add(value); } success = !contains; break; case CollectionModificationAction.Remove: success = source.RemoveAll(value, setting.EqualityComparer, context.MaxRemovalCount) > 0; //Failure if removed nothing break; default: throw new ArgumentException("Invalid action supplied.", nameof(context)); } return(success ? SetValueResult.FromSuccess(setting, value, $"Successfully {context.ActionString}.") : SetValueResult.FromError(setting, value, $"Already {context.ActionString}.")); }