| 40 | } |
| 41 | |
| 42 | func Parse(shortcut string) (*Accelerator, error) { |
| 43 | var result Accelerator |
| 44 | |
| 45 | // Split the shortcut by + |
| 46 | components := strings.Split(shortcut, "+") |
| 47 | |
| 48 | // If we only have one it should be a key |
| 49 | // We require components |
| 50 | if len(components) == 0 { |
| 51 | return nil, fmt.Errorf("no components given to validateComponents") |
| 52 | } |
| 53 | |
| 54 | // Keep track of modifiers we have processed |
| 55 | var modifiersProcessed slicer.StringSlicer |
| 56 | |
| 57 | // Check components |
| 58 | for index, component := range components { |
| 59 | |
| 60 | // If last component |
| 61 | if index == len(components)-1 { |
| 62 | processedkey, validKey := parseKey(component) |
| 63 | if !validKey { |
| 64 | return nil, fmt.Errorf("'%s' is not a valid key", component) |
| 65 | } |
| 66 | result.Key = processedkey |
| 67 | continue |
| 68 | } |
| 69 | |
| 70 | // Not last component - needs to be modifier |
| 71 | lowercaseComponent := strings.ToLower(component) |
| 72 | thisModifier, valid := modifierMap[lowercaseComponent] |
| 73 | if !valid { |
| 74 | return nil, fmt.Errorf("'%s' is not a valid modifier", component) |
| 75 | } |
| 76 | // Needs to be unique |
| 77 | if modifiersProcessed.Contains(lowercaseComponent) { |
| 78 | return nil, fmt.Errorf("Modifier '%s' is defined twice for shortcut: %s", component, shortcut) |
| 79 | } |
| 80 | |
| 81 | // Save this data |
| 82 | result.Modifiers = append(result.Modifiers, thisModifier) |
| 83 | modifiersProcessed.Add(lowercaseComponent) |
| 84 | } |
| 85 | |
| 86 | return &result, nil |
| 87 | } |