about summary refs log tree commit diff
path: root/tvix/castore/src/nodes/symlink_target.rs
blob: 838cdaaeda5b1378aecd0681d32cd7bdfc3caab6 (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// TODO: split out this error
use crate::ValidateNodeError;

use bstr::ByteSlice;
use std::fmt::{self, Debug, Display};

/// A wrapper type for symlink targets.
/// Internally uses a [bytes::Bytes], but disallows empty targets and those
/// containing null bytes.
#[repr(transparent)]
#[derive(Clone, PartialEq, Eq)]
pub struct SymlinkTarget {
    inner: bytes::Bytes,
}

impl AsRef<[u8]> for SymlinkTarget {
    fn as_ref(&self) -> &[u8] {
        self.inner.as_ref()
    }
}

impl From<SymlinkTarget> for bytes::Bytes {
    fn from(value: SymlinkTarget) -> Self {
        value.inner
    }
}

impl TryFrom<bytes::Bytes> for SymlinkTarget {
    type Error = ValidateNodeError;

    fn try_from(value: bytes::Bytes) -> Result<Self, Self::Error> {
        if value.is_empty() || value.contains(&b'\0') {
            return Err(ValidateNodeError::InvalidSymlinkTarget(value));
        }

        Ok(Self { inner: value })
    }
}

impl TryFrom<&'static [u8]> for SymlinkTarget {
    type Error = ValidateNodeError;

    fn try_from(value: &'static [u8]) -> Result<Self, Self::Error> {
        if value.is_empty() || value.contains(&b'\0') {
            return Err(ValidateNodeError::InvalidSymlinkTarget(
                bytes::Bytes::from_static(value),
            ));
        }

        Ok(Self {
            inner: bytes::Bytes::from_static(value),
        })
    }
}

impl TryFrom<&str> for SymlinkTarget {
    type Error = ValidateNodeError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        if value.is_empty() {
            return Err(ValidateNodeError::InvalidSymlinkTarget(
                bytes::Bytes::copy_from_slice(value.as_bytes()),
            ));
        }

        Ok(Self {
            inner: bytes::Bytes::copy_from_slice(value.as_bytes()),
        })
    }
}

impl Debug for SymlinkTarget {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Debug::fmt(self.inner.as_bstr(), f)
    }
}

impl Display for SymlinkTarget {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        Display::fmt(self.inner.as_bstr(), f)
    }
}