Skip to content

Commit

Permalink
Add All API Errors via Macro
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMc86 committed Feb 15, 2023
1 parent f7cdf48 commit bf703af
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 48 deletions.
1 change: 1 addition & 0 deletions ni-syscfg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ni-syscfg-sys = { path = "../ni-syscfg-sys", version="20.5" }
thiserror = "1"
num-derive = "0.3"
num-traits = "0.2"
paste = "1.0"

[[bin]]
name = "ni-syscfg"
Expand Down
258 changes: 210 additions & 48 deletions ni-syscfg/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ni_syscfg_sys::*;
use paste::paste;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -13,9 +14,37 @@ pub enum NiSystemConfigurationError {
UnexpectedEnumValue(i32),
}

#[derive(Clone, Copy, Debug)]
pub enum NiSysCfgApiStatus {
Unknown(NISysCfgStatus),
macro_rules! syscfg_error {
( $( $err:ident ),* ) => {
/// A rust representation of the different codes the API can return.
#[derive(Debug, PartialEq)]
pub enum NiSysCfgApiStatus {
Unknown(NISysCfgStatus),
$(
$err,
)*
}

impl From<i32> for NiSysCfgApiStatus {
fn from(item: i32) -> Self {
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
match item {
$(
paste!{[<NISysCfgStatus_NISysCfg_ $err>]} => Self::$err,
)*
_ => Self::Unknown(item),
}
}
}



};
}

// I've used a macro and then a vim search/replace to extract the specific names from the bindings.
syscfg_error!(
OK,
EndOfEnum,
SelfTestBasicOnly,
Expand All @@ -38,7 +67,7 @@ pub enum NiSysCfgApiStatus {
UriExpertDoesNotExist,
ItemDoesNotExist,
InvalidMode,
SysConfigApiNotInstalled,
SysConfigAPINotInstalled,
NameSyntaxIllegal,
NameCollision,
NoPropValidated,
Expand All @@ -50,50 +79,166 @@ pub enum NiSysCfgApiStatus {
ResponseSyntax,
ResourceIsNotPresent,
ResourceIsSimulated,
}

impl From<i32> for NiSysCfgApiStatus {
fn from(item: i32) -> Self {
#![allow(non_upper_case_globals)]
match item {
NISysCfgStatus_NISysCfg_OK => Self::OK,
NISysCfgStatus_NISysCfg_EndOfEnum => Self::EndOfEnum,
NISysCfgStatus_NISysCfg_SelfTestBasicOnly => Self::SelfTestBasicOnly,
NISysCfgStatus_NISysCfg_FoundCachedOfflineSystem => Self::FoundCachedOfflineSystem,
NISysCfgStatus_NISysCfg_RestartLocalhostInitiated => Self::RestartLocalhostInitiated,
NISysCfgStatus_NISysCfg_ChangedPropertyNotSaved => Self::ChangedPropertyNotSaved,
NISysCfgStatus_NISysCfg_NotImplemented => Self::NotImplemented,
NISysCfgStatus_NISysCfg_NullPointer => Self::NullPointer,
NISysCfgStatus_NISysCfg_Fail => Self::Fail,
NISysCfgStatus_NISysCfg_Unexpected => Self::Unexpected,
NISysCfgStatus_NISysCfg_OutOfMemory => Self::OutOfMemory,
NISysCfgStatus_NISysCfg_InvalidArg => Self::InvalidArg,
NISysCfgStatus_NISysCfg_OperationTimedOut => Self::OperationTimedOut,
NISysCfgStatus_NISysCfg_FileNotFound => Self::FileNotFound,
NISysCfgStatus_NISysCfg_InvalidMACFormat => Self::InvalidMACFormat,
NISysCfgStatus_NISysCfg_PropMismatch => Self::PropMismatch,
NISysCfgStatus_NISysCfg_PropDoesNotExist => Self::PropDoesNotExist,
NISysCfgStatus_NISysCfg_UriIllegalSyntax => Self::UriIllegalSyntax,
NISysCfgStatus_NISysCfg_UriTargetDoesNotExist => Self::UriTargetDoesNotExist,
NISysCfgStatus_NISysCfg_UriExpertDoesNotExist => Self::UriExpertDoesNotExist,
NISysCfgStatus_NISysCfg_ItemDoesNotExist => Self::ItemDoesNotExist,
NISysCfgStatus_NISysCfg_InvalidMode => Self::InvalidMode,
NISysCfgStatus_NISysCfg_SysConfigAPINotInstalled => Self::SysConfigApiNotInstalled,
NISysCfgStatus_NISysCfg_NameSyntaxIllegal => Self::NameSyntaxIllegal,
NISysCfgStatus_NISysCfg_NameCollision => Self::NameCollision,
NISysCfgStatus_NISysCfg_NoPropValidated => Self::NoPropValidated,
NISysCfgStatus_NISysCfg_UriUnauthorized => Self::UriUnauthorized,
NISysCfgStatus_NISysCfg_RenameResourceDependencies => Self::RenameResourceDependencies,
NISysCfgStatus_NISysCfg_ValueInvalid => Self::ValueInvalid,
NISysCfgStatus_NISysCfg_ValuesInconsistent => Self::ValuesInconsistent,
NISysCfgStatus_NISysCfg_Canceled => Self::Canceled,
NISysCfgStatus_NISysCfg_ResponseSyntax => Self::ResponseSyntax,
NISysCfgStatus_NISysCfg_ResourceIsNotPresent => Self::ResourceIsNotPresent,
NISysCfgStatus_NISysCfg_ResourceIsSimulated => Self::ResourceIsSimulated,
_ => Self::Unknown(item),
}
}
}
NotInFirmwareUpdateState,
FirmwareImageDeviceMismatch,
FirmwareImageCorrupt,
InvalidFirmwareVersion,
OlderFirmwareVersion,
InvalidLoginCredentials,
FirmwareUpdateAttemptFailed,
EncryptionFailed,
SomePropsNotValidated,
InvalidCalibrationCredentials,
CannotDeletePresentResource,
UriTargetTransmitError,
DecryptionFailed,
FirmwareExpertVersionMismatch,
AmbiguousImportAction,
RequiredItemFailedImport,
ItemInUse,
ItemTypeNotSupported,
PermissionDenied,
SystemNotFound,
TransformFailed,
NotInstalled,
LaunchFailure,
InternalTimeout,
MissingTransform,
IncorrectExtension,
FileReadOnly,
ReportOverwrite,
DirectoryError,
CannotOpenFile,
InsufficientPermissions,
NCECopierFailed,
FileOperationFailed,
NameCollisionError,
UnexpectedError,
NCENoStreamError,
NCECompressionError,
NCEStreamReadError,
NCEStreamWriteError,
NCEStreamSeekError,
NCERepoNotReady,
NCERepoInvalid,
NCERepoIncompat,
NCENoImportStorage,
NCENoExportStorage,
NCENoObjCopier,
CopyInProgress,
FileNotRecognized,
SystemNotSupported,
SystemNotReachable,
ProductSoftwareNotInstalled,
ProductSoftwareTooOld,
ProductSoftwareTooNew,
DataTooOld,
DataTooNew,
NoItemsToCopy,
OrphanItems,
DirtyItems,
FileOverwrite,
ItemOverwrite,
MissingDependency,
OperationCanceled,
WarningConflicts,
ErrorConflicts,
ItemsRequireUserInput,
ProductExpertNotReady,
OrphanFiles,
IsConst,
UnsupportedProductMode,
HostSoftwareTooOld,
OpkgUpdateFeedFailure,
FeedNotFound,
FeedAlreadyExists,
InstallOptionNotSupported,
FirmwareTooOld,
SoftwareTooOld,
RequiresSSH,
OpkgResponseSyntax,
WrongSoftwareSetType,
RequiresOpkg,
HDFormatEncryptNotSupported,
HDFormatNoRecoveryKeyDevice,
RestartLocalhostAmbiguous,
ImageInvalidCorrupt,
SafeOrInstallModeRequired,
EncryptPhraseMismatch,
InvalidIP,
InvalidGateway,
InvalidDNS,
InvalidSubnet,
CmdNotSupported,
ConfigFailed,
Locked,
BadPassword,
NotConfigurable,
UnlockFailed,
LockFailed,
InstallFailed,
InstallationCorrupt,
EmptyFile,
UnconfiguredIP,
InstallationGenericFailure,
DownloadAlreadyStarted,
Aborted,
DiskFull,
HDFormatFailed,
HDFormatNotSafeMode,
HDFormatRebootFailed,
ConnectionRefused,
GetRemoteFilesFailed,
PutRemoteFilesFailed,
InvalidImage,
ImageDeviceCodeMismatch,
SystemMismatch,
HDFormatWrongFS,
CustomInstallNotSupported,
FTPFailed,
Timeout,
DirNotFound,
PathNotFound,
NoSoftwareAvailable,
OverwriteError,
HDFormatCannotKeepCfg,
FileOrPathTooLong,
DDPInternalTimeout,
IOPermissionDenied,
PathAlreadyExists,
ExecutionFailure,
DownloadError,
NetSendFailed,
ContactHostDisconnected,
NetSvcDown,
NotConfirmed,
HostNotResolved,
RebootTimeout,
NoConfirmationFP1600,
DuplicateStartup,
RemoteInvalidArgument,
NotUninstallable,
DuplicatesNotAllowed,
NotInstallable,
WrongDevice,
WrongOS,
OSVersionTooOld,
IOError,
CorruptConfig,
BufferOverflow,
UnsupportedCDFVersion,
InvalidStack,
IncompleteStack,
StackItemMissing,
TopLevelHiddenComponentError,
InvalidAddon,
NoRTImagesFolder,
NoRTImagesRegistry,
NoRTS2CDF,
UnsupportedOS,
ExactVersionRequired,
InvalidStartup
);

pub type Result<T> = std::result::Result<T, NiSystemConfigurationError>;

Expand All @@ -104,3 +249,20 @@ pub fn api_status(status: NISysCfgStatus) -> Result<NiSysCfgApiStatus> {
return Err(NiSystemConfigurationError::ApiError(status.into()));
}
}

#[cfg(test)]
mod tests {
use super::NiSysCfgApiStatus;

#[test]
fn basic_error_test() {
assert_eq!(
NiSysCfgApiStatus::from(-2147418113i32),
NiSysCfgApiStatus::Unexpected
);
assert_eq!(
NiSysCfgApiStatus::from(-2147220277i32),
NiSysCfgApiStatus::InvalidStartup
)
}
}

0 comments on commit bf703af

Please sign in to comment.