pub struct Cnpj { /* private fields */ }Expand description
A validated CNPJ (Cadastro Nacional da Pessoa Jurídica).
Cnpj is a 14-byte, Copy, allocation-free value object. Once constructed, it is guaranteed to
satisfy the structural rules and Módulo 11 checksum required by the crate — there is no way to
obtain a Cnpj that hasn’t passed validation.
Internally, the identifier is stored as raw uppercase ASCII bytes ('0'...='9' or 'A'...='Z').
This keeps the compact representation lossless and makes borrowed access to the normalized form cheap.
§Constructing a Cnpj
| Constructor | Accepts |
|---|---|
Cnpj::parse / Cnpj::new | Punctuated or compact strings, any ASCII case |
Cnpj::from_bytes | Exactly 14 pre-normalized ASCII bytes, no punctuation |
FromStr / TryFrom<&str> | Same as parse, for use in generic code |
All of them run the same validation and return CnpjError on failure. See the module-level
documentation for the field layout, format history, and design rationale.
Implementations§
Source§impl Cnpj
impl Cnpj
Sourcepub fn parse(input: &str) -> Result<Self, CnpjError>
pub fn parse(input: &str) -> Result<Self, CnpjError>
Parses a CNPJ from a string.
The parser accepts the conventional AA.AAA.AAA/AAAA-DD form as well as the compact
14-character form. It also tolerates surrounding and embedded ASCII spaces and folds ASCII
letters to uppercase before validation.
This is the primary constructor; Cnpj::new, FromStr, and TryFrom<&str> all delegate to it.
§Errors
Returns CnpjError if the input is empty, does not contain exactly 14 meaningful
characters after formatting is removed, contains a character invalid for its position,
consists of a single repeated character, or fails the checksum.
§Examples
use ftracker_identifiers::Cnpj;
assert!(Cnpj::parse("00.000.000/0001-91").is_ok());
assert!(Cnpj::parse("00000000000191").is_ok());
assert!(Cnpj::parse("12abc34501de35").is_ok()); // lowercase is folded automatically
assert!(Cnpj::parse("not-a-cnpj").is_err());Sourcepub fn new(input: &str) -> Result<Self, CnpjError>
pub fn new(input: &str) -> Result<Self, CnpjError>
Alias for Cnpj::parse.
§Errors
See Cnpj::parse.
§Examples
use ftracker_identifiers::Cnpj;
assert_eq!(Cnpj::new("00000000000191"), Cnpj::parse("00000000000191"));Sourcepub fn from_bytes(bytes: [u8; 14]) -> Result<Self, CnpjError>
pub fn from_bytes(bytes: [u8; 14]) -> Result<Self, CnpjError>
Constructs a Cnpj directly from 14 raw ASCII bytes.
Each byte must already be an ASCII digit, and for the first 12 positions may also be an
uppercase ASCII letter. Use Cnpj::parse if the input might contain punctuation or
lowercase letters.
Numeric-only CNPJs remain fully supported. Pass ASCII digit bytes (b'0'...=b'9'), not raw
numeric values.
§Errors
Returns CnpjError under the same conditions as Cnpj::parse, except that length is
guaranteed by the [u8; 14] type itself: CnpjError::InvalidLength cannot occur here.
§Examples
use ftracker_identifiers::Cnpj;
let cnpj = Cnpj::from_bytes(*b"00000000000191").unwrap();
assert_eq!(cnpj.as_str(), "00000000000191");
// A malformed checksum is rejected just like it would be through `parse`.
assert!(Cnpj::from_bytes(*b"00000000000192").is_err());Sourcepub fn as_bytes(&self) -> &[u8; 14]
pub fn as_bytes(&self) -> &[u8; 14]
Returns the 14 raw ASCII bytes backing this CNPJ.
The returned bytes are in compact form, without punctuation (for example, b"12ABC34501DE35").
§Examples
use ftracker_identifiers::Cnpj;
let cnpj = Cnpj::parse("00000000000191").unwrap();
assert_eq!(cnpj.as_bytes(), b"00000000000191");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Returns the compact CNPJ as a &str.
This never allocates: the bytes are guaranteed to be valid ASCII by construction.
§Examples
use ftracker_identifiers::Cnpj;
let cnpj = Cnpj::parse("00.000.000/0001-91").unwrap();
assert_eq!(cnpj.as_str(), "00000000000191");Sourcepub fn formatted(&self) -> FormattedCnpj
pub fn formatted(&self) -> FormattedCnpj
Renders the punctuated AA.AAA.AAA/AAAA-DD form without heap allocation.
See FormattedCnpj. Cnpj’s own Display implementation
delegates to this, so cnpj.to_string() and cnpj.formatted().to_string() are equivalent.
§Examples
use ftracker_identifiers::Cnpj;
let cnpj = Cnpj::parse("00000000000191").unwrap();
assert_eq!(cnpj.formatted().as_str(), "00.000.000/0001-91");
assert_eq!(cnpj.to_string(), cnpj.formatted().as_str());Sourcepub fn root(&self) -> &str
pub fn root(&self) -> &str
Returns the 8-character root segment.
This identifies the entity itself and is shared by the company and all of its branches.
§Examples
use ftracker_identifiers::Cnpj;
let cnpj = Cnpj::parse("00000000000191").unwrap();
assert_eq!(cnpj.root(), "00000000");Sourcepub fn branch_code(&self) -> &str
pub fn branch_code(&self) -> &str
Returns the 4-character branch/order segment.
"0001" conventionally denotes the head office (matriz); see Cnpj::is_root.
§Examples
use ftracker_identifiers::Cnpj;
let cnpj = Cnpj::parse("11.222.333/0002-62").unwrap();
assert_eq!(cnpj.branch_code(), "0002");Sourcepub fn is_root(&self) -> bool
pub fn is_root(&self) -> bool
Returns true when the branch/order segment is "0001".
§Examples
use ftracker_identifiers::Cnpj;
assert!(Cnpj::parse("00000000000191").unwrap().is_root());
assert!(!Cnpj::parse("11.222.333/0002-62").unwrap().is_root());Sourcepub fn branch_number(&self) -> Option<u16>
pub fn branch_number(&self) -> Option<u16>
Returns the branch/order segment as a number when it is purely numeric.
Returns None when the segment contains a letter, which is only possible for
alphanumeric-format CNPJs. Numeric CNPJs, including the conventional matriz marker
("0001"), always parse successfully.
§Examples
use ftracker_identifiers::Cnpj;
let matriz = Cnpj::parse("00000000000191").unwrap();
assert_eq!(matriz.branch_number(), Some(1));
let alphanumeric_branch = Cnpj::parse("12ABC34501DE35").unwrap();
assert_eq!(alphanumeric_branch.branch_code(), "01DE");
assert_eq!(alphanumeric_branch.branch_number(), None);Sourcepub fn check_digits(&self) -> (u8, u8)
pub fn check_digits(&self) -> (u8, u8)
Returns the two verification digits as numeric values.
§Examples
use ftracker_identifiers::Cnpj;
let cnpj = Cnpj::parse("00000000000191").unwrap();
assert_eq!(cnpj.check_digits(), (9, 1));Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Cnpj
impl<'a> Arbitrary<'a> for Cnpj
Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreimpl Copy for Cnpj
Source§impl<'de> Deserialize<'de> for Cnpj
impl<'de> Deserialize<'de> for Cnpj
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl Eq for Cnpj
Source§impl JsonSchema for Cnpj
impl JsonSchema for Cnpj
Source§fn json_schema(_: &mut SchemaGenerator) -> Schema
fn json_schema(_: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref keyword. Read moreSource§impl Ord for Cnpj
impl Ord for Cnpj
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Cnpj
impl PartialOrd for Cnpj
impl StructuralPartialEq for Cnpj
Source§impl TryFrom<&[u8]> for Cnpj
impl TryFrom<&[u8]> for Cnpj
Source§fn try_from(value: &[u8]) -> Result<Self, Self::Error>
fn try_from(value: &[u8]) -> Result<Self, Self::Error>
Validates a byte slice as a CNPJ. The slice must be exactly 14 pre normalized ASCII bytes,
without punctuation; any other length yields CnpjError::InvalidLength. Once the length is
confirmed, this behaves like Cnpj::from_bytes.