1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright (c) 2020 Timo Savola.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

//! Find other program instances.

use std::fmt;

pub mod principal;

#[derive(Debug, Eq, PartialEq)]
pub enum ErrorKind {
    Other,
    NotRegistered,
}

#[derive(Debug)]
pub struct Error {
    code: i16,
}

impl Error {
    fn new(code: i16) -> Self {
        Self { code }
    }

    pub fn kind(&self) -> ErrorKind {
        match self.code {
            1 => ErrorKind::NotRegistered,
            _ => ErrorKind::Other,
        }
    }

    pub fn as_i16(&self) -> i16 {
        self.code
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        match self.kind() {
            ErrorKind::NotRegistered => f.write_str("not registered"),
            _ => self.code.fmt(f),
        }
    }
}