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

     1  package forwarding
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  )
     7  
     8  // Description returns a human-readable description of the session status.
     9  func (s Status) Description() string {
    10  	switch s {
    11  	case Status_Disconnected:
    12  		return "Disconnected"
    13  	case Status_ConnectingSource:
    14  		return "Connecting to source"
    15  	case Status_ConnectingDestination:
    16  		return "Connecting to destination"
    17  	case Status_ForwardingConnections:
    18  		return "Forwarding"
    19  	default:
    20  		return "Unknown"
    21  	}
    22  }
    23  
    24  // MarshalText implements encoding.TextMarshaler.MarshalText.
    25  func (s Status) MarshalText() ([]byte, error) {
    26  	var result string
    27  	switch s {
    28  	case Status_Disconnected:
    29  		result = "disconnected"
    30  	case Status_ConnectingSource:
    31  		result = "connecting-source"
    32  	case Status_ConnectingDestination:
    33  		result = "connecting-destination"
    34  	case Status_ForwardingConnections:
    35  		result = "forwarding"
    36  	default:
    37  		result = "unknown"
    38  	}
    39  	return []byte(result), nil
    40  }
    41  
    42  // ensureValid ensures that EndpointState's invariants are respected.
    43  func (s *EndpointState) ensureValid() error {
    44  	// A nil endpoint state is not valid.
    45  	if s == nil {
    46  		return errors.New("nil state")
    47  	}
    48  
    49  	// We could perform additional validation based on the session status and
    50  	// the endpoint connectivity, but it would be prohibitively complex, and all
    51  	// we're really concerned about here is memory safety and other structural
    52  	// invariants.
    53  
    54  	// Success.
    55  	return nil
    56  }
    57  
    58  // EnsureValid ensures that State's invariants are respected.
    59  func (s *State) EnsureValid() error {
    60  	// A nil state is not valid.
    61  	if s == nil {
    62  		return errors.New("nil state")
    63  	}
    64  
    65  	// We could perform additional validation based on the session status, but
    66  	// it would be prohibitively complex, and all we're really concerned about
    67  	// here is memory safety and other structural invariants.
    68  
    69  	// Ensure the session is valid.
    70  	if err := s.Session.EnsureValid(); err != nil {
    71  		return fmt.Errorf("invalid session: %w", err)
    72  	}
    73  
    74  	// Ensure that the connection counts are sane.
    75  	if s.OpenConnections > s.TotalConnections {
    76  		return errors.New("invalid connection counts")
    77  	}
    78  
    79  	// Ensure that endpoint states are valid.
    80  	if err := s.SourceState.ensureValid(); err != nil {
    81  		return fmt.Errorf("invalid source endpoint state: %w", err)
    82  	} else if err = s.DestinationState.ensureValid(); err != nil {
    83  		return fmt.Errorf("invalid destination endpoint state: %w", err)
    84  	}
    85  
    86  	// Success.
    87  	return nil
    88  }