codes_gs1_gln/lib.rs
1/*!
2This package contains an implementation of the
3[GS1 GLN](https://www.gs1.org/standards/id-keys/gln) Global Location Number (GLN) specification.
4
5The Global Location Number (GLN) provides a global supply chain solution by uniquely identifying parties and locations that are involved in business transactions.
6
7The GLN Allocation Rules Standard and contained GLN Management Rules is designed to help industry make consistent decisions about the unique identification of parties and locations in open supply chains. This standard has been developed in accordance with the GS1 Global Standards Management Process (GSMP) and is considered part of the GS1 system of standards. Overall, costs are minimised when all partners in the supply chain adhere to the GLN Allocation Rules Standard.
8
9Unique identification is critical to maintaining operational efficiencies that business partners rely on to exchange information in consistent ways, as well as, ensuring the smooth operations of global supply and value chains. More specifically, the unique identification of parties and locations is critical for efficient logistic operations, traceability programs, recall readiness, and more. It is essential that accurate and up-to-date information on parties and locations is able to be readily shared between business partners to allow the “who” and “where” of business to be reliably answered no matter the use case.
10
11# Example
12
13```rust
14use codes_check_digits::CodeWithCheckDigits;
15use codes_common::Code;
16use codes_gs1_gln::GlobalLocationNumber;
17use std::str::FromStr;
18
19let gln = GlobalLocationNumber::from_str("9436465792104").unwrap();
20
21assert_eq!(gln.data_no_check_digit(), "943646579210");
22assert_eq!(gln.check_digit_as_str(), "4");
23
24assert!(!GlobalLocationNumber::is_valid("9436465792109"));
25```
26
27```rust
28use codes_gs1_gln::GS1_GLN;
29
30assert_eq!(GS1_GLN.title(), "Global Location Number (GLN)");
31```
32
33# Features
34
35By default only the `serde` feature is enabled.
36
37* `serde` - Enables serialization of the [GlobalLocationNumber] type.
38
39*/
40
41#![warn(
42 unknown_lints,
43 // ---------- Stylistic
44 absolute_paths_not_starting_with_crate,
45 elided_lifetimes_in_paths,
46 explicit_outlives_requirements,
47 macro_use_extern_crate,
48 nonstandard_style, /* group */
49 noop_method_call,
50 rust_2018_idioms,
51 single_use_lifetimes,
52 trivial_casts,
53 trivial_numeric_casts,
54 // ---------- Future
55 future_incompatible, /* group */
56 rust_2021_compatibility, /* group */
57 // ---------- Public
58 missing_debug_implementations,
59 // missing_docs,
60 unreachable_pub,
61 // ---------- Unsafe
62 unsafe_code,
63 unsafe_op_in_unsafe_fn,
64 // ---------- Unused
65 unused, /* group */
66)]
67#![deny(
68 // ---------- Public
69 exported_private_dependencies,
70 private_in_public,
71 // ---------- Deprecated
72 anonymous_parameters,
73 bare_trait_objects,
74 ellipsis_inclusive_range_patterns,
75 // ---------- Unsafe
76 deref_nullptr,
77 drop_bounds,
78 dyn_drop,
79)]
80
81use codes_agency::{standardized_type, Agency, Standard};
82use codes_check_digits::{check_digits_impl, gs1, CodeWithCheckDigits};
83use codes_common::{code_as_str, code_impl, fixed_length_code};
84
85#[cfg(feature = "serde")]
86use serde::{Deserialize, Serialize};
87
88// ------------------------------------------------------------------------------------------------
89// Public Types
90// ------------------------------------------------------------------------------------------------
91
92///
93/// An instance of the `Standard` struct defined in the
94/// [`codes_agency`](https://docs.rs/codes-agency/latest/codes_agency/)
95/// package that describes the ISO-10383 specification.
96///
97pub const GS1_GLN: Standard = Standard::new(
98 Agency::GS1,
99 "GLN",
100 "Global Location Number (GLN)",
101 "https://www.gs1.org/standards/id-keys/gln",
102);
103
104///
105/// Encapsulates the complete Global Location Number (GLN) including
106/// check digit as a string.
107///
108#[derive(Clone, Debug, PartialEq, Eq, Hash)]
109#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
110pub struct GlobalLocationNumber(String);
111
112pub use codes_common::CodeParseError as GlobalLocationNumberError;
113
114// ------------------------------------------------------------------------------------------------
115// Implementations
116// ------------------------------------------------------------------------------------------------
117
118code_impl!(GlobalLocationNumber, as_str, str, String, to_string);
119
120code_as_str!(GlobalLocationNumber);
121
122check_digits_impl!(
123 GlobalLocationNumber,
124 GlobalLocationNumberError,
125 gs1::CheckDigitAlgorithm,
126 u8,
127 gs1::get_algorithm_instance(gs1::CodeFormat::Gln)
128);
129
130fixed_length_code!(GlobalLocationNumber, 13);
131
132standardized_type!(GlobalLocationNumber, GS1_GLN);
133
134// ------------------------------------------------------------------------------------------------
135// Unit Tests
136// ------------------------------------------------------------------------------------------------
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141 use codes_common::Code;
142
143 #[test]
144 fn test_is_valid() {
145 assert!(GlobalLocationNumber::is_valid("1234567890128"));
146
147 assert!(!GlobalLocationNumber::is_valid("9436465792103"));
148 }
149}