Skip to content

Commit 902b81d

Browse files
committed
feat(types): enhance Resource struct with entry management documentation
- Added detailed documentation for `add_entry`, `find_entry`, and `find_entry_mut` methods in the `Resource` struct. - Included usage examples in Rust to demonstrate how to add and retrieve entries, improving usability and clarity for developers. - This change aims to provide better guidance for users interacting with the `Resource` API.
1 parent 20b056c commit 902b81d

1 file changed

Lines changed: 85 additions & 1 deletion

File tree

langcodec/src/types.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,98 @@ pub struct Resource {
4646
}
4747

4848
impl Resource {
49-
pub(crate) fn add_entry(&mut self, entry: Entry) {
49+
/// Add an entry to the resource.
50+
///
51+
/// ```rust
52+
/// use langcodec::types::{Resource, Entry, Translation, EntryStatus, Metadata};
53+
/// use std::collections::HashMap;
54+
///
55+
/// let mut resource = Resource {
56+
/// metadata: Metadata {
57+
/// language: "en".to_string(),
58+
/// domain: "test".to_string(),
59+
/// custom: HashMap::new(),
60+
/// },
61+
/// entries: Vec::new(),
62+
/// };
63+
/// resource.add_entry(Entry {
64+
/// id: "hello".to_string(),
65+
/// value: Translation::Singular("Hello".to_string()),
66+
/// status: EntryStatus::Translated,
67+
/// comment: None,
68+
/// custom: HashMap::new(),
69+
/// });
70+
/// ```
71+
///
72+
/// # Returns
73+
///
74+
/// The added entry.
75+
pub fn add_entry(&mut self, entry: Entry) {
5076
self.entries.push(entry);
5177
}
5278

79+
/// Find an entry by its id.
80+
///
81+
/// ```rust
82+
/// use langcodec::types::{Resource, Entry, Translation, EntryStatus, Metadata};
83+
/// use std::collections::HashMap;
84+
///
85+
/// let mut resource = Resource {
86+
/// metadata: Metadata {
87+
/// language: "en".to_string(),
88+
/// domain: "test".to_string(),
89+
/// custom: HashMap::new(),
90+
/// },
91+
/// entries: Vec::new(),
92+
/// };
93+
/// resource.add_entry(Entry {
94+
/// id: "hello".to_string(),
95+
/// value: Translation::Singular("Hello".to_string()),
96+
/// status: EntryStatus::Translated,
97+
/// comment: None,
98+
/// custom: HashMap::new(),
99+
/// });
100+
/// let entry = resource.find_entry("hello").unwrap();
101+
/// assert_eq!(entry.value, Translation::Singular("Hello".to_string()));
102+
/// assert_eq!(entry.status, EntryStatus::Translated);
103+
/// assert_eq!(entry.comment, None);
104+
/// ```
53105
pub fn find_entry(&self, id: &str) -> Option<&Entry> {
54106
self.entries.iter().find(|e| e.id == id)
55107
}
56108

109+
/// Find a mutable entry by its id.
110+
///
111+
/// ```rust
112+
/// use langcodec::types::{Resource, Entry, Translation, EntryStatus, Metadata};
113+
/// use std::collections::HashMap;
114+
///
115+
/// let mut resource = Resource {
116+
/// metadata: Metadata {
117+
/// language: "en".to_string(),
118+
/// domain: "test".to_string(),
119+
/// custom: HashMap::new(),
120+
/// },
121+
/// entries: Vec::new(),
122+
/// };
123+
/// resource.add_entry(Entry {
124+
/// id: "hello".to_string(),
125+
/// value: Translation::Singular("Hello".to_string()),
126+
/// status: EntryStatus::Translated,
127+
/// comment: None,
128+
/// custom: HashMap::new(),
129+
/// });
130+
/// let entry = resource.find_entry_mut("hello").unwrap();
131+
/// assert_eq!(entry.value, Translation::Singular("Hello".to_string()));
132+
/// assert_eq!(entry.status, EntryStatus::Translated);
133+
/// assert_eq!(entry.comment, None);
134+
/// entry.value = Translation::Singular("Hello, World!".to_string());
135+
/// entry.status = EntryStatus::NeedsReview;
136+
/// entry.comment = Some("Hello, World!".to_string());
137+
/// assert_eq!(entry.value, Translation::Singular("Hello, World!".to_string()));
138+
/// assert_eq!(entry.status, EntryStatus::NeedsReview);
139+
/// assert_eq!(entry.comment, Some("Hello, World!".to_string()));
140+
/// ```
57141
pub fn find_entry_mut(&mut self, id: &str) -> Option<&mut Entry> {
58142
self.entries.iter_mut().find(|e| e.id == id)
59143
}

0 commit comments

Comments
 (0)