github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/service/prompting/prompting.go (about)

     1  package prompting
     2  
     3  import (
     4  	"errors"
     5  )
     6  
     7  // hostRequestMode indicates the mode for a HostRequest.
     8  type hostRequestMode uint8
     9  
    10  const (
    11  	// hostRequestModeInitial represents an initial request.
    12  	hostRequestModeInitial hostRequestMode = iota
    13  	// hostRequestModeMessageResponse indicates a response to a message.
    14  	hostRequestModeMessageResponse
    15  	// hostRequestModePromptResponse indicates a response to a prompt.
    16  	hostRequestModePromptResponse
    17  )
    18  
    19  // ensureValid verifies that a HostRequest is valid.
    20  func (r *HostRequest) ensureValid(mode hostRequestMode) error {
    21  	// A nil hosting request is not valid.
    22  	if r == nil {
    23  		return errors.New("nil hosting request")
    24  	}
    25  
    26  	// Handle validation based on mode.
    27  	if mode == hostRequestModeInitial {
    28  		// Any setting for prompt allowance is valid.
    29  
    30  		// Ensure that the response is empty.
    31  		if r.Response != "" {
    32  			return errors.New("unexpected response value on initial request")
    33  		}
    34  	} else {
    35  		// Ensure that prompt allowance hasn't been re-specified.
    36  		if r.AllowPrompts {
    37  			return errors.New("unexpected prompt allowance specification")
    38  		}
    39  
    40  		// If responding to a message, ensure that the response is empty. For
    41  		// prompt responses, any value is allowed.
    42  		if mode == hostRequestModeMessageResponse && r.Response != "" {
    43  			return errors.New("unexpected response value when performing messaging")
    44  		}
    45  	}
    46  
    47  	// Success.
    48  	return nil
    49  }
    50  
    51  // EnsureValid verifies that a HostResponse is valid.
    52  func (r *HostResponse) EnsureValid(first, allowPrompts bool) error {
    53  	// A nil hosting response is not valid.
    54  	if r == nil {
    55  		return errors.New("nil hosting response")
    56  	}
    57  
    58  	// Handle validation based on whether or not this is the first response.
    59  	if first {
    60  		// Ensure that the prompter identifier is specified.
    61  		if r.Identifier == "" {
    62  			return errors.New("empty prompter identifier")
    63  		}
    64  
    65  		// Ensure that no message type is specified.
    66  		if r.IsPrompt {
    67  			return errors.New("unexpected message type specification")
    68  		}
    69  
    70  		// Ensure that no message is provided.
    71  		if r.Message != "" {
    72  			return errors.New("unexpected message")
    73  		}
    74  	} else {
    75  		// Ensure that the prompter identifier isn't specified again.
    76  		if r.Identifier != "" {
    77  			return errors.New("unexpected prompter identifier")
    78  		}
    79  
    80  		// Ensure that the message type is allowed.
    81  		if r.IsPrompt && !allowPrompts {
    82  			return errors.New("disallowed prompt message type")
    83  		}
    84  
    85  		// Any value of the message is considered valid.
    86  	}
    87  
    88  	// Success.
    89  	return nil
    90  }
    91  
    92  // ensureValid verifies that a PromptRequest is valid.
    93  func (r *PromptRequest) ensureValid() error {
    94  	// A nil prompt request is not valid.
    95  	if r == nil {
    96  		return errors.New("nil prompt request")
    97  	}
    98  
    99  	// Verify that the prompter identifier is non-empty.
   100  	if r.Prompter == "" {
   101  		return errors.New("empty prompter identifier")
   102  	}
   103  
   104  	// Any value of the prompt is considered valid.
   105  
   106  	// Success.
   107  	return nil
   108  }
   109  
   110  // EnsureValid verifies that a PromptResponse is valid.
   111  func (r *PromptResponse) EnsureValid() error {
   112  	// A nil prompt response is not valid.
   113  	if r == nil {
   114  		return errors.New("nil prompt response")
   115  	}
   116  
   117  	// Any value of the response itself is considered valid.
   118  
   119  	// Success.
   120  	return nil
   121  }